|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | +"net/http" |
| 5 | +"strconv" |
| 6 | +"strings" |
| 7 | +"time" |
| 8 | + |
| 9 | +"github.com/gin-gonic/gin" |
| 10 | +"github.com/wpcodevo/golang-gorm-postgres/models" |
| 11 | +"gorm.io/gorm" |
| 12 | +) |
| 13 | + |
| 14 | +typePostControllerstruct { |
| 15 | +DB*gorm.DB |
| 16 | +} |
| 17 | + |
| 18 | +funcNewPostController(DB*gorm.DB)PostController { |
| 19 | +returnPostController{DB} |
| 20 | +} |
| 21 | + |
| 22 | +func (pc*PostController)CreatePost(ctx*gin.Context) { |
| 23 | +currentUser:=ctx.MustGet("currentUser").(models.User) |
| 24 | +varpayload*models.CreatePostRequest |
| 25 | + |
| 26 | +iferr:=ctx.ShouldBindJSON(&payload);err!=nil { |
| 27 | +ctx.JSON(http.StatusBadRequest,err.Error()) |
| 28 | +return |
| 29 | +} |
| 30 | + |
| 31 | +now:=time.Now() |
| 32 | +newPost:= models.Post{ |
| 33 | +Title:payload.Title, |
| 34 | +Content:payload.Content, |
| 35 | +Image:payload.Image, |
| 36 | +User:currentUser.ID, |
| 37 | +CreatedAt:now, |
| 38 | +UpdatedAt:now, |
| 39 | +} |
| 40 | + |
| 41 | +result:=pc.DB.Create(&newPost) |
| 42 | +ifresult.Error!=nil { |
| 43 | +ifstrings.Contains(result.Error.Error(),"duplicate key") { |
| 44 | +ctx.JSON(http.StatusConflict, gin.H{"status":"fail","message":"Post with that title already exists"}) |
| 45 | +return |
| 46 | +} |
| 47 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":result.Error.Error()}) |
| 48 | +return |
| 49 | +} |
| 50 | + |
| 51 | +ctx.JSON(http.StatusCreated, gin.H{"status":"success","data":newPost}) |
| 52 | +} |
| 53 | + |
| 54 | +func (pc*PostController)UpdatePost(ctx*gin.Context) { |
| 55 | +postId:=ctx.Param("postId") |
| 56 | +currentUser:=ctx.MustGet("currentUser").(models.User) |
| 57 | + |
| 58 | +varpayload*models.UpdatePost |
| 59 | +iferr:=ctx.ShouldBindJSON(&payload);err!=nil { |
| 60 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"fail","message":err.Error()}) |
| 61 | +return |
| 62 | +} |
| 63 | +varupdatedPost models.Post |
| 64 | +result:=pc.DB.First(&updatedPost,"id = ?",postId) |
| 65 | +ifresult.Error!=nil { |
| 66 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that title exists"}) |
| 67 | +return |
| 68 | +} |
| 69 | +now:=time.Now() |
| 70 | +postToUpdate:= models.Post{ |
| 71 | +Title:payload.Title, |
| 72 | +Content:payload.Content, |
| 73 | +Image:payload.Image, |
| 74 | +User:currentUser.ID, |
| 75 | +CreatedAt:updatedPost.CreatedAt, |
| 76 | +UpdatedAt:now, |
| 77 | +} |
| 78 | + |
| 79 | +pc.DB.Model(&updatedPost).Updates(postToUpdate) |
| 80 | + |
| 81 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","data":updatedPost}) |
| 82 | +} |
| 83 | + |
| 84 | +func (pc*PostController)FindPostById(ctx*gin.Context) { |
| 85 | +postId:=ctx.Param("postId") |
| 86 | + |
| 87 | +varpost models.Post |
| 88 | +result:=pc.DB.First(&post,"id = ?",postId) |
| 89 | +ifresult.Error!=nil { |
| 90 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that title exists"}) |
| 91 | +return |
| 92 | +} |
| 93 | + |
| 94 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","data":post}) |
| 95 | +} |
| 96 | + |
| 97 | +func (pc*PostController)FindPosts(ctx*gin.Context) { |
| 98 | +varpage=ctx.DefaultQuery("page","1") |
| 99 | +varlimit=ctx.DefaultQuery("limit","10") |
| 100 | + |
| 101 | +intPage,_:=strconv.Atoi(page) |
| 102 | +intLimit,_:=strconv.Atoi(limit) |
| 103 | +offset:= (intPage-1)*intLimit |
| 104 | + |
| 105 | +varposts []models.Post |
| 106 | +results:=pc.DB.Limit(intLimit).Offset(offset).Find(&posts) |
| 107 | +ifresults.Error!=nil { |
| 108 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":results.Error}) |
| 109 | +return |
| 110 | +} |
| 111 | + |
| 112 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","results":len(posts),"data":posts}) |
| 113 | +} |
| 114 | + |
| 115 | +func (pc*PostController)DeletePost(ctx*gin.Context) { |
| 116 | +postId:=ctx.Param("postId") |
| 117 | + |
| 118 | +result:=pc.DB.Delete(&models.Post{},"id = ?",postId) |
| 119 | + |
| 120 | +ifresult.Error!=nil { |
| 121 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that title exists"}) |
| 122 | +return |
| 123 | +} |
| 124 | + |
| 125 | +ctx.JSON(http.StatusNoContent,nil) |
| 126 | +} |