get_place_things.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package thing
  2. import (
  3. "git.dmitriygnatenko.ru/dima/go-common/logger"
  4. "github.com/gofiber/fiber/v2"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/location"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/request"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  8. )
  9. // @Router /api/v1/things/place/{placeId} [get]
  10. // @Param placeId path int true "Place ID"
  11. // @Success 200 {object} dto.ThingsExtResponse
  12. // @Failure 400 {object} dto.ErrorResponse
  13. // @Failure 500 {object} dto.ErrorResponse
  14. // @Summary Get things by place ID
  15. // @Tags Things
  16. // @security APIKey
  17. // @Accept json
  18. // @Produce json
  19. func GetPlaceThingsHandler(
  20. thingRepository ThingRepository,
  21. thingTagRepository ThingTagRepository,
  22. ) fiber.Handler {
  23. return func(fctx *fiber.Ctx) error {
  24. ctx := fctx.Context()
  25. id, err := request.ConvertToUint64(fctx, "placeId")
  26. if err != nil {
  27. logger.Info(ctx, err.Error())
  28. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  29. }
  30. things, err := thingRepository.GetAllByPlaceID(ctx, id)
  31. if err != nil {
  32. logger.Error(ctx, err.Error())
  33. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  34. }
  35. tags, err := thingTagRepository.GetByPlaceID(ctx, id)
  36. if err != nil {
  37. logger.Error(ctx, err.Error())
  38. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  39. }
  40. things = location.ApplyLocation(fctx, things)
  41. tags = location.ApplyLocation(fctx, tags)
  42. return fctx.JSON(mappers.ToThingsExtResponse(things, tags))
  43. }
  44. }