package handlers import ( "github.com/CesarSSH/cqrs-api-go/internal/command" "github.com/CesarSSH/cqrs-api-go/internal/service" "encoding/json" "net/http" ) 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": "Usuario creado"} 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) }