123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package handlers
- import (
- "fmt"
- "net/http"
- "strconv"
- "git.linuxforward.com/byop/byop-engine/models"
- "git.linuxforward.com/byop/byop-engine/services"
- "github.com/gin-gonic/gin"
- )
- // UserHandler handles client-related operations
- type UserHandler struct {
- service *services.UserService
- }
- // NewUserHandler creates a new UserHandler
- func NewUserHandler(service *services.UserService) *UserHandler {
- return &UserHandler{
- service: service,
- }
- }
- // CreateUser creates a new client
- func (h *UserHandler) CreateUser(c *gin.Context) {
- var user *models.User
- if err := c.ShouldBindJSON(&user); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
- return
- }
- if err := h.service.CreateUser(user); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create user: %v", err)})
- return
- }
- c.JSON(http.StatusCreated, user)
- }
- // GetUser retrieves a user by ID
- func (h *UserHandler) GetUser(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
- return
- }
- user, err := h.service.GetUser(id)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get user: %v", err)})
- return
- }
- if user == nil {
- c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
- return
- }
- c.JSON(http.StatusOK, user)
- }
- // UpdateUser updates an existing user
- func (h *UserHandler) UpdateUser(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
- return
- }
- var user *models.User
- if err := c.ShouldBindJSON(&user); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
- return
- }
- user.ID = id // Set the ID for the user to update
- if err := h.service.UpdateUser(user); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update user: %v", err)})
- return
- }
- c.JSON(http.StatusOK, user)
- }
- // DeleteUser deletes a user by ID
- func (h *UserHandler) DeleteUser(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
- return
- }
- if err := h.service.DeleteUser(id); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete user: %v", err)})
- return
- }
- c.JSON(http.StatusNoContent, nil)
- }
- // ListUsers retrieves all users with optional filtering
- func (h *UserHandler) ListUsers(c *gin.Context) {
- filter := make(map[string]interface{})
- // Attempt to bind query parameters, but allow empty filters
- if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
- return
- }
- // Call the service with the filter (empty or populated)
- users, err := h.service.ListUsers(filter)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list users: %v", err)})
- return
- }
- c.JSON(http.StatusOK, users)
- }
- // GetUserDeployments retrieves all deployments for a user
- func (h *UserHandler) GetUserDeployments(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
- return
- }
- deployments, err := h.service.GetUserDeployments(id)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get user deployments: %v", err)})
- return
- }
- c.JSON(http.StatusOK, deployments)
- }
|