|
|
@@ -0,0 +1,123 @@
|
|
|
+package handlers
|
|
|
+
|
|
|
+import (
|
|
|
+ "api-dockerization/internal/command"
|
|
|
+ "api-dockerization/internal/service"
|
|
|
+ "errors"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "encoding/json"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/gorilla/mux"
|
|
|
+ "gorm.io/gorm"
|
|
|
+)
|
|
|
+
|
|
|
+type UserHandler struct {
|
|
|
+ commandService *service.UserCommandService
|
|
|
+ queryService *service.UserQueryService
|
|
|
+}
|
|
|
+
|
|
|
+func NewUserHandler(cmdService *service.UserCommandService, qryService *service.UserQueryService) *UserHandler {
|
|
|
+ return &UserHandler{
|
|
|
+ commandService: cmdService,
|
|
|
+ queryService: qryService,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|
|
+ var cmd command.CreateUserCommand
|
|
|
+ if err := json.NewDecoder(r.Body).Decode(&cmd); err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // Llamar a la validación
|
|
|
+ if err := cmd.Validate(); err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := h.commandService.CreateUser(&cmd); err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ w.WriteHeader(http.StatusCreated)
|
|
|
+ // Enviar un mensaje de éxito como respuesta
|
|
|
+ response := map[string]string{"message": "User created"}
|
|
|
+ json.NewEncoder(w).Encode(response)
|
|
|
+}
|
|
|
+
|
|
|
+func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
+ users, err := h.queryService.GetUsers()
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ json.NewEncoder(w).Encode(users)
|
|
|
+}
|
|
|
+
|
|
|
+func (h *UserHandler) GetUserById(w http.ResponseWriter, r *http.Request) {
|
|
|
+ vars := mux.Vars(r) // Obtener las variables de la ruta
|
|
|
+ idParam := vars["id"] // Obtener el ID del usuario
|
|
|
+
|
|
|
+ // Convertir el ID a uint
|
|
|
+ id, err := strconv.ParseUint(idParam, 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Obtener el usuario usando el servicio de consulta
|
|
|
+ user, err := h.queryService.GetUserById(uint(id))
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ http.Error(w, "User not found", http.StatusNotFound)
|
|
|
+ } else {
|
|
|
+ http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Configurar el tipo de contenido y codificar el usuario en JSON
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ if err := json.NewEncoder(w).Encode(user); err != nil {
|
|
|
+ http.Error(w, "Error encoding JSON response", http.StatusInternalServerError)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (h *UserHandler) DeleteUserById(w http.ResponseWriter, r *http.Request) {
|
|
|
+ vars := mux.Vars(r) // Obtener las variables de la ruta
|
|
|
+ idParam := vars["id"] // Obtener el ID del usuario
|
|
|
+
|
|
|
+ id, err := strconv.ParseUint(idParam, 10, 32) // Convertir el ID a uint
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Buscar el usuario en la base de datos
|
|
|
+ user, err := h.queryService.GetUserById(uint(id))
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ http.Error(w, "User not found", http.StatusNotFound)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Eliminar el usuario (soft delete)
|
|
|
+ if err := h.commandService.DeleteUserById(user.ID); err != nil {
|
|
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json")
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
+ json.NewEncoder(w).Encode(map[string]string{
|
|
|
+ "message": "User successfully deleted",
|
|
|
+ })
|
|
|
+}
|