get_thing_images.go 1.0 KB

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