get_thing_notification.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package notification
  2. import (
  3. "database/sql"
  4. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  7. "github.com/gofiber/fiber/v2"
  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(sp interfaces.ServiceProvider) fiber.Handler {
  21. return func(fctx *fiber.Ctx) error {
  22. ctx := fctx.Context()
  23. id, err := fctx.ParamsInt("thingId")
  24. if err != nil {
  25. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  26. }
  27. res, err := sp.GetThingNotificationRepository().Get(ctx, id)
  28. if err != nil {
  29. if err == sql.ErrNoRows {
  30. return fiber.NewError(fiber.StatusNotFound, "")
  31. }
  32. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  33. }
  34. res = helpers.ApplyLocation(fctx, res)
  35. return fctx.JSON(mappers.ConvertToThingNotificationResponseDTO(*res))
  36. }
  37. }