fix(delete): update delete operations to return affected rows for cabinets, rooms, and warehouses

This commit is contained in:
Tran Anh Tuan
2026-05-08 15:54:28 +07:00
parent 58cfe890a1
commit 4382c53aac
15 changed files with 75 additions and 36 deletions

View File

@@ -43,6 +43,10 @@ func UpdateCabinet(ctx context.Context, queries *db.Queries, body models.Cabinet
return *mapper.ToDomainCabinet(result), nil
}
func DeleteCabinet(ctx context.Context, queries *db.Queries, id int64) error {
return queries.DeleteCabinet(ctx, id)
func DeleteCabinet(ctx context.Context, queries *db.Queries, id int64) (rowAffected int64, err error) {
rowsAffected, err := queries.DeleteCabinet(ctx, id)
if err != nil {
return rowAffected, err
}
return rowsAffected, nil
}

View File

@@ -43,6 +43,10 @@ func UpdateRoom(ctx context.Context, queries *db.Queries, body models.Room) (mod
return *mapper.ToDomainRoom(result), nil
}
func DeleteRoom(ctx context.Context, queries *db.Queries, id int64) error {
return queries.DeleteRoom(ctx, id)
func DeleteRoom(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
rowsAffected, err := queries.DeleteRoom(ctx, id)
if err != nil {
return rowsAffected, err
}
return rowsAffected, nil
}

View File

@@ -43,6 +43,10 @@ func UpdateWarehouse(ctx context.Context, queries *db.Queries, body models.Wareh
return *mapper.ToDomainWarehouse(result), nil
}
func DeleteWarehouse(ctx context.Context, queries *db.Queries, id int64) error {
return queries.DeleteWarehouse(ctx, id)
func DeleteWarehouse(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
rowsAffected, err := queries.DeleteWarehouse(ctx, id)
if err != nil {
return rowsAffected, err
}
return rowsAffected, nil
}

View File

@@ -171,11 +171,16 @@ func CabinetDelete(c *gin.Context) error {
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
return nil
}
err = repositories.DeleteCabinet(c.Request.Context(), global.Queries, id)
rowsAffected, err := repositories.DeleteCabinet(c.Request.Context(), global.Queries, id)
if err != nil {
if rowsAffected == 0 {
response.NotFoundError(c, http.StatusNotFound, "Cabinet not found")
return nil
}
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete cabinet")
return nil
}
response.Ok(c, "Đã xóa thành công", nil)
response.Ok(c, "Delete Success", nil)
return nil
}

View File

@@ -172,12 +172,16 @@ func RoomDelete(c *gin.Context) error {
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
return nil
}
err = repositories.DeleteRoom(c.Request.Context(), global.Queries, id)
rowsAffected, err := repositories.DeleteRoom(c.Request.Context(), global.Queries, id)
if err != nil {
log.Error().Err(err).Msgf("Failed to delete room with ID: %d", id)
if rowsAffected == 0 {
response.NotFoundError(c, http.StatusNotFound, "Room not found")
return nil
}
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete room")
return nil
}
response.Ok(c, "Đã xóa thành công", nil)
response.Ok(c, "Delete Success", nil)
return nil
}

View File

@@ -13,6 +13,7 @@ import (
"wm-backend/response"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
// WareHouseCreate creates a new warehouse.
@@ -161,11 +162,16 @@ func WareHouseDelete(c *gin.Context) error {
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
return nil
}
err = repositories.DeleteWarehouse(c.Request.Context(), global.Queries, id)
rowsAffected, err := repositories.DeleteWarehouse(c.Request.Context(), global.Queries, id)
if err != nil {
log.Error().Err(err).Msgf("Failed to delete warehouse with ID: %d", id)
if rowsAffected == 0 {
response.NotFoundError(c, http.StatusNotFound, "Warehouse not found")
return nil
}
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete warehouse")
return nil
}
response.Ok(c, "Đã xóa thành công", nil)
response.Ok(c, "Delete Success", nil)
return nil
}