add_place.go 3.3 KB

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