get_place_images.go 1.7 KB

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