profile.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package handlers
  2. import (
  3. "net/http"
  4. "git.linuxforward.com/byom/byom-core/common"
  5. "git.linuxforward.com/byom/byom-core/errors"
  6. "git.linuxforward.com/byom/byom-core/logger"
  7. "git.linuxforward.com/byom/byom-core/middleware"
  8. "git.linuxforward.com/byom/byom-core/store"
  9. "github.com/gin-gonic/gin"
  10. "github.com/google/uuid"
  11. "github.com/sirupsen/logrus"
  12. )
  13. type ProfileHandler struct {
  14. logger *logrus.Entry
  15. store *store.DataStore
  16. }
  17. func NewProfileHandler(db *store.DataStore) *ProfileHandler {
  18. return &ProfileHandler{
  19. logger: logger.NewLogger("profile-handler"),
  20. store: db,
  21. }
  22. }
  23. // Profile Management
  24. func (h *ProfileHandler) List(c *gin.Context) {
  25. profiles, err := h.store.GetProfiles(c)
  26. if err != nil {
  27. h.logger.WithError(err).Error("failed to get profiles")
  28. _ = c.Error(errors.NewInternalError("Failed to get profiles", err))
  29. return
  30. }
  31. c.JSON(http.StatusOK, common.ProfileListResponse{Profiles: profiles})
  32. }
  33. func (h *ProfileHandler) Create(c *gin.Context) {
  34. req, ok := middleware.ValidateRequest[common.ProfileCreateRequest](c)
  35. if !ok {
  36. return
  37. }
  38. profile := &common.Profile{
  39. ID: uuid.New(),
  40. Name: req.Name,
  41. WorkspaceID: req.WorkspaceID,
  42. }
  43. if err := h.store.CreateProfile(c, profile); err != nil {
  44. h.logger.WithError(err).Error("failed to create profile")
  45. _ = c.Error(errors.NewInternalError("Failed to create profile", err))
  46. return
  47. }
  48. c.JSON(http.StatusCreated, common.ProfileResponse{Profile: *profile})
  49. }
  50. func (h *ProfileHandler) Get(c *gin.Context) {
  51. id := c.Param("id")
  52. if id == "" {
  53. _ = c.Error(errors.NewValidationError("Profile ID is required", "id"))
  54. return
  55. }
  56. // Validate UUID format
  57. if _, err := uuid.Parse(id); err != nil {
  58. _ = c.Error(errors.NewValidationError("Profile ID is required", "id"))
  59. return
  60. }
  61. profile, err := h.store.GetProfile(c, id)
  62. if err != nil {
  63. if errors.IsNotFound(err) {
  64. _ = c.Error(errors.NewNotFoundError("Profile", id))
  65. return
  66. }
  67. h.logger.WithError(err).Error("failed to get profile")
  68. _ = c.Error(errors.NewInternalError("Failed to get profile", err))
  69. return
  70. }
  71. c.JSON(http.StatusOK, common.ProfileResponse{Profile: *profile})
  72. }
  73. func (h *ProfileHandler) Update(c *gin.Context) {
  74. id := c.Param("id")
  75. if id == "" {
  76. _ = c.Error(errors.NewValidationError("Profile ID is required", "id"))
  77. return
  78. }
  79. req, ok := middleware.ValidateRequest[common.ProfileUpdateRequest](c)
  80. if !ok {
  81. return
  82. }
  83. profile, err := h.store.GetProfile(c, id)
  84. if err != nil {
  85. if errors.IsNotFound(err) {
  86. _ = c.Error(errors.NewNotFoundError("Profile", id))
  87. return
  88. }
  89. h.logger.WithError(err).Error("failed to get profile")
  90. _ = c.Error(errors.NewInternalError("Failed to get profile", err))
  91. return
  92. }
  93. profile.Name = req.Name
  94. if err := h.store.UpdateProfile(c, profile); err != nil {
  95. h.logger.WithError(err).Error("failed to update profile")
  96. _ = c.Error(errors.NewInternalError("Failed to update profile", err))
  97. return
  98. }
  99. c.JSON(http.StatusOK, common.ProfileResponse{Profile: *profile})
  100. }
  101. func (h *ProfileHandler) Delete(c *gin.Context) {
  102. id := c.Param("id")
  103. if id == "" {
  104. _ = c.Error(errors.NewValidationError("Profile ID is required", "id"))
  105. return
  106. }
  107. if err := h.store.DeleteProfile(c, id); err != nil {
  108. if errors.IsNotFound(err) {
  109. _ = c.Error(errors.NewNotFoundError("Profile", id))
  110. return
  111. }
  112. h.logger.WithError(err).Error("failed to delete profile")
  113. _ = c.Error(errors.NewInternalError("Failed to delete profile", err))
  114. return
  115. }
  116. c.JSON(http.StatusOK, common.MessageResponse{Message: "Profile deleted successfully"})
  117. }
  118. func (h *ProfileHandler) ListByWorkspace(c *gin.Context) {
  119. workspaceID := c.Param("id")
  120. if workspaceID == "" {
  121. _ = c.Error(errors.NewValidationError("Workspace ID is required", "id"))
  122. return
  123. }
  124. // Validate workspace exists
  125. if _, err := h.store.GetWorkspace(c, workspaceID); err != nil {
  126. if errors.IsNotFound(err) {
  127. _ = c.Error(errors.NewNotFoundError("Workspace", workspaceID))
  128. return
  129. }
  130. _ = c.Error(errors.NewValidationError("Invalid workspace ID", "id"))
  131. return
  132. }
  133. profiles, err := h.store.GetProfilesByWorkspaceID(c, workspaceID)
  134. if err != nil {
  135. h.logger.WithError(err).Error("failed to get profiles by workspace")
  136. _ = c.Error(errors.NewInternalError("Failed to get profiles", err))
  137. return
  138. }
  139. c.JSON(http.StatusOK, common.ProfileListResponse{Profiles: profiles})
  140. }