feat: add container management functionality
This commit is contained in:
61
internal/mapper/container_mapper.go
Normal file
61
internal/mapper/container_mapper.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func ToDomainContainer(r db.Container) *models.Container {
|
||||
return &models.Container{
|
||||
ID: r.ID,
|
||||
ShelfID: r.ShelfID,
|
||||
Name: r.Name,
|
||||
ContainerType: string(r.ContainerType),
|
||||
Description: r.Description.String,
|
||||
MaxCapacity: r.MaxCapacity.Int32,
|
||||
Metadata: r.Metadata,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelContainer(r *models.Container) *db.CreateContainerParams {
|
||||
return &db.CreateContainerParams{
|
||||
ShelfID: r.ShelfID,
|
||||
Name: r.Name,
|
||||
ContainerType: db.ContainerTypeEnum(r.ContainerType),
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
MaxCapacity: pgtype.Int4{
|
||||
Int32: r.MaxCapacity,
|
||||
Valid: r.MaxCapacity != 0,
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelContainer(r *models.Container) *db.UpdateContainerParams {
|
||||
return &db.UpdateContainerParams{
|
||||
Name: r.Name,
|
||||
ContainerType: db.NullContainerTypeEnum{
|
||||
ContainerTypeEnum: db.ContainerTypeEnum(r.ContainerType),
|
||||
Valid: r.ContainerType != "",
|
||||
},
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
MaxCapacity: pgtype.Int4{
|
||||
Int32: r.MaxCapacity,
|
||||
Valid: r.MaxCapacity != 0,
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
18
internal/models/container_model.go
Normal file
18
internal/models/container_model.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
ID int64 `json:"id"`
|
||||
ShelfID int64 `json:"shelfId"`
|
||||
Name string `json:"name"`
|
||||
ContainerType string `json:"containerType"`
|
||||
Description string `json:"description"`
|
||||
MaxCapacity int32 `json:"maxCapacity"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
20
internal/models/requests/container_request.go
Normal file
20
internal/models/requests/container_request.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package requests
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type CreateContainerRequest struct {
|
||||
ShelfID int64 `json:"shelfId" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
ContainerType string `json:"containerType" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
MaxCapacity int32 `json:"maxCapacity"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
}
|
||||
|
||||
type UpdateContainerRequest struct {
|
||||
Name string `json:"name"`
|
||||
ContainerType string `json:"containerType"`
|
||||
Description string `json:"description"`
|
||||
MaxCapacity int32 `json:"maxCapacity"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
}
|
||||
17
internal/models/responses/container_response.go
Normal file
17
internal/models/responses/container_response.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package responses
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type CreateContainerResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type UpdateContainerResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
ShelfID int64 `json:"shelfId"`
|
||||
Name string `json:"name"`
|
||||
ContainerType string `json:"containerType"`
|
||||
Description string `json:"description"`
|
||||
MaxCapacity int32 `json:"maxCapacity"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
}
|
||||
55
internal/repositories/container_repository.go
Normal file
55
internal/repositories/container_repository.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func CreateContainer(ctx context.Context, queries *db.Queries, body models.Container) (models.Container, error) {
|
||||
result, err := queries.CreateContainer(ctx, *mapper.ToModelContainer(&body))
|
||||
if err != nil {
|
||||
return models.Container{}, err
|
||||
}
|
||||
return *mapper.ToDomainContainer(result), nil
|
||||
}
|
||||
|
||||
func GetContainerByID(ctx context.Context, queries *db.Queries, id int64) (models.Container, error) {
|
||||
result, err := queries.GetContainerByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.Container{}, err
|
||||
}
|
||||
return *mapper.ToDomainContainer(result), nil
|
||||
}
|
||||
|
||||
func ListContainers(ctx context.Context, queries *db.Queries) ([]models.Container, error) {
|
||||
results, err := queries.ListContainers(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.Container
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainContainer(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateContainer(ctx context.Context, queries *db.Queries, body models.Container) (models.Container, error) {
|
||||
result, err := queries.UpdateContainer(ctx, *mapper.ToUpdateModelContainer(&body))
|
||||
if err != nil {
|
||||
return models.Container{}, err
|
||||
}
|
||||
return *mapper.ToDomainContainer(result), nil
|
||||
}
|
||||
|
||||
func DeleteContainer(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
|
||||
rowsAffected, err := queries.DeleteContainer(ctx, id)
|
||||
log.Info().Int64("id", id).Int64("rowsAffected", rowsAffected).Msg("Deleted container")
|
||||
if err != nil {
|
||||
return rowsAffected, err
|
||||
}
|
||||
return rowsAffected, nil
|
||||
}
|
||||
@@ -64,6 +64,15 @@ func NewRouter() *gin.Engine {
|
||||
shelve.PUT("/:id", utils.AsyncHandler(services.ShelveUpdate))
|
||||
shelve.DELETE("/:id", utils.AsyncHandler(services.ShelveDelete))
|
||||
}
|
||||
|
||||
container := v1.Group(constants.API_GROUP_CONTAINER)
|
||||
{
|
||||
container.GET("", utils.AsyncHandler(services.ContainerList))
|
||||
container.GET("/:id", utils.AsyncHandler(services.ContainerGetByID))
|
||||
container.POST("", utils.AsyncHandler(services.ContainerCreate))
|
||||
container.PUT("/:id", utils.AsyncHandler(services.ContainerUpdate))
|
||||
container.DELETE("/:id", utils.AsyncHandler(services.ContainerDelete))
|
||||
}
|
||||
}
|
||||
|
||||
r.GET(constants.API_PATH_PING, services.PingHandler)
|
||||
|
||||
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