delete_thing.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package thing
  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. "github.com/gofiber/fiber/v2"
  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(sp interfaces.ServiceProvider) 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. if _, err = sp.GetThingRepository().Get(ctx, id); err != nil {
  27. if err == sql.ErrNoRows {
  28. return fiber.NewError(fiber.StatusBadRequest, "")
  29. }
  30. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  31. }
  32. tx, err := sp.GetThingRepository().BeginTx(ctx, API.DefaultTxLevel)
  33. if err != nil {
  34. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  35. }
  36. if err = sp.GetPlaceThingRepository().DeleteThing(ctx, id, tx); err != nil {
  37. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  38. }
  39. images, err := sp.GetThingImageRepository().GetByThingID(ctx, id)
  40. if err != nil {
  41. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  42. }
  43. imageURLs := make([]string, 0, len(images))
  44. for i := range images {
  45. imageURLs = append(imageURLs, images[i].Image)
  46. if err = sp.GetThingImageRepository().Delete(ctx, images[i].ID, tx); err != nil {
  47. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  48. }
  49. }
  50. if err = sp.GetThingTagRepository().DeleteByThingID(ctx, id, tx); err != nil {
  51. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  52. }
  53. if err = sp.GetThingNotificationRepository().Delete(ctx, id, tx); err != nil {
  54. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  55. }
  56. if err = sp.GetThingRepository().Delete(ctx, id, tx); err != nil {
  57. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  58. }
  59. if err = sp.GetThingRepository().CommitTx(tx); err != nil {
  60. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  61. }
  62. if len(imageURLs) > 0 {
  63. for i := range imageURLs {
  64. if err = sp.GetFileRepository().Delete(imageURLs[i]); err != nil {
  65. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  66. }
  67. }
  68. }
  69. return fctx.JSON(factory.CreateEmptyResponse())
  70. }
  71. }