get_thing_notification.go 1.4 KB

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