delete_thing_notification.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package notification
  2. import (
  3. "database/sql"
  4. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  6. "github.com/gofiber/fiber/v2"
  7. )
  8. // @Router /api/v1/things/notifications/{thingId} [delete]
  9. // @Param thingId path int true "Thing ID"
  10. // @Success 200 {object} dto.EmptyResponse
  11. // @Failure 400 {object} dto.ErrorResponse
  12. // @Failure 500 {object} dto.ErrorResponse
  13. // @Summary Delete thing notification
  14. // @Tags Notifications
  15. // @security APIKey
  16. // @Accept json
  17. // @Produce json
  18. func DeleteThingNotificationHandler(sp interfaces.ServiceProvider) fiber.Handler {
  19. return func(fctx *fiber.Ctx) error {
  20. ctx := fctx.Context()
  21. id, err := fctx.ParamsInt("thingId")
  22. if err != nil {
  23. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  24. }
  25. _, err = sp.GetThingNotificationRepository().Get(ctx, id)
  26. if err != nil {
  27. if err == sql.ErrNoRows {
  28. return fiber.NewError(fiber.StatusBadRequest, "")
  29. }
  30. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  31. }
  32. if err = sp.GetThingNotificationRepository().Delete(ctx, id, nil); err != nil {
  33. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  34. }
  35. return fctx.JSON(factory.CreateEmptyResponse())
  36. }
  37. }