add_thing.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package thing
  2. //go:generate mkdir -p mocks
  3. //go:generate rm -rf ./mocks/*_minimock.go
  4. //go:generate minimock -i ThingRepository,PlaceThingRepository,ThingTagRepository,ThingImageRepository,ThingNotificationRepository,FileRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  14. "github.com/go-playground/validator/v10"
  15. "github.com/gofiber/fiber/v2"
  16. )
  17. type (
  18. ThingRepository interface {
  19. Get(ctx context.Context, thingID int) (*models.Thing, error)
  20. Search(ctx context.Context, search string) ([]models.Thing, error)
  21. GetByPlaceID(ctx context.Context, placeID int) ([]models.Thing, error)
  22. GetAllByPlaceID(ctx context.Context, placeID int) ([]models.Thing, error)
  23. Add(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) (int, error)
  24. Update(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) error
  25. Delete(ctx context.Context, thingID int, tx *sql.Tx) error
  26. BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error)
  27. CommitTx(tx *sql.Tx) error
  28. }
  29. PlaceThingRepository interface {
  30. Add(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) error
  31. GetByThingID(ctx context.Context, thingID int) (*models.PlaceThing, error)
  32. UpdatePlace(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) error
  33. DeleteThing(ctx context.Context, thingID int, tx *sql.Tx) error
  34. }
  35. ThingTagRepository interface {
  36. GetByPlaceID(ctx context.Context, placeID int) ([]models.ThingTag, error)
  37. DeleteByThingID(ctx context.Context, thingID int, tx *sql.Tx) error
  38. }
  39. ThingImageRepository interface {
  40. GetByThingID(ctx context.Context, thingID int) ([]models.Image, error)
  41. Delete(ctx context.Context, imageID int, tx *sql.Tx) error
  42. }
  43. ThingNotificationRepository interface {
  44. Delete(ctx context.Context, thingID int, tx *sql.Tx) error
  45. }
  46. FileRepository interface {
  47. Delete(path string) error
  48. }
  49. )
  50. // @Router /api/v1/things [post]
  51. // @Param data body dto.AddThingRequest true "Request body"
  52. // @Success 200 {object} dto.ThingResponse
  53. // @Failure 400 {object} dto.ErrorResponse
  54. // @Failure 500 {object} dto.ErrorResponse
  55. // @Summary Add thing
  56. // @Tags Things
  57. // @security APIKey
  58. // @Accept json
  59. // @Produce json
  60. func AddThingHandler(
  61. thingRepository ThingRepository,
  62. placeThingRepository PlaceThingRepository,
  63. ) fiber.Handler {
  64. return func(fctx *fiber.Ctx) error {
  65. ctx := fctx.Context()
  66. req := dto.AddThingRequest{}
  67. if err := fctx.BodyParser(&req); err != nil {
  68. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  69. }
  70. var validate = validator.New()
  71. if err := validate.Struct(req); err != nil {
  72. return fctx.Status(fiber.StatusBadRequest).JSON(factory.CreateValidateErrorResponse(err))
  73. }
  74. tx, err := thingRepository.BeginTx(ctx, API.DefaultTxLevel)
  75. if err != nil {
  76. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  77. }
  78. id, err := thingRepository.Add(ctx, mappers.ToAddThingRequest(req), tx)
  79. if err != nil {
  80. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  81. }
  82. err = placeThingRepository.Add(ctx, mappers.ToAddPlaceThingRequest(id, req.PlaceID), tx)
  83. if err != nil {
  84. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  85. }
  86. if err = thingRepository.CommitTx(tx); err != nil {
  87. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  88. }
  89. res, err := thingRepository.Get(ctx, id)
  90. if err != nil {
  91. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  92. }
  93. res = helpers.ApplyLocation(fctx, res)
  94. return fctx.JSON(mappers.ToThingResponse(*res))
  95. }
  96. }