-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_adapter.go
78 lines (74 loc) · 2.07 KB
/
user_adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package repository
import (
"context"
"database/sql"
"fmt"
q "github.com/core-go/sql"
. "go-service/internal/user/domain"
"reflect"
)
func NewUserAdapter(db *sql.DB) *UserAdapter {
userType := reflect.TypeOf(User{})
keys, _ := q.FindPrimaryKeys(userType)
jsonColumnMap := q.MakeJsonColumnMap(userType)
return &UserAdapter{keys: keys, jsonColumnMap: jsonColumnMap, DB: db}
}
type UserAdapter struct {
keys []string
jsonColumnMap map[string]string
DB *sql.DB
}
func (r *UserAdapter) Load(ctx context.Context, id string) (*User, error) {
var users []User
query := fmt.Sprintf(`
select
id,
username,
email,
phone,
date_of_birth
from users where id = %s limit 1`, q.BuildDollarParam(1))
err := q.Select(ctx, r.DB, &users, query, id)
if err != nil {
return nil, err
}
if len(users) > 0 {
return &users[0], nil
}
return nil, nil
}
func (r *UserAdapter) Create(ctx context.Context, user *User) (int64, error) {
query, args := q.BuildToInsert("users", user, q.BuildDollarParam)
tx := q.GetTx(ctx)
res, err := tx.ExecContext(ctx, query, args...)
return RowsAffected(res, err)
}
func (r *UserAdapter) Update(ctx context.Context, user *User) (int64, error) {
tx := q.GetTx(ctx)
query, args := q.BuildToUpdate("users", user, q.BuildDollarParam)
res, err := tx.ExecContext(ctx, query, args...)
return RowsAffected(res, err)
}
func (r *UserAdapter) Patch(ctx context.Context, user map[string]interface{}) (int64, error) {
colMap := q.JSONToColumns(user, r.jsonColumnMap)
query, args := q.BuildToPatch("users", colMap, r.keys, q.BuildDollarParam)
tx := q.GetTx(ctx)
res, err := tx.ExecContext(ctx, query, args...)
return RowsAffected(res, err)
}
func (r *UserAdapter) Delete(ctx context.Context, id string) (int64, error) {
query := "delete from users where id = ?"
tx := q.GetTx(ctx)
stmt, err := tx.Prepare(query)
if err != nil {
return -1, err
}
res, err := stmt.ExecContext(ctx, id)
return RowsAffected(res, err)
}
func RowsAffected(res sql.Result, err error) (int64, error) {
if err != nil {
return -1, err
}
return res.RowsAffected()
}