Merge branch 'promoDelete' into 'dev'
add base logic for delete promocode See merge request pena-services/codeword!11
This commit is contained in:
commit
f50cdf5168
@ -216,6 +216,47 @@ paths:
|
||||
'500':
|
||||
description: Внутренняя ошибка сервера
|
||||
|
||||
/promocode/{promocodeID}:
|
||||
delete:
|
||||
summary: Мягко удалить промокод по его id
|
||||
parameters:
|
||||
- in: path
|
||||
name: promocodeID
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
description: Id промокода для удаления
|
||||
responses:
|
||||
'204':
|
||||
description: Промокод успешно помечен как удаленный
|
||||
'400':
|
||||
description: Неверный запрос, отсутствует идентификатор промокода
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: string
|
||||
'404':
|
||||
description: Промокод не найден
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: string
|
||||
'500':
|
||||
description: Внутренняя ошибка сервера
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: string
|
||||
|
||||
|
||||
components:
|
||||
schemas:
|
||||
|
@ -100,7 +100,7 @@ func (p *PromoCodeController) Activate(c *fiber.Ctx) error {
|
||||
if errors.Is(err, repository.ErrPromoCodeNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "PromoCode not found"})
|
||||
}
|
||||
|
||||
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||
}
|
||||
|
||||
@ -109,3 +109,24 @@ func (p *PromoCodeController) Activate(c *fiber.Ctx) error {
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(resp)
|
||||
}
|
||||
|
||||
func (p *PromoCodeController) Delete(c *fiber.Ctx) error {
|
||||
promoCodeID := c.Params("promocodeID")
|
||||
|
||||
if promoCodeID == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "PromoCode ID is required"})
|
||||
}
|
||||
|
||||
err := p.promoCodeService.DeletePromoCode(c.Context(), promoCodeID)
|
||||
if err != nil {
|
||||
p.logger.Error("Failed to delete promocode", zap.Error(err))
|
||||
|
||||
if errors.Is(err, repository.ErrPromoCodeNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "PromoCode not found"})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
@ -257,3 +257,25 @@ func (r *PromoCodeRepository) ActivatePromo(ctx context.Context, req *models.Act
|
||||
|
||||
return greetings, nil
|
||||
}
|
||||
|
||||
func (r *PromoCodeRepository) DeletePromoCode(ctx context.Context, promoCodeID string) error {
|
||||
id, err := primitive.ObjectIDFromHex(promoCodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result, err := r.mdb.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"_id": id, "delete": false},
|
||||
bson.M{"$set": bson.M{"delete": true}},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.MatchedCount == 0 {
|
||||
return ErrPromoCodeNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -61,6 +61,7 @@ func (s *Server) registerRoutes() {
|
||||
s.app.Put("/promocode/edit", s.PromoCodeController.EditPromoCode)
|
||||
s.app.Post("/promocode/getList", s.PromoCodeController.GetList)
|
||||
s.app.Post("/promocode/activate", s.PromoCodeController.Activate)
|
||||
s.app.Delete("/promocode/:promocodeID", s.PromoCodeController.Delete)
|
||||
//... other
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ type PromoCodeRepository interface {
|
||||
EditPromoCode(ctx context.Context, req *models.ReqEditPromoCode) (*models.PromoCode, error)
|
||||
GetPromoCodesList(ctx context.Context, req *models.GetPromoCodesListReq) ([]models.PromoCode, int64, error)
|
||||
ActivatePromo(ctx context.Context, req *models.ActivateReq) (string, error)
|
||||
DeletePromoCode(ctx context.Context, promoCodeID string) error
|
||||
}
|
||||
|
||||
type PromoDeps struct {
|
||||
@ -69,3 +70,13 @@ func (s *PromoCodeService) ActivatePromo(ctx context.Context, req *models.Activa
|
||||
|
||||
return greetings, nil
|
||||
}
|
||||
|
||||
func (s *PromoCodeService) DeletePromoCode(ctx context.Context, promoCodeID string) error {
|
||||
err := s.promoCodeRepo.DeletePromoCode(ctx, promoCodeID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed simple delete promocode from database", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user