get_place_images.go 1.5 KB

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