add_place.go 3.3 KB

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