delete_place.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package place
  2. import (
  3. "database/sql"
  4. "github.com/gofiber/fiber/v2"
  5. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  7. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  8. )
  9. // @Router /api/v1/places/{placeId} [delete]
  10. // @Param placeId path int true "Place ID"
  11. // @Success 200 {object} dto.EmptyResponse
  12. // @Failure 400 {object} dto.ErrorResponse
  13. // @Failure 500 {object} dto.ErrorResponse
  14. // @Summary Delete place
  15. // @Tags Places
  16. // @security APIKey
  17. // @Accept json
  18. // @Produce json
  19. func DeletePlaceHandler(
  20. placeRepository PlaceRepository,
  21. thingRepository ThingRepository,
  22. placeImageRepository PlaceImageRepository,
  23. thingImageRepository ThingImageRepository,
  24. placeThingRepository PlaceThingRepository,
  25. thingTagRepository ThingTagRepository,
  26. thingNotificationRepository ThingNotificationRepository,
  27. fileRepository FileRepository,
  28. ) fiber.Handler {
  29. return func(fctx *fiber.Ctx) error {
  30. ctx := fctx.Context()
  31. id, err := fctx.ParamsInt("placeId")
  32. if err != nil {
  33. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  34. }
  35. _, err = placeRepository.Get(ctx, id)
  36. if err != nil {
  37. if err == sql.ErrNoRows {
  38. return fiber.NewError(fiber.StatusBadRequest, "")
  39. }
  40. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  41. }
  42. nestedRes, err := placeRepository.GetNestedPlaces(ctx, id)
  43. if err != nil {
  44. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  45. }
  46. if len(nestedRes) > 0 {
  47. return fiber.NewError(fiber.StatusBadRequest, "")
  48. }
  49. placeImages, err := placeImageRepository.GetByPlaceID(ctx, id)
  50. if err != nil {
  51. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  52. }
  53. things, err := thingRepository.GetByPlaceID(ctx, id)
  54. if err != nil {
  55. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  56. }
  57. var thingImages []models.Image
  58. thingIDs := make([]int, 0, len(things))
  59. for _, thing := range things {
  60. thingIDs = append(thingIDs, thing.ID)
  61. thingImagesRes, thingImagesErr := thingImageRepository.GetByThingID(ctx, thing.ID)
  62. if thingImagesErr != nil {
  63. return fiber.NewError(fiber.StatusInternalServerError, thingImagesErr.Error())
  64. }
  65. thingImages = append(thingImages, thingImagesRes...)
  66. }
  67. tx, err := placeRepository.BeginTx(ctx, API.DefaultTxLevel)
  68. if err != nil {
  69. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  70. }
  71. placeImageURLs := make([]string, 0, len(placeImages))
  72. for i := range placeImages {
  73. placeImageURLs = append(placeImageURLs, placeImages[i].Image)
  74. if err = placeImageRepository.Delete(ctx, placeImages[i].ID, tx); err != nil {
  75. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  76. }
  77. }
  78. for i := range thingImages {
  79. if err = thingImageRepository.Delete(ctx, thingImages[i].ID, tx); err != nil {
  80. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  81. }
  82. }
  83. for _, thingID := range thingIDs {
  84. if err = placeThingRepository.DeleteThing(ctx, thingID, tx); err != nil {
  85. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  86. }
  87. if err = thingTagRepository.DeleteByThingID(ctx, thingID, tx); err != nil {
  88. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  89. }
  90. if err = thingNotificationRepository.Delete(ctx, thingID, tx); err != nil {
  91. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  92. }
  93. if err = thingRepository.Delete(ctx, thingID, tx); err != nil {
  94. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  95. }
  96. }
  97. if err = placeRepository.Delete(ctx, id, tx); err != nil {
  98. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  99. }
  100. if err = placeRepository.CommitTx(tx); err != nil {
  101. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  102. }
  103. if len(placeImageURLs) > 0 {
  104. for i := range placeImageURLs {
  105. if err = fileRepository.Delete(placeImageURLs[i]); err != nil {
  106. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  107. }
  108. }
  109. }
  110. for i := range thingImages {
  111. if err = fileRepository.Delete(thingImages[i].Image); err != nil {
  112. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  113. }
  114. }
  115. return fctx.JSON(factory.CreateEmptyResponse())
  116. }
  117. }