delete_place_image.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package image
  2. import (
  3. "database/sql"
  4. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  6. "github.com/gofiber/fiber/v2"
  7. )
  8. // @Router /api/v1/images/place/{imageId} [delete]
  9. // @Param imageId path int true "Image ID"
  10. // @Success 200 {object} dto.EmptyResponse
  11. // @Failure 400 {object} dto.ErrorResponse
  12. // @Failure 500 {object} dto.ErrorResponse
  13. // @Summary Delete image
  14. // @Tags Images
  15. // @security APIKey
  16. // @Accept json
  17. // @Produce json
  18. func DeletePlaceImageHandler(sp interfaces.ServiceProvider) fiber.Handler {
  19. return func(fctx *fiber.Ctx) error {
  20. ctx := fctx.Context()
  21. id, err := fctx.ParamsInt("imageId")
  22. if err != nil {
  23. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  24. }
  25. image, err := sp.GetPlaceImageRepository().Get(ctx, id)
  26. if err != nil {
  27. if err == sql.ErrNoRows {
  28. return fiber.NewError(fiber.StatusBadRequest, "")
  29. }
  30. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  31. }
  32. if err = sp.GetPlaceImageRepository().Delete(ctx, id, nil); err != nil {
  33. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  34. }
  35. if err = sp.GetFileRepository().Delete(image.Image); err != nil {
  36. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  37. }
  38. return fctx.JSON(factory.CreateEmptyResponse())
  39. }
  40. }