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