profile.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package handlers
  2. import (
  3. "git.linuxforward.com/byom/byom-core/common"
  4. "git.linuxforward.com/byom/byom-core/store"
  5. "github.com/gin-gonic/gin"
  6. "github.com/google/uuid"
  7. "github.com/sirupsen/logrus"
  8. )
  9. type ProfileHandler struct {
  10. logger *logrus.Entry
  11. store *store.DataStore
  12. }
  13. func NewProfileHandler(db *store.DataStore) *ProfileHandler {
  14. logger := logrus.WithField("core", "profile-handler")
  15. return &ProfileHandler{
  16. logger: logger,
  17. store: db,
  18. }
  19. }
  20. func (h *ProfileHandler) GetProfiles(c *gin.Context) {
  21. profiles, err := h.store.GetProfiles(c)
  22. if err != nil {
  23. c.JSON(500, gin.H{"error": "Failed to get profiles"})
  24. return
  25. }
  26. c.JSON(200, profiles)
  27. }
  28. func (h *ProfileHandler) CreateProfile(c *gin.Context) {
  29. var req common.CreateProfileRequest
  30. if err := c.ShouldBindJSON(&req); err != nil {
  31. c.JSON(400, gin.H{"error": "Invalid request"})
  32. return
  33. }
  34. h.logger.Info("Creating profile")
  35. h.logger.Info(req)
  36. profile := &common.Profile{
  37. ID: uuid.New(),
  38. Name: req.Name,
  39. WorkspaceID: uuid.MustParse(req.WorkspaceID),
  40. }
  41. if err := h.store.CreateProfile(c, profile); err != nil {
  42. c.JSON(500, gin.H{"error": "Failed to create profile"})
  43. return
  44. }
  45. c.JSON(201, profile)
  46. }
  47. func (h *ProfileHandler) GetProfile(c *gin.Context) {
  48. id := c.Param("id")
  49. if id == "" {
  50. c.JSON(400, gin.H{"error": "profile id is required"})
  51. return
  52. }
  53. profile, err := h.store.GetProfile(c, id)
  54. if err != nil {
  55. c.JSON(500, gin.H{"error": "Failed to get profile"})
  56. return
  57. }
  58. c.JSON(200, profile)
  59. }
  60. func (h *ProfileHandler) UpdateProfile(c *gin.Context) {
  61. var req common.UpdateProfileRequest
  62. if err := c.ShouldBindJSON(&req); err != nil {
  63. c.JSON(400, gin.H{"error": "Invalid request"})
  64. return
  65. }
  66. id := c.Param("id")
  67. if id == "" {
  68. c.JSON(400, gin.H{"error": "profile id is required"})
  69. return
  70. }
  71. profile, err := h.store.GetProfile(c, id)
  72. if err != nil {
  73. c.JSON(500, gin.H{"error": "Failed to get profile"})
  74. return
  75. }
  76. profile.Name = req.Name
  77. if err := h.store.UpdateProfile(c, profile); err != nil {
  78. c.JSON(500, gin.H{"error": "Failed to update profile"})
  79. return
  80. }
  81. c.JSON(200, profile)
  82. }
  83. func (h *ProfileHandler) DeleteProfile(c *gin.Context) {
  84. id := c.Param("id")
  85. if id == "" {
  86. c.JSON(400, gin.H{"error": "profile id is required"})
  87. return
  88. }
  89. if err := h.store.DeleteProfile(c, id); err != nil {
  90. c.JSON(500, gin.H{"error": "Failed to delete profile"})
  91. return
  92. }
  93. c.JSON(200, gin.H{"message": "Profile deleted"})
  94. }
  95. func (h *ProfileHandler) GetProfilesByWorkspace(c *gin.Context) {
  96. wId := c.Query("workspaceID")
  97. if wId == "" {
  98. c.JSON(400, gin.H{"error": "workspaceID is required"})
  99. return
  100. }
  101. profiles, err := h.store.GetProfilesByWorkspaceID(c, wId)
  102. if err != nil {
  103. c.JSON(500, gin.H{"error": "Failed to get profiles"})
  104. return
  105. }
  106. c.JSON(200, profiles)
  107. }