delete_thing_notification.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package notification
  2. import (
  3. "database/sql"
  4. "errors"
  5. "github.com/gofiber/fiber/v2"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  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(
  19. thingNotificationRepository ThingNotificationRepository,
  20. ) fiber.Handler {
  21. return func(fctx *fiber.Ctx) error {
  22. ctx := fctx.Context()
  23. id, err := fctx.ParamsInt("thingId")
  24. if err != nil {
  25. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  26. }
  27. _, err = thingNotificationRepository.Get(ctx, id)
  28. if err != nil {
  29. if errors.Is(err, sql.ErrNoRows) {
  30. return fiber.NewError(fiber.StatusBadRequest, "")
  31. }
  32. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  33. }
  34. if err = thingNotificationRepository.Delete(ctx, id, nil); err != nil {
  35. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  36. }
  37. return fctx.JSON(factory.CreateEmptyResponse())
  38. }
  39. }