diff --git a/internal/controller/tariff_internal/controller.go b/internal/controller/tariff_internal/controller.go index 04d2f12..7fb1036 100644 --- a/internal/controller/tariff_internal/controller.go +++ b/internal/controller/tariff_internal/controller.go @@ -149,5 +149,31 @@ func (t *TariffInternal) Update(ctx *fiber.Ctx) error { } func (t *TariffInternal) Restore(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": "id don`t be empty"}) + } + + 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.Restore(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) } diff --git a/internal/repository/privilege/privilege.go b/internal/repository/privilege/privilege.go index e3302cf..c50e9f5 100644 --- a/internal/repository/privilege/privilege.go +++ b/internal/repository/privilege/privilege.go @@ -87,9 +87,10 @@ func (p *Privilege) Create(ctx context.Context, req models.CreateUpdateReq) (mod func (p *Privilege) Update(ctx context.Context, req models.CreateUpdateReq) (models.Privilege, error) { exist := models.Privilege{} err := p.mdb.FindOne(ctx, bson.M{"privilegeId": req.PrivilegeId}).Decode(&exist) - if err == mongo.ErrNoDocuments { - return models.Privilege{}, errors.ErrNotFound - } else if err != nil { + if err != nil { + if err == mongo.ErrNoDocuments { + return models.Privilege{}, errors.ErrNotFound + } p.logger.Error("Failed to find privilege", zap.Error(err)) return models.Privilege{}, err } @@ -268,9 +269,10 @@ func (p *Privilege) UpdateMany(ctx context.Context, req models.ManyCreateUpdate) func (p *Privilege) RestorePrivilege(ctx context.Context, id string) (models.Privilege, error) { exist := models.Privilege{} err := p.mdb.FindOne(ctx, bson.M{"privilegeId": id}).Decode(&exist) - if err == mongo.ErrNoDocuments { - return models.Privilege{}, errors.ErrNotFound - } else if err != nil { + if err != nil { + if err == mongo.ErrNoDocuments { + return models.Privilege{}, errors.ErrNotFound + } p.logger.Error("Failed to find privilege", zap.Error(err)) return models.Privilege{}, err } diff --git a/internal/repository/tariff/tariff.go b/internal/repository/tariff/tariff.go index cbfac2a..66ba118 100644 --- a/internal/repository/tariff/tariff.go +++ b/internal/repository/tariff/tariff.go @@ -112,9 +112,10 @@ func (t *Tariff) SoftDelete(ctx context.Context, tariffID primitive.ObjectID) (m 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 { + if err != nil { + if err == mongo.ErrNoDocuments { + return tariff, errors.ErrNotFound + } t.logger.Error("failed soft delete tariff", zap.Error(err)) return tariff, err } @@ -125,9 +126,10 @@ func (t *Tariff) SoftDelete(ctx context.Context, tariffID primitive.ObjectID) (m func (t *Tariff) Update(ctx context.Context, tariffID primitive.ObjectID, req models.Tariff) (models.Tariff, error) { var tariff models.Tariff err := t.mdb.FindOne(ctx, bson.M{"_id": tariffID}).Decode(&tariff) - if err == mongo.ErrNoDocuments { - return tariff, errors.ErrNotFound - } else if err != nil { + if err != nil { + if err == mongo.ErrNoDocuments { + return tariff, errors.ErrNotFound + } t.logger.Error("failed find tariff", zap.Error(err)) return tariff, err } @@ -179,12 +181,30 @@ func (t *Tariff) Update(ctx context.Context, tariffID primitive.ObjectID, req mo } err = t.mdb.FindOneAndUpdate(ctx, bson.M{"_id": tariffID}, update).Decode(&tariff) - if err == mongo.ErrNoDocuments { - return tariff, errors.ErrNotFound - } else if err != nil { + if err != nil { + if err == mongo.ErrNoDocuments { + return tariff, errors.ErrNotFound + } t.logger.Error("failed update tariff", zap.Error(err)) return tariff, err } return tariff, nil } + +func (t *Tariff) Restore(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": false}} + err := t.mdb.FindOneAndUpdate(ctx, filter, update, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&tariff) + if err != nil { + if err == mongo.ErrNoDocuments { + return tariff, errors.ErrNotFound + } + t.logger.Error("failed restore tariff", zap.Error(err)) + return tariff, err + } + + return tariff, nil +}