package controller import ( "strconv" "narasi-ahli-be/app/module/user_agent/request" "narasi-ahli-be/app/module/user_agent/response" "narasi-ahli-be/app/module/user_agent/service" utilRes "narasi-ahli-be/utils/response" "github.com/gofiber/fiber/v2" ) type userAgentController struct { userAgentService service.UserAgentService } type UserAgentController interface { UpdateUserAgents(c *fiber.Ctx) error GetAgentsByUser(c *fiber.Ctx) error } func NewUserAgentController(userAgentService service.UserAgentService) UserAgentController { return &userAgentController{userAgentService: userAgentService} } type UpdateUserAgentRequest struct { UserID uint `json:"userId"` AgentIDs []uint `json:"agentIds"` } // UpdateUserAgents godoc // @Summary Update agents assigned to user // @Description Replace all agent mapping for a user // @Tags User Agent // @Accept json // @Produce json // @Param request body request.UpdateUserAgentRequest true "User Agent Mapping" // @Success 200 {object} response.UpdateUserAgentResponse // @Failure 400 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /user-agent [put] func (c *userAgentController) UpdateUserAgents(ctx *fiber.Ctx) error { var req request.UpdateUserAgentRequest if err := ctx.BodyParser(&req); err != nil { return ctx.Status(fiber.StatusBadRequest).JSON(response.ErrorResponse{ Message: "invalid request", }) } err := c.userAgentService.UpdateUserAgents(req.UserID, req.AgentID) if err != nil { return ctx.Status(fiber.StatusInternalServerError).JSON(response.ErrorResponse{ Message: err.Error(), }) } return ctx.JSON(response.UpdateUserAgentResponse{ Message: "user agents updated", }) } // GetAgentsByUser godoc // @Summary Get agents by user // @Description Get all agent IDs assigned to a user // @Tags User Agent // @Produce json // @Param user_id query int true "User ID" // @Success 200 {object} response.GetUserAgentResponse // @Failure 400 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /user-agent [get] func (c *userAgentController) GetAgentsByUser(ctx *fiber.Ctx) error { userIDStr := ctx.Query("user_id") userID, err := strconv.Atoi(userIDStr) if err != nil || userID == 0 { return ctx.Status(fiber.StatusBadRequest).JSON(response.ErrorResponse{ Message: "user_id required", }) } agents, err := c.userAgentService.GetAgentsByUser(uint(userID)) if err != nil { return ctx.Status(fiber.StatusInternalServerError).JSON(response.ErrorResponse{ Message: err.Error(), }) } var agentResponses []response.AgentDetail for _, a := range agents { agentResponses = append(agentResponses, response.AgentDetail{ ID: a.ID, AgentID: a.AgentID, Name: a.Name, Description: a.Description, Instructions: a.Instructions, Type: a.Type, Status: a.Status, IsActive: a.IsActive, CreatedAt: a.CreatedAt, UpdatedAt: a.UpdatedAt, }) } // return ctx.JSON(response.GetUserAgentResponse{ // UserID: uint(userID), // Agents: agentResponses, // }) return utilRes.Resp(ctx, utilRes.Response{ Success: true, Data: response.GetUserAgentResponse{ UserID: uint(userID), Agents: agentResponses, }, }) }