forked from knadh/listmonk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
138 lines (115 loc) · 3.01 KB
/
users.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
package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/asaskevich/govalidator"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo"
)
// handleGetUsers handles retrieval of users.
func handleGetUsers(c echo.Context) error {
var (
app = c.Get("app").(*App)
out []models.User
id, _ = strconv.Atoi(c.Param("id"))
single = false
)
// Fetch one list.
if id > 0 {
single = true
}
err := app.Queries.GetUsers.Select(&out, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error fetching users: %s", pqErrMsg(err)))
} else if single && len(out) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "User not found.")
} else if len(out) == 0 {
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
}
if single {
return c.JSON(http.StatusOK, okResp{out[0]})
}
return c.JSON(http.StatusOK, okResp{out})
}
// handleCreateUser handles user creation.
func handleCreateUser(c echo.Context) error {
var (
app = c.Get("app").(*App)
o = models.User{}
)
if err := c.Bind(&o); err != nil {
return err
}
if !govalidator.IsEmail(o.Email) {
return errors.New("invalid `email`")
}
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
return errors.New("invalid length for `name`")
}
// Insert and read ID.
var newID int
if err := app.Queries.CreateUser.Get(&newID,
o.Email,
o.Name,
o.Password,
o.Type,
o.Status); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error creating user: %v", pqErrMsg(err)))
}
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
return c.JSON(http.StatusOK, handleGetLists(c))
}
// handleUpdateUser handles user modification.
func handleUpdateUser(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.Atoi(c.Param("id"))
)
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
} else if id == 1 {
return echo.NewHTTPError(http.StatusBadRequest,
"The primordial super admin cannot be deleted.")
}
var o models.User
if err := c.Bind(&o); err != nil {
return err
}
if !govalidator.IsEmail(o.Email) {
return errors.New("invalid `email`")
}
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
return errors.New("invalid length for `name`")
}
// TODO: PASSWORD HASHING.
res, err := app.Queries.UpdateUser.Exec(o.ID,
o.Email,
o.Name,
o.Password,
o.Type,
o.Status)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error updating user: %s", pqErrMsg(err)))
}
if n, _ := res.RowsAffected(); n == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "User not found.")
}
return handleGetUsers(c)
}
// handleDeleteUser handles user deletion.
func handleDeleteUser(c echo.Context) error {
var (
id, _ = strconv.Atoi(c.Param("id"))
)
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
return c.JSON(http.StatusOK, okResp{true})
}