delete_thing_notification.go 1.4 KB

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