get_place_things.go 1.3 KB

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