feat: add container management functionality
This commit is contained in:
202
internal/services/container_service.go
Normal file
202
internal/services/container_service.go
Normal file
@@ -0,0 +1,202 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"wm-backend/global"
|
||||
"wm-backend/internal/models"
|
||||
"wm-backend/internal/models/requests"
|
||||
"wm-backend/internal/models/responses"
|
||||
"wm-backend/internal/repositories"
|
||||
"wm-backend/pkg/helper"
|
||||
"wm-backend/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// ContainerCreate creates a new container.
|
||||
// It validates the request body and creates the container in the database.
|
||||
//
|
||||
// @Summary Create a new container
|
||||
// @Description Create a new container with the provided details
|
||||
// @Tags container
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateContainerRequest true "Container request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateContainerResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/containers [post]
|
||||
func ContainerCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateContainerRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
containerModel := &models.Container{
|
||||
ShelfID: requestBody.ShelfID,
|
||||
Name: requestBody.Name,
|
||||
ContainerType: requestBody.ContainerType,
|
||||
Description: requestBody.Description,
|
||||
MaxCapacity: requestBody.MaxCapacity,
|
||||
Metadata: requestBody.Metadata,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
container, err := repositories.CreateContainer(c.Request.Context(), global.Queries, *containerModel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create container")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create container")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Container created successfully", &responses.CreateContainerResponse{
|
||||
ID: container.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainerGetByID retrieves a single container by its ID.
|
||||
//
|
||||
// @Summary Get container by ID
|
||||
// @Description Retrieve a single container using its unique identifier
|
||||
// @Tags container
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Container ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.Container}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/containers/{id} [get]
|
||||
func ContainerGetByID(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
container, err := repositories.GetContainerByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to get container by ID: %d", id)
|
||||
response.NotFoundError(c, http.StatusNotFound, "Container not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", container)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainerList retrieves all containers.
|
||||
//
|
||||
// @Summary List all containers
|
||||
// @Description Retrieve a list of all containers ordered by creation date
|
||||
// @Tags container
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.Container}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/containers [get]
|
||||
func ContainerList(c *gin.Context) error {
|
||||
containers, err := repositories.ListContainers(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list containers")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", containers)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainerUpdate updates an existing container by its ID.
|
||||
// It validates the request body, fetches the existing record,
|
||||
// merges non-empty fields from the request, and updates the container in the database.
|
||||
//
|
||||
// @Summary Update container
|
||||
// @Description Update an existing container by its ID. Only non-empty fields will be updated.
|
||||
// @Tags container
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Container ID"
|
||||
// @Param body body requests.UpdateContainerRequest true "Container request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateContainerResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/containers/{id} [put]
|
||||
func ContainerUpdate(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
requestBody := requests.UpdateContainerRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
existing, err := repositories.GetContainerByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Container not found")
|
||||
return nil
|
||||
}
|
||||
if requestBody.Name != "" {
|
||||
existing.Name = requestBody.Name
|
||||
}
|
||||
if requestBody.ContainerType != "" {
|
||||
existing.ContainerType = requestBody.ContainerType
|
||||
}
|
||||
if requestBody.Description != "" {
|
||||
existing.Description = requestBody.Description
|
||||
}
|
||||
if requestBody.MaxCapacity != 0 {
|
||||
existing.MaxCapacity = requestBody.MaxCapacity
|
||||
}
|
||||
if len(requestBody.Metadata) > 0 {
|
||||
existing.Metadata = requestBody.Metadata
|
||||
}
|
||||
existing.UpdatedAt = time.Now()
|
||||
container, err := repositories.UpdateContainer(c.Request.Context(), global.Queries, existing)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to update container with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update container")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Container updated successfully", &responses.UpdateContainerResponse{
|
||||
ID: container.ID,
|
||||
ShelfID: container.ShelfID,
|
||||
Name: container.Name,
|
||||
ContainerType: container.ContainerType,
|
||||
Description: container.Description,
|
||||
MaxCapacity: container.MaxCapacity,
|
||||
Metadata: container.Metadata,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainerDelete deletes a container by its ID.
|
||||
//
|
||||
// @Summary Delete container
|
||||
// @Description Delete a container by its unique identifier
|
||||
// @Tags container
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Container ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/containers/{id} [delete]
|
||||
func ContainerDelete(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
rowsAffected, err := repositories.DeleteContainer(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to delete container with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete container")
|
||||
return nil
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Container not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Delete Success", nil)
|
||||
return nil
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func ShelveCreate(c *gin.Context) error {
|
||||
}
|
||||
shelve, err := repositories.CreateShelve(c.Request.Context(), global.Queries, *shelveModel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create shelve")
|
||||
log.Error().Any("shelve", shelve).Err(err).Msg("Failed to create shelve")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create shelve")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user