feat: add room management functionality
This commit is contained in:
43
internal/mapper/room_mapper.go
Normal file
43
internal/mapper/room_mapper.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func ToDomainRoom(r db.Room) *models.Room {
|
||||
return &models.Room{
|
||||
ID: r.ID,
|
||||
WarehouseID: r.WarehouseID,
|
||||
Name: r.Name,
|
||||
Description: r.Description.String,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelRoom(r *models.Room) *db.CreateRoomParams {
|
||||
return &db.CreateRoomParams{
|
||||
WarehouseID: r.WarehouseID,
|
||||
Name: r.Name,
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelRoom(r *models.Room) *db.UpdateRoomParams {
|
||||
return &db.UpdateRoomParams{
|
||||
Name: r.Name,
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
12
internal/models/requests/room_request.go
Normal file
12
internal/models/requests/room_request.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package requests
|
||||
|
||||
type CreateRoomRequest struct {
|
||||
WarehouseID int64 `json:"warehouseId" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type UpdateRoomRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
12
internal/models/responses/room_response.go
Normal file
12
internal/models/responses/room_response.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package responses
|
||||
|
||||
type CreateRoomResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type UpdateRoomResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
WarehouseID int64 `json:"warehouseId"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
12
internal/models/room_model.go
Normal file
12
internal/models/room_model.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Room struct {
|
||||
ID int64 `json:"id"`
|
||||
WarehouseID int64 `json:"warehouseId"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
48
internal/repositories/room_repository.go
Normal file
48
internal/repositories/room_repository.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
func CreateRoom(ctx context.Context, queries *db.Queries, body models.Room) (models.Room, error) {
|
||||
result, err := queries.CreateRoom(ctx, *mapper.ToModelRoom(&body))
|
||||
if err != nil {
|
||||
return models.Room{}, err
|
||||
}
|
||||
return *mapper.ToDomainRoom(result), nil
|
||||
}
|
||||
|
||||
func GetRoomByID(ctx context.Context, queries *db.Queries, id int64) (models.Room, error) {
|
||||
result, err := queries.GetRoomByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.Room{}, err
|
||||
}
|
||||
return *mapper.ToDomainRoom(result), nil
|
||||
}
|
||||
|
||||
func ListRooms(ctx context.Context, queries *db.Queries) ([]models.Room, error) {
|
||||
results, err := queries.ListRooms(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.Room
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainRoom(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateRoom(ctx context.Context, queries *db.Queries, body models.Room) (models.Room, error) {
|
||||
result, err := queries.UpdateRoom(ctx, *mapper.ToUpdateModelRoom(&body))
|
||||
if err != nil {
|
||||
return models.Room{}, err
|
||||
}
|
||||
return *mapper.ToDomainRoom(result), nil
|
||||
}
|
||||
|
||||
func DeleteRoom(ctx context.Context, queries *db.Queries, id int64) error {
|
||||
return queries.DeleteRoom(ctx, id)
|
||||
}
|
||||
@@ -37,6 +37,15 @@ func NewRouter() *gin.Engine {
|
||||
warehouse.PUT("/:id", utils.AsyncHandler(services.WareHouseUpdate))
|
||||
warehouse.DELETE("/:id", utils.AsyncHandler(services.WareHouseDelete))
|
||||
}
|
||||
|
||||
room := v1.Group(constants.API_GROUP_ROOM)
|
||||
{
|
||||
room.GET("", utils.AsyncHandler(services.RoomList))
|
||||
room.GET("/:id", utils.AsyncHandler(services.RoomGetByID))
|
||||
room.POST("", utils.AsyncHandler(services.RoomCreate))
|
||||
room.PUT("/:id", utils.AsyncHandler(services.RoomUpdate))
|
||||
room.DELETE("/:id", utils.AsyncHandler(services.RoomDelete))
|
||||
}
|
||||
}
|
||||
|
||||
r.GET(constants.API_PATH_PING, services.PingHandler)
|
||||
|
||||
181
internal/services/room_service.go
Normal file
181
internal/services/room_service.go
Normal file
@@ -0,0 +1,181 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// RoomCreate creates a new room.
|
||||
// It validates the request body and creates the room in the database.
|
||||
//
|
||||
// @Summary Create a new room
|
||||
// @Description Create a new room with the provided details
|
||||
// @Tags room
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateRoomRequest true "Room request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateRoomResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/rooms [post]
|
||||
func RoomCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateRoomRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
roomModel := &models.Room{
|
||||
WarehouseID: requestBody.WarehouseID,
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
room, err := repositories.CreateRoom(c.Request.Context(), global.Queries, *roomModel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create room")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create room")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Room created successfully", &responses.CreateRoomResponse{
|
||||
ID: room.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// RoomGetByID retrieves a single room by its ID.
|
||||
//
|
||||
// @Summary Get room by ID
|
||||
// @Description Retrieve a single room using its unique identifier
|
||||
// @Tags room
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Room ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.Room}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/rooms/{id} [get]
|
||||
func RoomGetByID(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
|
||||
}
|
||||
room, err := repositories.GetRoomByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to get room by ID: %d", id)
|
||||
response.NotFoundError(c, http.StatusNotFound, "Room not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", room)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RoomList retrieves all rooms.
|
||||
//
|
||||
// @Summary List all rooms
|
||||
// @Description Retrieve a list of all rooms ordered by creation date
|
||||
// @Tags room
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.Room}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/rooms [get]
|
||||
func RoomList(c *gin.Context) error {
|
||||
rooms, err := repositories.ListRooms(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list rooms")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", rooms)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RoomUpdate updates an existing room by its ID.
|
||||
// It validates the request body, fetches the existing record,
|
||||
// merges non-empty fields from the request, and updates the room in the database.
|
||||
//
|
||||
// @Summary Update room
|
||||
// @Description Update an existing room by its ID. Only non-empty fields will be updated.
|
||||
// @Tags room
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Room ID"
|
||||
// @Param body body requests.UpdateRoomRequest true "Room request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateRoomResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/rooms/{id} [put]
|
||||
func RoomUpdate(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.UpdateRoomRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
existing, err := repositories.GetRoomByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Room not found")
|
||||
return nil
|
||||
}
|
||||
if requestBody.Name != "" {
|
||||
existing.Name = requestBody.Name
|
||||
}
|
||||
if requestBody.Description != "" {
|
||||
existing.Description = requestBody.Description
|
||||
}
|
||||
existing.UpdatedAt = time.Now()
|
||||
room, err := repositories.UpdateRoom(c.Request.Context(), global.Queries, existing)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update room")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Room updated successfully", &responses.UpdateRoomResponse{
|
||||
ID: room.ID,
|
||||
WarehouseID: room.WarehouseID,
|
||||
Name: room.Name,
|
||||
Description: room.Description,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// RoomDelete deletes a room by its ID.
|
||||
//
|
||||
// @Summary Delete room
|
||||
// @Description Delete a room by its unique identifier
|
||||
// @Tags room
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Room ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/rooms/{id} [delete]
|
||||
func RoomDelete(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
|
||||
}
|
||||
err = repositories.DeleteRoom(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete room")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Đã xóa thành công", nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user