Files
template-vitrine/backend/internal/modules/users/handler.go
T
Xor290 919c807004
ci-api / test (push) Failing after 8m7s
ci-web / test (push) Failing after 5m5s
chore: build
2026-09-20 12:18:33 +02:00

160 lines
4.3 KiB
Go

package users
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type Handler struct {
service *Service
}
func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
type userResponse struct {
ID uuid.UUID `json:"id"`
Username string `json:"username"`
Role string `json:"role"`
IsActive bool `json:"is_active"`
}
func toResponse(u *User) userResponse {
return userResponse{ID: u.ID, Username: u.Username, Role: u.Role, IsActive: u.IsActive}
}
func (h *Handler) List(c *gin.Context) {
list, err := h.service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list users"})
return
}
resp := make([]userResponse, 0, len(list))
for _, u := range list {
resp = append(resp, toResponse(u))
}
c.JSON(http.StatusOK, gin.H{"users": resp})
}
type createUserRequest struct {
Username string `json:"username" binding:"required,min=3,max=50"`
Password string `json:"password" binding:"required,min=12"`
Role string `json:"role" binding:"required,oneof=admin customer"`
}
func (h *Handler) Create(c *gin.Context) {
var req createUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
return
}
user, err := h.service.Create(c.Request.Context(), req.Username, req.Password, req.Role)
if err != nil {
if errors.Is(err, ErrAdminLimitReached) {
c.JSON(http.StatusConflict, gin.H{"error": "admin limit reached"})
}
if errors.Is(err, ErrUsernameTaken) {
c.JSON(http.StatusConflict, gin.H{"error": "username already in use"})
return
}
if errors.Is(err, ErrCustomerAccountsDisabled) {
c.JSON(http.StatusForbidden, gin.H{"error": "customer accounts are disabled: enable customer login or registration first"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create user"})
return
}
c.JSON(http.StatusCreated, toResponse(user))
}
func (h *Handler) Get(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
user, err := h.service.FindByID(c.Request.Context(), id)
if err != nil {
if errors.Is(err, ErrNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get user"})
return
}
c.JSON(http.StatusOK, toResponse(user))
}
func (h *Handler) GetRole(c *gin.Context) {
role := c.Param("role")
if role == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role"})
return
}
count, err := h.service.CountByRole(c.Request.Context(), role)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "failed to count users",
})
return
}
c.JSON(http.StatusOK, count)
}
type updateUserRequest struct {
Username string `json:"username" binding:"required,min=3,max=50"`
IsActive bool `json:"is_active"`
}
func (h *Handler) Update(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
var req updateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
return
}
user, err := h.service.UpdateProfile(c.Request.Context(), id, req.Username, req.IsActive)
if err != nil {
if errors.Is(err, ErrNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
return
}
if errors.Is(err, ErrUsernameTaken) {
c.JSON(http.StatusConflict, gin.H{"error": "username already in use"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update user"})
return
}
c.JSON(http.StatusOK, toResponse(user))
}
func (h *Handler) Delete(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
if err := h.service.Delete(c.Request.Context(), id); err != nil {
if errors.Is(err, ErrNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete user"})
return
}
c.Status(http.StatusNoContent)
}