get_thing_images.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package image
  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/images/thing/{thingId} [get]
  10. // @Param thingId path int true "Thing ID"
  11. // @Success 200 {object} dto.ImagesResponse
  12. // @Failure 400 {object} dto.ErrorResponse
  13. // @Failure 500 {object} dto.ErrorResponse
  14. // @Summary Get images by thing ID
  15. // @Tags Images
  16. // @security APIKey
  17. // @Accept json
  18. // @Produce json
  19. func GetThingImagesHandler(
  20. thingImageRepository ThingImageRepository,
  21. ) fiber.Handler {
  22. return func(fctx *fiber.Ctx) error {
  23. ctx := fctx.Context()
  24. id, err := request.ConvertToUint64(fctx, "thingId")
  25. if err != nil {
  26. logger.Info(ctx, err.Error())
  27. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  28. }
  29. res, err := thingImageRepository.GetByThingID(ctx, id)
  30. if err != nil {
  31. logger.Error(ctx, err.Error())
  32. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  33. }
  34. return fctx.JSON(mappers.ToImagesResponse(location.ApplyLocation(fctx, res)))
  35. }
  36. }