123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package handlers
- import (
- "fmt"
- "net/http"
- "strconv"
- "git.linuxforward.com/byop/byop-engine/dbstore"
- "git.linuxforward.com/byop/byop-engine/models"
- "github.com/gin-gonic/gin"
- )
- // ClientHandler handles client-related operations and contains integrated service logic
- type ClientHandler struct {
- store *dbstore.SQLiteStore
- }
- // NewClientHandler creates a new ClientHandler
- func NewClientHandler(store *dbstore.SQLiteStore) *ClientHandler {
- return &ClientHandler{
- store: store,
- }
- }
- // ListClients returns all clients
- func (h *ClientHandler) ListClients(c *gin.Context) {
- filter := make(map[string]interface{})
- ctx := c.Request.Context()
- // Attempt to bind query parameters, but allow empty filters
- if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
- appErr := models.NewErrValidation("invalid_query_params", nil, err)
- models.RespondWithError(c, appErr)
- return
- }
- clients, err := h.store.GetAllClients(ctx)
- if err != nil {
- appErr := models.NewErrInternalServer("failed_fetch_clients", fmt.Errorf("Failed to fetch clients: %w", err))
- models.RespondWithError(c, appErr)
- return
- }
- c.JSON(http.StatusOK, clients)
- }
- // CreateClient creates a new client
- func (h *ClientHandler) CreateClient(c *gin.Context) {
- var client models.Client
- ctx := c.Request.Context()
- if err := c.ShouldBindJSON(&client); err != nil {
- appErr := models.NewErrValidation("invalid_request_body", nil, err)
- models.RespondWithError(c, appErr)
- return
- }
- // Validate client data
- if client.Name == "" {
- validationErrors := map[string]string{"name": "Client name is required"}
- appErr := models.NewErrValidation("client_name_required", validationErrors, nil)
- models.RespondWithError(c, appErr)
- return
- }
- id, err := h.store.CreateClient(ctx, client)
- if err != nil {
- appErr := models.NewErrInternalServer("failed_create_client", fmt.Errorf("Failed to create client: %w", err))
- models.RespondWithError(c, appErr)
- return
- }
- // Set the generated ID
- client.ID = id
- c.JSON(http.StatusCreated, client)
- }
- // GetClient returns a specific client
- func (h *ClientHandler) GetClient(c *gin.Context) {
- idStr := c.Param("id")
- ctx := c.Request.Context()
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
- models.RespondWithError(c, appErr)
- return
- }
- client, err := h.store.GetClientByID(ctx, int(id))
- if err != nil {
- models.RespondWithError(c, err)
- return
- }
- if client.ID == 0 {
- appErr := models.NewErrNotFound("client_not_found", fmt.Errorf("Client with ID %d not found", id))
- models.RespondWithError(c, appErr)
- return
- }
- c.JSON(http.StatusOK, client)
- }
- // UpdateClient updates a client
- func (h *ClientHandler) UpdateClient(c *gin.Context) {
- idStr := c.Param("id")
- ctx := c.Request.Context()
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
- models.RespondWithError(c, appErr)
- return
- }
- var updatedClient models.Client
- if err := c.ShouldBindJSON(&updatedClient); err != nil {
- appErr := models.NewErrValidation("invalid_request_body", nil, err)
- models.RespondWithError(c, appErr)
- return
- }
- updatedClient.ID = int(id)
- // Validate client data
- if updatedClient.Name == "" {
- validationErrors := map[string]string{"name": "Client name is required"}
- appErr := models.NewErrValidation("client_name_required", validationErrors, nil)
- models.RespondWithError(c, appErr)
- return
- }
- if err := h.store.UpdateClient(ctx, updatedClient); err != nil {
- models.RespondWithError(c, err)
- return
- }
- c.JSON(http.StatusOK, updatedClient)
- }
- // DeleteClient deletes a client
- func (h *ClientHandler) DeleteClient(c *gin.Context) {
- idStr := c.Param("id")
- ctx := c.Request.Context()
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
- models.RespondWithError(c, appErr)
- return
- }
- if err := h.store.DeleteClient(ctx, int(id)); err != nil {
- models.RespondWithError(c, err)
- return
- }
- c.Status(http.StatusNoContent)
- }
- // GetClientDeployments returns all deployments for a client
- func (h *ClientHandler) GetClientDeployments(c *gin.Context) {
- idStr := c.Param("id")
- ctx := c.Request.Context()
- id, err := strconv.ParseInt(idStr, 10, 64)
- if err != nil {
- appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
- models.RespondWithError(c, appErr)
- return
- }
- client, err := h.store.GetClientByID(ctx, int(id))
- if err != nil {
- models.RespondWithError(c, err)
- return
- }
- if client.ID == 0 {
- appErr := models.NewErrNotFound("client_not_found_for_deployments", fmt.Errorf("Client with ID %d not found", id))
- models.RespondWithError(c, appErr)
- return
- }
- deployments := []models.Deployment{}
- c.JSON(http.StatusOK, deployments)
- }
|