get_place_images.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package image
  2. import (
  3. "sort"
  4. "github.com/gofiber/fiber/v2"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  8. )
  9. // @Router /api/v1/images/place/{placeId} [get]
  10. // @Param placeId path int true "Place ID"
  11. // @Success 200 {object} dto.ImagesResponse
  12. // @Failure 400 {object} dto.ErrorResponse
  13. // @Failure 500 {object} dto.ErrorResponse
  14. // @Summary Get images by place ID (with child places)
  15. // @Tags Images
  16. // @security APIKey
  17. // @Accept json
  18. // @Produce json
  19. func GetPlaceImagesHandler(
  20. thingImageRepository ThingImageRepository,
  21. placeImageRepository PlaceImageRepository,
  22. ) fiber.Handler {
  23. return func(fctx *fiber.Ctx) error {
  24. var res []models.Image
  25. ctx := fctx.Context()
  26. id, err := fctx.ParamsInt("placeId")
  27. if err != nil {
  28. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  29. }
  30. placesRes, err := placeImageRepository.GetByPlaceID(ctx, id)
  31. if err != nil {
  32. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  33. }
  34. res = append(res, placesRes...)
  35. thingsRes, err := thingImageRepository.GetByPlaceID(ctx, id)
  36. if err != nil {
  37. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  38. }
  39. res = append(res, thingsRes...)
  40. sort.Slice(res, func(i, j int) bool {
  41. return res[i].CreatedAt.After(res[j].CreatedAt)
  42. })
  43. res = helpers.ApplyLocation(fctx, res)
  44. return fctx.JSON(mappers.ToImagesResponse(res))
  45. }
  46. }