users.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "git.linuxforward.com/byop/byop-engine/models"
  7. "git.linuxforward.com/byop/byop-engine/services"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // UserHandler handles client-related operations
  11. type UserHandler struct {
  12. service *services.UserService
  13. }
  14. // NewUserHandler creates a new UserHandler
  15. func NewUserHandler(service *services.UserService) *UserHandler {
  16. return &UserHandler{
  17. service: service,
  18. }
  19. }
  20. // CreateUser creates a new client
  21. func (h *UserHandler) CreateUser(c *gin.Context) {
  22. var user *models.User
  23. if err := c.ShouldBindJSON(&user); err != nil {
  24. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  25. return
  26. }
  27. if err := h.service.CreateUser(user); err != nil {
  28. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create user: %v", err)})
  29. return
  30. }
  31. c.JSON(http.StatusCreated, user)
  32. }
  33. // GetUser retrieves a user by ID
  34. func (h *UserHandler) GetUser(c *gin.Context) {
  35. idStr := c.Param("id")
  36. id, err := strconv.ParseInt(idStr, 10, 64)
  37. if err != nil {
  38. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
  39. return
  40. }
  41. user, err := h.service.GetUser(id)
  42. if err != nil {
  43. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get user: %v", err)})
  44. return
  45. }
  46. if user == nil {
  47. c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
  48. return
  49. }
  50. c.JSON(http.StatusOK, user)
  51. }
  52. // UpdateUser updates an existing user
  53. func (h *UserHandler) UpdateUser(c *gin.Context) {
  54. idStr := c.Param("id")
  55. id, err := strconv.ParseInt(idStr, 10, 64)
  56. if err != nil {
  57. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
  58. return
  59. }
  60. var user *models.User
  61. if err := c.ShouldBindJSON(&user); err != nil {
  62. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
  63. return
  64. }
  65. user.ID = id // Set the ID for the user to update
  66. if err := h.service.UpdateUser(user); err != nil {
  67. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update user: %v", err)})
  68. return
  69. }
  70. c.JSON(http.StatusOK, user)
  71. }
  72. // DeleteUser deletes a user by ID
  73. func (h *UserHandler) DeleteUser(c *gin.Context) {
  74. idStr := c.Param("id")
  75. id, err := strconv.ParseInt(idStr, 10, 64)
  76. if err != nil {
  77. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
  78. return
  79. }
  80. if err := h.service.DeleteUser(id); err != nil {
  81. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete user: %v", err)})
  82. return
  83. }
  84. c.JSON(http.StatusNoContent, nil)
  85. }
  86. // ListUsers retrieves all users with optional filtering
  87. func (h *UserHandler) ListUsers(c *gin.Context) {
  88. filter := make(map[string]interface{})
  89. // Attempt to bind query parameters, but allow empty filters
  90. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  91. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  92. return
  93. }
  94. // Call the service with the filter (empty or populated)
  95. users, err := h.service.ListUsers(filter)
  96. if err != nil {
  97. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list users: %v", err)})
  98. return
  99. }
  100. c.JSON(http.StatusOK, users)
  101. }
  102. // GetUserDeployments retrieves all deployments for a user
  103. func (h *UserHandler) GetUserDeployments(c *gin.Context) {
  104. idStr := c.Param("id")
  105. id, err := strconv.ParseInt(idStr, 10, 64)
  106. if err != nil {
  107. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
  108. return
  109. }
  110. deployments, err := h.service.GetUserDeployments(id)
  111. if err != nil {
  112. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get user deployments: %v", err)})
  113. return
  114. }
  115. c.JSON(http.StatusOK, deployments)
  116. }