123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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})
- }
|