123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package handlers
- import (
- "git.linuxforward.com/byom/byom-core/common"
- "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 {
- logger := logrus.WithField("core", "profile-handler")
- return &ProfileHandler{
- logger: logger,
- store: db,
- }
- }
- func (h *ProfileHandler) GetProfiles(c *gin.Context) {
- profiles, err := h.store.GetProfiles(c)
- if err != nil {
- c.JSON(500, gin.H{"error": "Failed to get profiles"})
- return
- }
- c.JSON(200, profiles)
- }
- func (h *ProfileHandler) CreateProfile(c *gin.Context) {
- var req common.CreateProfileRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(400, gin.H{"error": "Invalid request"})
- return
- }
- h.logger.Info("Creating profile")
- h.logger.Info(req)
- profile := &common.Profile{
- ID: uuid.New(),
- Name: req.Name,
- WorkspaceID: uuid.MustParse(req.WorkspaceID),
- }
- if err := h.store.CreateProfile(c, profile); err != nil {
- c.JSON(500, gin.H{"error": "Failed to create profile"})
- return
- }
- c.JSON(201, profile)
- }
- func (h *ProfileHandler) GetProfile(c *gin.Context) {
- id := c.Param("id")
- if id == "" {
- c.JSON(400, gin.H{"error": "profile id is required"})
- return
- }
- profile, err := h.store.GetProfile(c, id)
- if err != nil {
- c.JSON(500, gin.H{"error": "Failed to get profile"})
- return
- }
- c.JSON(200, profile)
- }
- func (h *ProfileHandler) UpdateProfile(c *gin.Context) {
- var req common.UpdateProfileRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(400, gin.H{"error": "Invalid request"})
- return
- }
- id := c.Param("id")
- if id == "" {
- c.JSON(400, gin.H{"error": "profile id is required"})
- return
- }
- profile, err := h.store.GetProfile(c, id)
- if err != nil {
- c.JSON(500, gin.H{"error": "Failed to get profile"})
- return
- }
- profile.Name = req.Name
- if err := h.store.UpdateProfile(c, profile); err != nil {
- c.JSON(500, gin.H{"error": "Failed to update profile"})
- return
- }
- c.JSON(200, profile)
- }
- func (h *ProfileHandler) DeleteProfile(c *gin.Context) {
- id := c.Param("id")
- if id == "" {
- c.JSON(400, gin.H{"error": "profile id is required"})
- return
- }
- if err := h.store.DeleteProfile(c, id); err != nil {
- c.JSON(500, gin.H{"error": "Failed to delete profile"})
- return
- }
- c.JSON(200, gin.H{"message": "Profile deleted"})
- }
- func (h *ProfileHandler) GetProfilesByWorkspace(c *gin.Context) {
- wId := c.Query("workspaceID")
- if wId == "" {
- c.JSON(400, gin.H{"error": "workspaceID is required"})
- return
- }
- profiles, err := h.store.GetProfilesByWorkspaceID(c, wId)
- if err != nil {
- c.JSON(500, gin.H{"error": "Failed to get profiles"})
- return
- }
- c.JSON(200, profiles)
- }
|