Skip to content

Commit 976b390

Browse files
committed
dev CRUD user
1 parent 2abb7af commit 976b390

File tree

12 files changed

+147
-6
lines changed

12 files changed

+147
-6
lines changed

Diff for: .env

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ PORT=8080
22
GO_ENV=dev
33

44
DB_USERNAME=root
5-
DB_PASSWORD=123456
5+
DB_PASSWORD=bvquoc2003
66
DB_DATABASE=todoapp
77
DB_HOST=127.0.0.1:3306

Diff for: component/appconfig/app_config.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package appconfig
22

33
type AppConfig struct {
4-
Port string
5-
Env string
6-
StaticPath string
4+
Port string
5+
Env string
76

87
DBUsername string
98
DBPassword string

Diff for: main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"golang-simple-web-api/component/appconfig"
55
"golang-simple-web-api/component/appctx"
66
"golang-simple-web-api/middleware"
7+
userhandler "golang-simple-web-api/modules/user/handler"
78
"time"
89

910
"fmt"
@@ -47,6 +48,13 @@ func main() {
4748
})
4849
return
4950
})
51+
52+
userGroup := v1.Group("/users")
53+
{
54+
userGroup.POST("", userhandler.CreateUser(appCtx))
55+
userGroup.GET("", userhandler.ListUser(appCtx))
56+
userGroup.PATCH("/:id", userhandler.UpdateUser(appCtx))
57+
}
5058
}
5159

5260
if err := r.Run(fmt.Sprintf(":%s", cfg.Port)); err != nil {
@@ -63,12 +71,10 @@ func loadConfig() (*appconfig.AppConfig, error) {
6371
return &appconfig.AppConfig{
6472
Port: env["PORT"],
6573
Env: env["GO_ENV"],
66-
StaticPath: env["STATIC_PATH"],
6774
DBUsername: env["DB_USERNAME"],
6875
DBPassword: env["DB_PASSWORD"],
6976
DBHost: env["DB_HOST"],
7077
DBDatabase: env["DB_DATABASE"],
71-
SecretKey: env["SECRET_KEY"],
7278
}, nil
7379
}
7480

Diff for: modules/user/handler/create_user.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package userhandler
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"golang-simple-web-api/component/appctx"
6+
usermodel "golang-simple-web-api/modules/user/model"
7+
userstorage "golang-simple-web-api/modules/user/storage"
8+
"net/http"
9+
)
10+
11+
func CreateUser(appctx appctx.AppContext) gin.HandlerFunc {
12+
return func(c *gin.Context) {
13+
var reqCreateUser usermodel.ReqCreateUser
14+
if err := c.ShouldBindJSON(&reqCreateUser); err != nil {
15+
panic(err)
16+
}
17+
18+
user := usermodel.User{
19+
Name: reqCreateUser.Name,
20+
}
21+
err := userstorage.CreateUser(appctx.GetMainDBConnection(), &user)
22+
if err != nil {
23+
panic(err)
24+
}
25+
c.JSON(http.StatusCreated, user)
26+
}
27+
}

Diff for: modules/user/handler/list_user.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package userhandler
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"golang-simple-web-api/component/appctx"
6+
userstorage "golang-simple-web-api/modules/user/storage"
7+
"net/http"
8+
)
9+
10+
func ListUser(appctx appctx.AppContext) gin.HandlerFunc {
11+
return func(c *gin.Context) {
12+
users, err := userstorage.ListUser(appctx.GetMainDBConnection())
13+
if err != nil {
14+
panic(err)
15+
}
16+
c.JSON(http.StatusOK, users)
17+
}
18+
}

Diff for: modules/user/handler/update_user.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package userhandler
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"golang-simple-web-api/component/appctx"
6+
usermodel "golang-simple-web-api/modules/user/model"
7+
userstorage "golang-simple-web-api/modules/user/storage"
8+
)
9+
10+
func UpdateUser(appCtx appctx.AppContext) gin.HandlerFunc {
11+
return func(c *gin.Context) {
12+
var reqUpdateUser usermodel.ReqUpdateUser
13+
if err := c.ShouldBindJSON(&reqUpdateUser); err != nil {
14+
panic(err)
15+
}
16+
17+
userId := c.Param("id")
18+
19+
userstorage.UpdateUser(appCtx.GetMainDBConnection(), userId, &reqUpdateUser)
20+
}
21+
}

Diff for: modules/user/model/req_create_user.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package usermodel
2+
3+
type ReqCreateUser struct {
4+
Name string `json:"name" gorm:"column:name"`
5+
}

Diff for: modules/user/model/req_update_user.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package usermodel
2+
3+
type ReqUpdateUser struct {
4+
Name string `json:"name" gorm:"column:name"`
5+
}
6+
7+
func (ReqUpdateUser) TableName() string {
8+
return User{}.TableName()
9+
}

Diff for: modules/user/model/user.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package usermodel
2+
3+
type User struct {
4+
ID int `json:"id" gorm:"column:id"`
5+
Name string `json:"name" gorm:"column:name"`
6+
}
7+
8+
func (User) TableName() string {
9+
return "users"
10+
}

Diff for: modules/user/storage/create.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package userstorage
2+
3+
import (
4+
usermodel "golang-simple-web-api/modules/user/model"
5+
"gorm.io/gorm"
6+
)
7+
8+
func CreateUser(db *gorm.DB, data *usermodel.User) error {
9+
if err := db.Create(data).Error; err != nil {
10+
return err
11+
}
12+
13+
return nil
14+
}

Diff for: modules/user/storage/list.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package userstorage
2+
3+
import (
4+
usermodel "golang-simple-web-api/modules/user/model"
5+
"gorm.io/gorm"
6+
)
7+
8+
func ListUser(db *gorm.DB) ([]usermodel.User, error) {
9+
var users []usermodel.User
10+
11+
if err := db.Find(&users).Error; err != nil {
12+
return nil, err
13+
}
14+
15+
return users, nil
16+
}

Diff for: modules/user/storage/update.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package userstorage
2+
3+
import (
4+
usermodel "golang-simple-web-api/modules/user/model"
5+
"gorm.io/gorm"
6+
)
7+
8+
func UpdateUser(db *gorm.DB, id string, data *usermodel.ReqUpdateUser) error {
9+
if err := db.
10+
Where("id = ?", id).
11+
Updates(data).Error; err != nil {
12+
return err
13+
}
14+
15+
return nil
16+
}

0 commit comments

Comments
 (0)