Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6625ae

Browse files
committed
updated
1 parentacef7b8 commita6625ae

File tree

9 files changed

+189
-14
lines changed

9 files changed

+189
-14
lines changed

‎.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tmp_dir = "tmp"
44

55
[build]
66
bin ="./tmp/main"
7-
cmd ="go build -o ./tmp/main ./cmd/server/main.go"
7+
cmd ="go build -o ./tmp/main ./main.go"
88
delay =1000
99
exclude_dir = ["assets","tmp","vendor","testdata"]
1010
exclude_file = []

‎controllers/post.controller.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

‎docker-compose.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,5 @@ services:
99
-progresDB:/var/lib/postgresql/data
1010
env_file:
1111
-./app.env
12-
13-
redis:
14-
image:redis:alpine
15-
container_name:redis
16-
ports:
17-
-'6379:6379'
18-
volumes:
19-
-redisDB:/data
2012
volumes:
2113
progresDB:
22-
redisDB:

‎go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ go 1.18
55
require (
66
github.com/gin-contrib/corsv1.3.1
77
github.com/gin-gonic/ginv1.7.7
8-
github.com/golang-jwt/jwtv3.2.2+incompatible
98
github.com/google/uuidv1.3.0
109
github.com/lib/pqv1.10.6
1110
github.com/spf13/viperv1.11.0
12-
golang.org/x/cryptov0.0.0-20220411220226-7b82a4e95df4
1311
)
1412

1513
require (
@@ -35,6 +33,7 @@ require (
3533
github.com/spf13/pflagv1.0.5// indirect
3634
github.com/subosito/gotenvv1.2.0// indirect
3735
github.com/ugorji/go/codecv1.1.7// indirect
36+
golang.org/x/cryptov0.0.0-20220411220226-7b82a4e95df4// indirect
3837
golang.org/x/sysv0.0.0-20220412211240-33da011f77ad// indirect
3938
golang.org/x/textv0.3.7// indirect
4039
google.golang.org/protobufv1.28.0// indirect

‎go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
7777
github.com/go-playground/universal-translatorv0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
7878
github.com/go-playground/validator/v10v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
7979
github.com/go-playground/validator/v10v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
80-
github.com/golang-jwt/jwtv3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
81-
github.com/golang-jwt/jwtv3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
8280
github.com/golang/glogv0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
8381
github.com/golang/groupcachev0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
8482
github.com/golang/groupcachev0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=

‎cmd/server/main.gorenamed to‎main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"github.com/gin-contrib/cors"
1111
"github.com/gin-gonic/gin"
1212
"github.com/wpcodevo/golang-postgresql-api/config"
13+
"github.com/wpcodevo/golang-postgresql-api/controllers"
1314
dbConn"github.com/wpcodevo/golang-postgresql-api/db/sqlc"
15+
"github.com/wpcodevo/golang-postgresql-api/routes"
1416

1517
_"github.com/lib/pq"
1618
)
@@ -19,6 +21,9 @@ var (
1921
server*gin.Engine
2022
db*dbConn.Queries
2123
ctx context.Context
24+
25+
PostController controllers.PostController
26+
PostRoutes routes.PostRoutes
2227
)
2328

2429
funcinit() {
@@ -38,6 +43,9 @@ func init() {
3843

3944
fmt.Println("PostgreSQL connected successfully...")
4045

46+
PostController=*controllers.NewPostController(db,ctx)
47+
PostRoutes=routes.NewRoutePost(PostController)
48+
4149
server=gin.Default()
4250
}
4351

@@ -60,6 +68,7 @@ func main() {
6068
ctx.JSON(http.StatusOK, gin.H{"status":"success","message":"Welcome to Golang with PostgreSQL"})
6169
})
6270

71+
PostRoutes.PostRoute(router)
6372
server.NoRoute(func(ctx*gin.Context) {
6473
ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":fmt.Sprintf("Route %s not found",ctx.Request.URL)})
6574
})

‎models/post.model.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ type CreatePost struct {
66
Contentstring`json:"content" binding:"required"`
77
Imagestring`json:"image" binding:"required"`
88
}
9+
10+
typeUpdatePoststruct {
11+
Titlestring`json:"title"`
12+
Categorystring`json:"category"`
13+
Contentstring`json:"content"`
14+
Imagestring`json:"image"`
15+
}

‎routes/post.route.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package routes
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/wpcodevo/golang-postgresql-api/controllers"
6+
)
7+
8+
typePostRoutesstruct {
9+
postController controllers.PostController
10+
}
11+
12+
funcNewRoutePost(postController controllers.PostController)PostRoutes {
13+
returnPostRoutes{postController}
14+
}
15+
16+
func (pc*PostRoutes)PostRoute(rg*gin.RouterGroup) {
17+
18+
router:=rg.Group("posts")
19+
router.POST("/",pc.postController.CreatePost)
20+
router.GET("/",pc.postController.GetAllPosts)
21+
router.PATCH("/:postId",pc.postController.UpdatePost)
22+
router.GET("/:postId",pc.postController.GetPostById)
23+
router.DELETE("/:postId",pc.postController.DeletePostById)
24+
}

‎tmp/main

-103 KB
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp