delete_thing.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package thing
  2. import (
  3. "database/sql"
  4. "errors"
  5. "github.com/gofiber/fiber/v2"
  6. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  8. )
  9. // @Router /api/v1/things/{thingId} [delete]
  10. // @Param thingId path int true "Thing ID"
  11. // @Success 200 {object} dto.EmptyResponse
  12. // @Failure 400 {object} dto.ErrorResponse
  13. // @Failure 500 {object} dto.ErrorResponse
  14. // @Summary Delete thing
  15. // @Tags Things
  16. // @security APIKey
  17. // @Accept json
  18. // @Produce json
  19. func DeleteThingHandler(
  20. thingRepository ThingRepository,
  21. thingTagRepository ThingTagRepository,
  22. placeThingRepository PlaceThingRepository,
  23. thingImageRepository ThingImageRepository,
  24. thingNotificationRepository ThingNotificationRepository,
  25. fileRepository FileRepository,
  26. ) fiber.Handler {
  27. return func(fctx *fiber.Ctx) error {
  28. ctx := fctx.Context()
  29. id, err := fctx.ParamsInt("thingId")
  30. if err != nil {
  31. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  32. }
  33. if _, err = thingRepository.Get(ctx, id); err != nil {
  34. if errors.Is(err, sql.ErrNoRows) {
  35. return fiber.NewError(fiber.StatusBadRequest, "")
  36. }
  37. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  38. }
  39. tx, err := thingRepository.BeginTx(ctx, API.DefaultTxLevel)
  40. if err != nil {
  41. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  42. }
  43. if err = placeThingRepository.DeleteThing(ctx, id, tx); err != nil {
  44. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  45. }
  46. images, err := thingImageRepository.GetByThingID(ctx, id)
  47. if err != nil {
  48. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  49. }
  50. imageURLs := make([]string, 0, len(images))
  51. for i := range images {
  52. imageURLs = append(imageURLs, images[i].Image)
  53. if err = thingImageRepository.Delete(ctx, images[i].ID, tx); err != nil {
  54. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  55. }
  56. }
  57. if err = thingTagRepository.DeleteByThingID(ctx, id, tx); err != nil {
  58. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  59. }
  60. if err = thingNotificationRepository.Delete(ctx, id, tx); err != nil {
  61. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  62. }
  63. if err = thingRepository.Delete(ctx, id, tx); err != nil {
  64. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  65. }
  66. if err = thingRepository.CommitTx(tx); err != nil {
  67. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  68. }
  69. if len(imageURLs) > 0 {
  70. for i := range imageURLs {
  71. if err = fileRepository.Delete(imageURLs[i]); err != nil {
  72. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  73. }
  74. }
  75. }
  76. return fctx.JSON(factory.CreateEmptyResponse())
  77. }
  78. }