delete_thing_notification.go 1.5 KB

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