feat(BE): update DeleteOne method to use uint64 for ID and implement soft delete logic

This commit is contained in:
aguhh18
2025-12-30 19:39:10 +07:00
parent b988f45a0b
commit 3ecea6741f
3 changed files with 37 additions and 14 deletions
@@ -203,12 +203,12 @@ func (u *ExpenseController) UpdateOne(c *fiber.Ctx) error {
func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error { func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("id") param := c.Params("id")
id, err := strconv.Atoi(param) id64, err := strconv.ParseUint(param, 10, 64)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
} }
if err := u.ExpenseService.DeleteOne(c, uint(id)); err != nil { if err := u.ExpenseService.DeleteOne(c, id64); err != nil {
return err return err
} }
@@ -3,6 +3,8 @@ package repository
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"time"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository" "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -17,6 +19,7 @@ type ExpenseRepository interface {
GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error) GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error)
WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB
CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error) CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error)
DeleteOne(ctx context.Context, id uint) error
} }
type ExpenseRepositoryImpl struct { type ExpenseRepositoryImpl struct {
@@ -107,3 +110,23 @@ func (r *ExpenseRepositoryImpl) CountUnfinishedByProjectFlockKandang(ctx context
} }
return unfinished, nil return unfinished, nil
} }
func (r *ExpenseRepositoryImpl) DeleteOne(ctx context.Context, id uint) error {
// Cast to uint64 to match entity.Id type
id64 := uint64(id)
deletedAt := time.Now()
// Use raw SQL with interpolated integer to avoid type issues
// Interpolate id directly as integer literal (safe because it's uint64)
result := r.DB().WithContext(ctx).
Exec(`UPDATE "expenses" SET "deleted_at" = $1 WHERE "id" = `+fmt.Sprintf("%d", id64)+` AND "deleted_at" IS NULL`,
deletedAt)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}