package handlers import ( "net/http" "git.linuxforward.com/byom/byom-core/common" "git.linuxforward.com/byom/byom-core/errors" "git.linuxforward.com/byom/byom-core/logger" "git.linuxforward.com/byom/byom-core/middleware" "git.linuxforward.com/byom/byom-core/store" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/sirupsen/logrus" ) type ProfileHandler struct { logger *logrus.Entry store *store.DataStore } func NewProfileHandler(db *store.DataStore) *ProfileHandler { return &ProfileHandler{ logger: logger.NewLogger("profile-handler"), store: db, } } // Profile Management func (h *ProfileHandler) List(c *gin.Context) { profiles, err := h.store.GetProfiles(c) if err != nil { h.logger.WithError(err).Error("failed to get profiles") _ = c.Error(errors.NewInternalError("Failed to get profiles", err)) return } c.JSON(http.StatusOK, common.ProfileListResponse{Profiles: profiles}) } func (h *ProfileHandler) Create(c *gin.Context) { req, ok := middleware.ValidateRequest[common.ProfileCreateRequest](c) if !ok { return } profile := &common.Profile{ ID: uuid.New(), Name: req.Name, WorkspaceID: req.WorkspaceID, } if err := h.store.CreateProfile(c, profile); err != nil { h.logger.WithError(err).Error("failed to create profile") _ = c.Error(errors.NewInternalError("Failed to create profile", err)) return } c.JSON(http.StatusCreated, common.ProfileResponse{Profile: *profile}) } func (h *ProfileHandler) Get(c *gin.Context) { id := c.Param("id") if id == "" { _ = c.Error(errors.NewValidationError("Profile ID is required", "id")) return } // Validate UUID format if _, err := uuid.Parse(id); err != nil { _ = c.Error(errors.NewValidationError("Profile ID is required", "id")) return } profile, err := h.store.GetProfile(c, id) if err != nil { if errors.IsNotFound(err) { _ = c.Error(errors.NewNotFoundError("Profile", id)) return } h.logger.WithError(err).Error("failed to get profile") _ = c.Error(errors.NewInternalError("Failed to get profile", err)) return } c.JSON(http.StatusOK, common.ProfileResponse{Profile: *profile}) } func (h *ProfileHandler) Update(c *gin.Context) { id := c.Param("id") if id == "" { _ = c.Error(errors.NewValidationError("Profile ID is required", "id")) return } req, ok := middleware.ValidateRequest[common.ProfileUpdateRequest](c) if !ok { return } profile, err := h.store.GetProfile(c, id) if err != nil { if errors.IsNotFound(err) { _ = c.Error(errors.NewNotFoundError("Profile", id)) return } h.logger.WithError(err).Error("failed to get profile") _ = c.Error(errors.NewInternalError("Failed to get profile", err)) return } profile.Name = req.Name if err := h.store.UpdateProfile(c, profile); err != nil { h.logger.WithError(err).Error("failed to update profile") _ = c.Error(errors.NewInternalError("Failed to update profile", err)) return } c.JSON(http.StatusOK, common.ProfileResponse{Profile: *profile}) } func (h *ProfileHandler) Delete(c *gin.Context) { id := c.Param("id") if id == "" { _ = c.Error(errors.NewValidationError("Profile ID is required", "id")) return } if err := h.store.DeleteProfile(c, id); err != nil { if errors.IsNotFound(err) { _ = c.Error(errors.NewNotFoundError("Profile", id)) return } h.logger.WithError(err).Error("failed to delete profile") _ = c.Error(errors.NewInternalError("Failed to delete profile", err)) return } c.JSON(http.StatusOK, common.MessageResponse{Message: "Profile deleted successfully"}) } func (h *ProfileHandler) ListByWorkspace(c *gin.Context) { workspaceID := c.Param("id") if workspaceID == "" { _ = c.Error(errors.NewValidationError("Workspace ID is required", "id")) return } // Validate workspace exists if _, err := h.store.GetWorkspace(c, workspaceID); err != nil { if errors.IsNotFound(err) { _ = c.Error(errors.NewNotFoundError("Workspace", workspaceID)) return } _ = c.Error(errors.NewValidationError("Invalid workspace ID", "id")) return } profiles, err := h.store.GetProfilesByWorkspaceID(c, workspaceID) if err != nil { h.logger.WithError(err).Error("failed to get profiles by workspace") _ = c.Error(errors.NewInternalError("Failed to get profiles", err)) return } c.JSON(http.StatusOK, common.ProfileListResponse{Profiles: profiles}) }