delete_place.go 4.2 KB

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