first commit

This commit is contained in:
2026-08-08 23:12:37 +02:00
parent ff6ef46cd4
commit e3fd942c47
20 changed files with 494 additions and 271 deletions
+26 -2
View File
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/labstack/echo/v5"
"trankilou.fr/lassistanoque/backend/internal/domain"
"trankilou.fr/lassistanoque/backend/internal/service/auth"
"trankilou.fr/lassistanoque/backend/internal/service/user"
)
@@ -15,7 +16,7 @@ func NewUserGroup(prefix string, e *echo.Group, service *user.Service, middlewar
_ = userHandler
auth := e.Group(prefix, middlewares...)
auth.GET("/me", userHandler.Me)
// auth.GET("/register", userHandler.Register)
auth.PUT("/:id", userHandler.Update)
return auth
}
@@ -27,7 +28,30 @@ func (h UserHandler) Me(c *echo.Context) error {
userID := c.Get(auth.ContextUserIDKey).(string)
user, err := h.userService.GetUser(userID)
if err != nil {
return echo.NewHTTPError(http.StatusForbidden, err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
return c.JSON(http.StatusOK, user)
}
func (h UserHandler) Update(c *echo.Context) error {
userID := c.Get(auth.ContextUserIDKey).(string)
id := c.Param("id")
var updUser domain.User
if err := c.Bind(&updUser); err != nil {
return c.String(http.StatusBadRequest, "bad request")
}
updUser.ID = id
isAdmin := false // TODO : check if caller is admin
if !(isAdmin || userID == id) {
return echo.NewHTTPError(http.StatusForbidden, "unauthorized to update user")
}
user, err := h.userService.UpdateUser(&updUser, isAdmin)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, user)
}