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