added method for soft delete tariff
This commit is contained in:
parent
91b67483b8
commit
9de4b4d894
@ -82,7 +82,34 @@ func (t *TariffInternal) Create(ctx *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (t *TariffInternal) Delete(ctx *fiber.Ctx) error {
|
||||
return nil
|
||||
var req struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||
}
|
||||
|
||||
if req.ID == "" {
|
||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Tariff id don't be nil"})
|
||||
}
|
||||
|
||||
objID, err := primitive.ObjectIDFromHex(req.ID)
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "no valid object tariff id"})
|
||||
}
|
||||
|
||||
result, err := t.repo.SoftDelete(ctx.Context(), objID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, our_errors.ErrNotFound):
|
||||
return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Tariff not found"})
|
||||
default:
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(result)
|
||||
}
|
||||
|
||||
func (t *TariffInternal) Update(ctx *fiber.Ctx) error {
|
||||
|
@ -103,3 +103,18 @@ func (t *Tariff) GetList(ctx context.Context, page, limit int64, userID string)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (t *Tariff) SoftDelete(ctx context.Context, tariffID primitive.ObjectID) (models.Tariff, error) {
|
||||
var tariff models.Tariff
|
||||
filter := bson.M{"_id": tariffID}
|
||||
update := bson.M{"$set": bson.M{"isDeleted": true, "deletedAt": time.Now()}}
|
||||
err := t.mdb.FindOneAndUpdate(ctx, filter, update, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&tariff)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return tariff, errors.ErrNotFound
|
||||
} else if err != nil {
|
||||
t.logger.Error("failed soft delete tariff", zap.Error(err))
|
||||
return tariff, err
|
||||
}
|
||||
|
||||
return tariff, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user