-
Notifications
You must be signed in to change notification settings - Fork 0
/
linuxUsers.go
126 lines (116 loc) · 3.62 KB
/
linuxUsers.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
package www
import (
"encoding/json"
"net/http"
"strconv"
"github.com/csg33k/golug/dbmodels"
"github.com/csg33k/golug/services"
"github.com/go-chi/chi"
)
// ListUsers
// @Description List all users
// @Produce json
// @Success 200 {object} dbmodels.LinuxUser
// @Router /api/v1/users/list [get]
func ListUsers(w http.ResponseWriter, r *http.Request) {
srv := services.GetServices()
users, err := srv.ListUsers()
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
//Return empty list
if users == nil {
users = []dbmodels.LinuxUser{}
}
respondwithJSON(w, http.StatusOK, users)
}
// CreateUser
// @Description Create a New User
// @Produce json
// @Param request body dbmodels.LinuxUser true "query params"
// @Success 200 {object} dbmodels.LinuxUser
// @Router /api/v1/users/ [post]
func CreateUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
var newUser dbmodels.LinuxUser
err := json.NewDecoder(r.Body).Decode(&newUser)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Invalid Request received")
}
srv := services.GetServices()
createdUser, err := srv.CreateUser(newUser)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
respondwithJSON(w, http.StatusOK, createdUser)
}
// Update
// @Description Create a New User
// @Produce json
// @Param id path int true "user id"
// @Param request body dbmodels.LinuxUser true "query params"
// @Success 200 {object} dbmodels.LinuxUser
// @Router /api/v1/users/{id} [put]
func UpdateUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
var newUser dbmodels.LinuxUser
err := json.NewDecoder(r.Body).Decode(&newUser)
userId := extractPathUserId(w, r)
if err != nil || newUser.LinuxUserID != userId {
respondWithError(w, http.StatusInternalServerError, "Invalid Request received")
return
}
srv := services.GetServices()
updatedUser, err := srv.UpdateUser(newUser)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondwithJSON(w, http.StatusOK, updatedUser)
}
// DeleteUser
// @Description Retrieves a User by ID
// @Produce json
// @Param id path int true "user id"
// @Success 200 {object} dbmodels.LinuxUser
// @Router /api/v1/users/{id} [delete]
func DeleteUser(w http.ResponseWriter, r *http.Request) {
rawUserId := chi.URLParam(r, "id") // 👈 getting path param
svc := services.GetServices()
userId, err := strconv.ParseInt(rawUserId, 10, 64)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Invalid userID")
return
}
err = svc.DeleteUser(userId)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "could not delete user")
return
}
respondwithJSON(w, http.StatusOK, map[string]string{"message": "User Successfully deleted", "error": ""})
}
func extractPathUserId(w http.ResponseWriter, r *http.Request) int64 {
rawUserId := chi.URLParam(r, "id") // 👈 getting path param
userId, err := strconv.ParseInt(rawUserId, 10, 64)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Invalid userID")
}
return userId
}
// GetUser
// @Description Retrieves a User by ID
// @Produce json
// @Param id path int true "user id"
// @Success 200 {object} dbmodels.LinuxUser
// @Router /api/v1/users/{id} [get]
func GetUser(w http.ResponseWriter, r *http.Request) {
userId := extractPathUserId(w, r)
svc := services.GetServices()
user, err := svc.GetUser(userId)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Invalid userID")
return
}
respondwithJSON(w, http.StatusOK, user)
}