|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/gin-gonic/gin" |
| 6 | + "github.com/google/uuid" |
| 7 | + "github.com/samber/lo" |
| 8 | + "github.com/scarlettmiss/bestPal/application" |
| 9 | + "github.com/scarlettmiss/bestPal/application/domain/user" |
| 10 | + "github.com/scarlettmiss/bestPal/cmd/server/types" |
| 11 | + "github.com/scarlettmiss/bestPal/converters" |
| 12 | + "github.com/scarlettmiss/bestPal/middlewares" |
| 13 | + "github.com/scarlettmiss/bestPal/utils" |
| 14 | + "net/http" |
| 15 | +) |
| 16 | + |
| 17 | +type API struct { |
| 18 | + *gin.Engine |
| 19 | + app *application.Application |
| 20 | +} |
| 21 | + |
| 22 | +func New(application *application.Application) *API { |
| 23 | + api := &API{ |
| 24 | + Engine: gin.Default(), |
| 25 | + app: application, |
| 26 | + } |
| 27 | + |
| 28 | + api.NoRoute(func(ctx *gin.Context) { ctx.Status(http.StatusNotFound) }) |
| 29 | + |
| 30 | + api.POST("/api/auth/register", api.register) |
| 31 | + api.POST("/api/auth/login", api.login) |
| 32 | + |
| 33 | + protected := api.Group("/").Use(middlewares.Auth()) |
| 34 | + protected.PATCH("/api/user", api.updateUser) |
| 35 | + protected.DELETE("/api/user", api.deleteUser) |
| 36 | + protected.GET("/api/user/:id", api.user) |
| 37 | + protected.GET("/api/user", api.user) |
| 38 | + protected.GET("/api/users", api.users) |
| 39 | + |
| 40 | + return api |
| 41 | +} |
| 42 | + |
| 43 | +func (api *API) register(c *gin.Context) { |
| 44 | + var requestBody types.UserCreateRequest |
| 45 | + |
| 46 | + err := c.ShouldBindJSON(&requestBody) |
| 47 | + if err != nil { |
| 48 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + u, err := converters.UserCreateRequestToUser(requestBody) |
| 53 | + if err != nil { |
| 54 | + c.JSON(http.StatusBadRequest, utils.ErrorResponse(err)) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + u, err = api.app.CreateUser(u) |
| 59 | + if err != nil { |
| 60 | + if err == user.ErrMailExists { |
| 61 | + c.JSON(http.StatusBadRequest, utils.ErrorResponse(err)) |
| 62 | + } else { |
| 63 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 64 | + } |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + token, err := api.app.UserToken(u) |
| 69 | + if err != nil { |
| 70 | + c.JSON(http.StatusBadRequest, utils.ErrorResponse(err)) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token}) |
| 75 | + fmt.Println(u) |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +func (api *API) login(c *gin.Context) { |
| 80 | + var requestBody types.LoginRequest |
| 81 | + |
| 82 | + err := c.ShouldBindJSON(&requestBody) |
| 83 | + if err != nil { |
| 84 | + c.Status(http.StatusBadRequest) |
| 85 | + return |
| 86 | + } |
| 87 | + u, err := api.app.Authenticate(requestBody.Email, requestBody.Password) |
| 88 | + if err != nil { |
| 89 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + token, err := api.app.UserToken(u) |
| 94 | + if err != nil { |
| 95 | + c.JSON(http.StatusBadRequest, utils.ErrorResponse(err)) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token}) |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +func (api *API) users(c *gin.Context) { |
| 104 | + users := api.app.Users() |
| 105 | + |
| 106 | + usersResp := lo.MapValues(users, func(u user.User, _ uuid.UUID) types.UserResponse { |
| 107 | + return converters.UserToResponse(u) |
| 108 | + }) |
| 109 | + |
| 110 | + c.JSON(http.StatusOK, usersResp) |
| 111 | +} |
| 112 | + |
| 113 | +func (api *API) user(c *gin.Context) { |
| 114 | + id := c.Param("id") |
| 115 | + |
| 116 | + var ok bool |
| 117 | + if id == "" { |
| 118 | + idParam, _ := c.Get("UserId") |
| 119 | + // Convert UserId to string if it's not already. |
| 120 | + id, ok = idParam.(string) |
| 121 | + if !ok { |
| 122 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"}) |
| 123 | + return |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + //parse |
| 128 | + uId, err := uuid.Parse(id) |
| 129 | + if err != nil { |
| 130 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + u, err := api.app.User(uId) |
| 135 | + if err != nil { |
| 136 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + c.JSON(http.StatusOK, converters.UserToResponse(u)) |
| 141 | +} |
| 142 | + |
| 143 | +func (api *API) deleteUser(c *gin.Context) { |
| 144 | + idParam, _ := c.Get("UserId") |
| 145 | + // Convert UserId to string if it's not already. |
| 146 | + id, ok := idParam.(string) |
| 147 | + if !ok { |
| 148 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"}) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + //parse |
| 153 | + uId, err := uuid.Parse(id) |
| 154 | + if err != nil { |
| 155 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + err = api.app.DeleteUser(uId) |
| 160 | + if err != nil { |
| 161 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + c.JSON(http.StatusOK, gin.H{"message": "user deleted"}) |
| 166 | +} |
| 167 | + |
| 168 | +func (api *API) updateUser(c *gin.Context) { |
| 169 | + id, _ := c.Get("UserId") |
| 170 | + // Convert UserId to string if it's not already. |
| 171 | + idString, ok := id.(string) |
| 172 | + if !ok { |
| 173 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"}) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + uId, err := uuid.Parse(idString) |
| 178 | + if err != nil { |
| 179 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + var requestBody types.UserUpdateRequest |
| 184 | + err = c.ShouldBindJSON(&requestBody) |
| 185 | + if err != nil { |
| 186 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + u, err := api.app.User(uId) |
| 191 | + if err != nil { |
| 192 | + c.JSON(http.StatusNotFound, utils.ErrorResponse(err)) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + u = converters.UserUpdateRequestToUser(requestBody, u) |
| 197 | + |
| 198 | + u, err = api.app.UpdateUser(u) |
| 199 | + if err != nil { |
| 200 | + if err == user.ErrMailExists { |
| 201 | + c.JSON(http.StatusBadRequest, utils.ErrorResponse(err)) |
| 202 | + } else { |
| 203 | + c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err)) |
| 204 | + } |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + c.JSON(http.StatusOK, converters.UserToResponse(u)) |
| 209 | +} |
0 commit comments