-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
56 lines (46 loc) · 1.29 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
package handler
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/l-orlov/slim-fairy/bot/internal/model"
"github.com/l-orlov/slim-fairy/bot/internal/store"
"github.com/pkg/errors"
)
func (h *Handler) registerUser(c *gin.Context) {
userToReg := &model.UserToRegister{}
err := c.BindJSON(userToReg)
if err != nil {
log.Printf("c.BindJSON: %v", err)
h.writeErrorResponse(c, http.StatusBadRequest, "invalid request")
return
}
user, err := h.svc.RegisterUser(c, userToReg)
if err != nil {
log.Printf("h.svc.RegisterUser: %v", err)
h.writeErrorResponse(c, http.StatusInternalServerError, "failed to create user")
return
}
c.JSON(http.StatusOK, user)
}
func (h *Handler) getUserByID(c *gin.Context) {
idStr := c.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
log.Printf("uuid.Parse: %v", err)
h.writeErrorResponse(c, http.StatusBadRequest, "invalid id format")
return
}
user, err := h.storage.GetUserByID(c, id)
if err != nil {
log.Printf("h.storage.GetUserByID: %v", err)
if errors.Is(err, store.ErrNotFound) {
h.writeErrorResponse(c, http.StatusNotFound, "user not found")
return
}
h.writeErrorResponse(c, http.StatusInternalServerError, "failed to get user")
return
}
c.IndentedJSON(http.StatusOK, user)
}