-
Notifications
You must be signed in to change notification settings - Fork 13
/
user_mutations.resolvers.go
117 lines (106 loc) · 3.27 KB
/
user_mutations.resolvers.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package resolver
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"fmt"
"go-template/daos"
"go-template/gqlmodels"
"go-template/internal/config"
"go-template/internal/middleware/auth"
"go-template/internal/service"
"go-template/models"
"go-template/pkg/utl/cnvrttogql"
"go-template/pkg/utl/resultwrapper"
"go-template/pkg/utl/throttle"
"strconv"
"time"
null "github.com/volatiletech/null/v8"
)
// CreateUser is the resolver for the createUser field.
func (r *mutationResolver) CreateUser(ctx context.Context, input gqlmodels.UserCreateInput) (*gqlmodels.User, error) {
err := throttle.Check(ctx, 5, 10*time.Second)
if err != nil {
return nil, err
}
roleId, _ := strconv.Atoi(input.RoleID)
active := null.NewBool(false, false)
if input.Active != nil {
active = null.BoolFrom(*input.Active)
}
user := models.User{
Username: null.StringFrom(input.Username),
Password: null.StringFrom(input.Password),
Email: null.StringFrom(input.Email),
FirstName: null.StringFrom(input.FirstName),
LastName: null.StringFrom(input.LastName),
RoleID: null.IntFrom(roleId),
Active: active,
}
// loading configurations
cfg, err := config.Load()
if err != nil {
return nil, fmt.Errorf("error in loading config ")
}
// creating new secure service
sec := service.Secure(cfg)
user.Password = null.StringFrom(sec.Hash(user.Password.String))
newUser, err := daos.CreateUser(user, ctx)
if err != nil {
return nil, resultwrapper.ResolverSQLError(err, "user information")
}
graphUser := cnvrttogql.UserToGraphQlUser(&newUser, 1)
r.Lock()
for _, observer := range r.Observers {
observer <- graphUser
}
r.Unlock()
return graphUser, err
}
// UpdateUser is the resolver for the updateUser field.
func (r *mutationResolver) UpdateUser(ctx context.Context, input *gqlmodels.UserUpdateInput) (*gqlmodels.User, error) {
userID := auth.UserIDFromContext(ctx)
user, _ := daos.FindUserByID(userID, ctx)
var u models.User
if user != nil {
u = *user
} else {
return nil, resultwrapper.ResolverWrapperFromMessage(404, "user not found")
}
if input.FirstName != nil {
u.FirstName = null.StringFromPtr(input.FirstName)
}
if input.LastName != nil {
u.LastName = null.StringFromPtr(input.LastName)
}
if input.Mobile != nil {
u.Mobile = null.StringFromPtr(input.Mobile)
}
if input.Address != nil {
u.Address = null.StringFromPtr(input.Address)
}
_, err := daos.UpdateUser(u, ctx)
if err != nil {
return nil, resultwrapper.ResolverSQLError(err, "new information")
}
graphUser := cnvrttogql.UserToGraphQlUser(&u, 1)
r.Lock()
for _, observer := range r.Observers {
observer <- graphUser
}
r.Unlock()
return graphUser, nil
}
// DeleteUser is the resolver for the deleteUser field.
func (r *mutationResolver) DeleteUser(ctx context.Context) (*gqlmodels.UserDeletePayload, error) {
userID := auth.UserIDFromContext(ctx)
u, err := daos.FindUserByID(userID, ctx)
if err != nil {
return nil, resultwrapper.ResolverSQLError(err, "data")
}
_, err = daos.DeleteUser(*u, ctx)
if err != nil {
return nil, resultwrapper.ResolverSQLError(err, "user")
}
return &gqlmodels.UserDeletePayload{ID: fmt.Sprint(userID)}, nil
}