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

Commit 1ef6266

Browse files
committed
PATCH example with gorm
1 parent afae422 commit 1ef6266

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

‎db/gorm/crud.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/jinzhu/copier"
78
"gorm.io/gorm"
89

910
"godb/db"
@@ -29,7 +30,7 @@ func (r *repo) Create(ctx context.Context, u *db.UserRequest, hash string) (*Use
2930
FavouriteColour: u.FavouriteColour,
3031
}
3132

32-
err := r.db.Debug().WithContext(ctx).Create(user).Error
33+
err := r.db.WithContext(ctx).Create(user).Error
3334
if err != nil {
3435
return nil, err
3536
}
@@ -47,7 +48,7 @@ func (r *repo) List(ctx context.Context, f *db.Filter) ([]*User, error) {
4748
if f.Base.Page > 1 {
4849
return r.ListFilterPagination(ctx, f)
4950
}
50-
if len(f.LastName) > 0 {
51+
if len(f.LastNames) > 0 {
5152
return r.ListFilterWhereIn(ctx, f)
5253
}
5354

@@ -98,13 +99,32 @@ func (r *repo) Update(ctx context.Context, userID int64, req *db.UserUpdateReque
9899
return r.Get(ctx, userID)
99100
}
100101

102+
func (r *repo) Patch(ctx context.Context, userID int64, req *db.UserPatchRequest) (*User, error) {
103+
var u User
104+
if err := copier.Copy(&u, req); err != nil {
105+
return nil, err
106+
}
107+
108+
err := r.db.Debug().
109+
WithContext(ctx).
110+
Model(&u).
111+
Where("id", userID). // order is important. Cannot be after Updates()
112+
Updates(&u).
113+
Error
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
return r.Get(ctx, userID)
119+
}
120+
101121
func (r *repo) Delete(ctx context.Context, userID int64) error {
102122
return r.db.WithContext(ctx).Delete(&User{}, userID).Error
103123
}
104124

105125
func (r *repo) ListFilterWhereIn(ctx context.Context, f *db.Filter) (users []*User, err error) {
106126
err = r.db.WithContext(ctx).
107-
Where("last_name IN ?", f.LastName).
127+
Where("last_name IN ?", f.LastNames).
108128
Find(&users).
109129
Error
110130
if err != nil {

‎db/gorm/handle.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Register(r *chi.Mux, db *gorm.DB) {
2929
router.Get("/m2m", h.ListM2M)
3030
router.Get("/{userID}", h.Get)
3131
router.Put("/{userID}", h.Update)
32+
router.Patch("/{userID}", h.Patch)
3233
router.Delete("/{userID}", h.Delete)
3334
})
3435

@@ -117,6 +118,28 @@ func (h *handler) Update(w http.ResponseWriter, r *http.Request) {
117118
respond.Json(w, http.StatusOK, updated)
118119
}
119120

121+
func (h *handler) Patch(w http.ResponseWriter, r *http.Request) {
122+
userID, err := param.Int64(r, "userID")
123+
if err != nil {
124+
respond.Error(w, http.StatusBadRequest, param.ErrParam)
125+
return
126+
}
127+
var req db.UserPatchRequest
128+
err = json.NewDecoder(r.Body).Decode(&req)
129+
if err != nil {
130+
respond.Error(w, http.StatusInternalServerError, err)
131+
return
132+
}
133+
134+
updated, err := h.db.Patch(r.Context(), userID, &req)
135+
if err != nil {
136+
respond.Error(w, http.StatusInternalServerError, err)
137+
return
138+
}
139+
140+
respond.Json(w, http.StatusOK, updated)
141+
}
142+
120143
func (h *handler) Delete(w http.ResponseWriter, r *http.Request) {
121144
userID, err := param.Int64(r, "userID")
122145
if err != nil {

‎db/gorm/model.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package gorm
22

33
import (
4+
"godb/db"
45
"log"
56

67
"gorm.io/driver/mysql"
78
"gorm.io/driver/postgres"
89
"gorm.io/gorm"
910

1011
"godb/config"
11-
"godb/db/sqlx"
1212
)
1313

1414
type User struct {
@@ -53,13 +53,13 @@ func New(c config.Database) *gorm.DB {
5353

5454
switch c.Type {
5555
case "postgres", "postgresql", "psql", "pgsql", "pgx":
56-
db, err := gorm.Open(postgres.Open(sqlx.Dsn(c)), &gorm.Config{})
56+
db, err := gorm.Open(postgres.Open(db.Dsn(c)), &gorm.Config{})
5757
if err != nil {
5858
log.Panic(err)
5959
}
6060
driver = db
6161
case "mysql", "mariadb":
62-
db, err := gorm.Open(mysql.Open(sqlx.Dsn(c)), &gorm.Config{})
62+
db, err := gorm.Open(mysql.Open(db.Dsn(c)), &gorm.Config{})
6363
if err != nil {
6464
log.Panic(err)
6565
}

‎example/rest.http

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ POST http://localhost:3080/api/sqlx/user
77
Content-Type: application/json
88

99
{
10-
"first_name": "Jake34",
10+
"first_name": "Jake43",
1111
"last_name": "Doe",
12-
"email": "jake35@example.com",
12+
"email": "jake43@example.com",
1313
"password": "password"
1414
}
1515

@@ -27,7 +27,7 @@ Accept: application/json
2727

2828

2929
### Update PUT request with json body
30-
PUT http://localhost:3080/api/sqlx/user/13
30+
PUT http://localhost:3080/api/sqlx/user/1
3131
Content-Type: application/json
3232

3333
{
@@ -75,6 +75,17 @@ Accept: application/json
7575
GET http://localhost:3080/api/sqlx/user/m2m
7676
Accept: application/json
7777

78+
79+
### Create Transaction
80+
PUT http://localhost:3080/api/sqlx/user/1?transaction=true
81+
Content-Type: application/json
82+
83+
{
84+
"first_name": "Jake36",
85+
"last_name": "Doe",
86+
"email": "jake36@example.com",
87+
"password": "password"
88+
}
7889
################################################################################
7990
# sqlc
8091
################################################################################
@@ -267,6 +278,15 @@ Content-Type: application/json
267278
"favourite_colour": "blue"
268279
}
269280

281+
### Update PATCH request with json body
282+
PATCH http://localhost:3080/api/gorm/user/1
283+
Content-Type: application/json
284+
285+
{
286+
"first_name": "John PATCHED",
287+
"favourite_colour": "green"
288+
}
289+
270290
### Delete DELETE request with json body
271291
DELETE http://localhost:3080/api/gorm/user/23
272292
Content-Type: application/json

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /