forked from shamanec/GADS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
79 lines (64 loc) · 1.55 KB
/
routes.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
package router
import (
"GADS/models"
"GADS/util"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)
func HealthCheck(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "ok"})
}
func AddUser(c *gin.Context) {
var user models.User
body, err := io.ReadAll(c.Request.Body)
if err != nil {
InternalServerError(c, fmt.Sprintf("%s", err))
return
}
err = json.Unmarshal(body, &user)
if err != nil {
BadRequest(c, fmt.Sprintf("%s", err))
return
}
if user == (models.User{}) {
BadRequest(c, "Empty or invalid body")
return
}
if user.Email == "" || user.Password == "" || user.Role == "" {
BadRequest(c, "Email, password and role are mandatory")
return
}
if user.Role != "admin" && user.Role != "user" {
BadRequest(c, "Invalid role - `admin` and `user` are the accepted values")
return
}
if user.Username == "" {
user.Username = "New user"
}
dbUser, err := util.GetUserFromDB(user.Email)
if err != nil && err != mongo.ErrNoDocuments {
InternalServerError(c, "Failed checking for user in db - "+err.Error())
return
} else {
fmt.Println("User does not exist, creating")
// ADD LOGGER HERE
}
if dbUser != (models.User{}) {
BadRequest(c, "User already exists")
return
}
err = util.AddOrUpdateUser(user)
if err != nil {
InternalServerError(c, fmt.Sprintf("Failed adding/updating user - %s", err))
return
}
OK(c, "Successfully added user")
}
func GetProviders(c *gin.Context) {
providers := util.GetProvidersFromDB()
c.JSON(http.StatusOK, providers)
}