|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"database/sql" |
| 6 | +"net/http" |
| 7 | +"strconv" |
| 8 | +"time" |
| 9 | + |
| 10 | +"github.com/gin-gonic/gin" |
| 11 | +"github.com/google/uuid" |
| 12 | +db"github.com/wpcodevo/golang-postgresql-api/db/sqlc" |
| 13 | +"github.com/wpcodevo/golang-postgresql-api/models" |
| 14 | +) |
| 15 | + |
| 16 | +typePostControllerstruct { |
| 17 | +db*db.Queries |
| 18 | +ctx context.Context |
| 19 | +} |
| 20 | + |
| 21 | +funcNewPostController(db*db.Queries,ctx context.Context)*PostController { |
| 22 | +return&PostController{db,ctx} |
| 23 | +} |
| 24 | + |
| 25 | +func (ac*PostController)CreatePost(ctx*gin.Context) { |
| 26 | +varpayload*models.CreatePost |
| 27 | + |
| 28 | +iferr:=ctx.ShouldBindJSON(&payload);err!=nil { |
| 29 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 30 | +return |
| 31 | +} |
| 32 | + |
| 33 | +now:=time.Now() |
| 34 | +args:=&db.CreatePostParams{ |
| 35 | +Title:payload.Title, |
| 36 | +Category:payload.Category, |
| 37 | +Content:payload.Content, |
| 38 | +Image:payload.Image, |
| 39 | +CreatedAt:now, |
| 40 | +UpdatedAt:now, |
| 41 | +} |
| 42 | + |
| 43 | +post,err:=ac.db.CreatePost(ctx,*args) |
| 44 | + |
| 45 | +iferr!=nil { |
| 46 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 47 | +return |
| 48 | +} |
| 49 | + |
| 50 | +ctx.JSON(http.StatusCreated, gin.H{"status":"success","post":post}) |
| 51 | +} |
| 52 | + |
| 53 | +func (ac*PostController)UpdatePost(ctx*gin.Context) { |
| 54 | +varpayload*models.UpdatePost |
| 55 | +postId:=ctx.Param("postId") |
| 56 | + |
| 57 | +iferr:=ctx.ShouldBindJSON(&payload);err!=nil { |
| 58 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 59 | +return |
| 60 | +} |
| 61 | + |
| 62 | +now:=time.Now() |
| 63 | +args:=&db.UpdatePostParams{ |
| 64 | +ID:uuid.MustParse(postId), |
| 65 | +Title: sql.NullString{String:payload.Title,Valid:payload.Title!=""}, |
| 66 | +Category: sql.NullString{String:payload.Category,Valid:payload.Category!=""}, |
| 67 | +Content: sql.NullString{String:payload.Content,Valid:payload.Content!=""}, |
| 68 | +Image: sql.NullString{String:payload.Image,Valid:payload.Image!=""}, |
| 69 | +UpdatedAt: sql.NullTime{Time:now,Valid:true}, |
| 70 | +} |
| 71 | + |
| 72 | +post,err:=ac.db.UpdatePost(ctx,*args) |
| 73 | +iferr!=nil { |
| 74 | +iferr==sql.ErrNoRows { |
| 75 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that ID exists"}) |
| 76 | +return |
| 77 | +} |
| 78 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 79 | +return |
| 80 | +} |
| 81 | + |
| 82 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","post":post}) |
| 83 | +} |
| 84 | + |
| 85 | +func (ac*PostController)GetPostById(ctx*gin.Context) { |
| 86 | +postId:=ctx.Param("postId") |
| 87 | + |
| 88 | +post,err:=ac.db.GetPostById(ctx,uuid.MustParse(postId)) |
| 89 | +iferr!=nil { |
| 90 | +iferr==sql.ErrNoRows { |
| 91 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that ID exists"}) |
| 92 | +return |
| 93 | +} |
| 94 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 95 | +return |
| 96 | +} |
| 97 | + |
| 98 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","post":post}) |
| 99 | +} |
| 100 | + |
| 101 | +func (ac*PostController)GetAllPosts(ctx*gin.Context) { |
| 102 | +varpage=ctx.DefaultQuery("page","1") |
| 103 | +varlimit=ctx.DefaultQuery("limit","10") |
| 104 | + |
| 105 | +intPage,_:=strconv.Atoi(page) |
| 106 | +intLimit,_:=strconv.Atoi(limit) |
| 107 | +offset:= (intPage-1)*intLimit |
| 108 | + |
| 109 | +args:=&db.ListPostsParams{ |
| 110 | +Limit:int32(intLimit), |
| 111 | +Offset:int32(offset), |
| 112 | +} |
| 113 | + |
| 114 | +posts,err:=ac.db.ListPosts(ctx,*args) |
| 115 | +iferr!=nil { |
| 116 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 117 | +return |
| 118 | +} |
| 119 | + |
| 120 | +ifposts==nil { |
| 121 | +posts= []db.Post{} |
| 122 | +} |
| 123 | + |
| 124 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","results":len(posts),"data":posts}) |
| 125 | +} |
| 126 | + |
| 127 | +func (ac*PostController)DeletePostById(ctx*gin.Context) { |
| 128 | +postId:=ctx.Param("postId") |
| 129 | + |
| 130 | +_,err:=ac.db.GetPostById(ctx,uuid.MustParse(postId)) |
| 131 | +iferr!=nil { |
| 132 | +iferr==sql.ErrNoRows { |
| 133 | +ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":"No post with that ID exists"}) |
| 134 | +return |
| 135 | +} |
| 136 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 137 | +return |
| 138 | +} |
| 139 | + |
| 140 | +err=ac.db.DeletePost(ctx,uuid.MustParse(postId)) |
| 141 | +iferr!=nil { |
| 142 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 143 | +return |
| 144 | +} |
| 145 | + |
| 146 | +ctx.JSON(http.StatusNoContent, gin.H{"status":"success"}) |
| 147 | +} |