-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
139 lines (131 loc) · 4.14 KB
/
handlers.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"fmt"
"net/http"
"github.com/eensymachines-in/errx/httperr"
"github.com/eensymachines-in/webapi-userauth/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
func HndlAUser(c *gin.Context) {
// --------- mongo connections
val, _ := c.Get("mongo-client")
mongoClient := val.(*mongo.Client)
val, _ = c.Get("mongo-database")
db := val.(*mongo.Database)
uc := models.UsersCollection{DbColl: db.Collection("users")}
defer mongoClient.Disconnect(context.Background())
usrId := c.Param("id")
if usrId == "" {
httperr.HttpErrOrOkDispatch(c, httperr.ErrContxParamMissing(fmt.Errorf("missing user id for which request")), log.WithFields(log.Fields{
"stack": "HndlAUser",
}))
return
}
usr := models.User{} // onto which we take the payload on
if err := c.ShouldBind(&usr); err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrContxParamMissing(fmt.Errorf("missing user id for which request")), log.WithFields(log.Fields{
"stack": "HndlAUser",
}))
return
}
if err := uc.FindUser(usrId, &usr); err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlAUser/GET",
}))
return
}
if c.Request.Method == "GET" {
// trying to get the single user i
c.AbortWithStatusJSON(http.StatusOK, usr)
} else if c.Request.Method == "DELETE" {
if err := uc.DeleteUser(usr.Id.Hex()); err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlAUser/DELETE",
}))
return
}
} else if c.Request.Method == "PATCH" {
/* Incase the default /empty value fo the user, they would NOT be patched,
validation thoughb happens for non-zero values */
if err := uc.EditUser(string(usr.Email), string(usr.Name), usr.Auth, usr.TelegID); err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlAUser/PATCH",
}))
return
}
}
}
// HndlLstUsers : handles list of users, can post a new user
// Can login when POST, action=auth
// Can authorize when GET action=auth
// for all other purposes it will ne method not allowed
func HndlLstUsers(c *gin.Context) {
// --------- request binding
// --------- mongo connections
val, _ := c.Get("mongo-client")
mongoClient := val.(*mongo.Client)
val, _ = c.Get("mongo-database")
db := val.(*mongo.Database)
uc := models.UsersCollection{DbColl: db.Collection("users")}
defer mongoClient.Disconnect(context.Background())
action := c.Query("action")
if c.Request.Method == "POST" {
usr := models.User{}
err := httperr.ErrBinding(c.ShouldBind(&usr))
if err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlUserAuth",
}))
return
}
if action == "auth" {
err := uc.Authenticate(&usr)
if err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlUserAuth",
}))
return
}
// time to send back the token
c.AbortWithStatusJSON(http.StatusOK, usr)
} else if action == "create" {
usr.Role = models.EndUser // when creating new user the role will always be EndUser
err = uc.NewUser(&usr)
if err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlUsers",
}))
return
}
c.AbortWithStatusJSON(http.StatusOK, &usr)
} else {
c.AbortWithStatus(http.StatusMethodNotAllowed) // on all other cases method is not allowed.
}
} else if c.Request.Method == "GET" {
if action == "auth" {
tok := c.Request.Header.Get("Authorization")
if tok == "" {
httperr.HttpErrOrOkDispatch(c, httperr.ErrForbidden(fmt.Errorf("empty token cannot request authorization")), log.WithFields(log.Fields{
"stack": "HndlUserAuth",
}))
return
} else {
err := uc.Authorize(tok) // user fields would be empty per say since its only the token you are authorizing
if err != nil {
httperr.HttpErrOrOkDispatch(c, err, log.WithFields(log.Fields{
"stack": "HndlUserAuth",
}))
return
}
c.AbortWithStatus(http.StatusOK)
}
} else {
c.AbortWithStatus(http.StatusMethodNotAllowed)
}
} else {
c.AbortWithStatus(http.StatusMethodNotAllowed)
}
}