add_image.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package image
  2. //go:generate mkdir -p mocks
  3. //go:generate rm -rf ./mocks/*_minimock.go
  4. //go:generate minimock -i FileRepository,ThingImageRepository,PlaceImageRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. "mime/multipart"
  9. "path/filepath"
  10. "strconv"
  11. "time"
  12. "github.com/gofiber/fiber/v2"
  13. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. const fileDateLayout = "2006-01-02-15-04-05"
  20. type (
  21. FileRepository interface {
  22. Save(fctx *fiber.Ctx, header *multipart.FileHeader, path string) error
  23. Delete(path string) error
  24. }
  25. ThingImageRepository interface {
  26. Add(ctx context.Context, req models.AddThingImageRequest, tx *sql.Tx) error
  27. Get(ctx context.Context, imageID int) (*models.Image, error)
  28. GetByThingID(ctx context.Context, thingID int) ([]models.Image, error)
  29. GetByPlaceID(ctx context.Context, placeID int) ([]models.Image, error)
  30. Delete(ctx context.Context, imageID int, tx *sql.Tx) error
  31. BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error)
  32. CommitTx(tx *sql.Tx) error
  33. }
  34. PlaceImageRepository interface {
  35. Add(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) error
  36. Get(ctx context.Context, imageID int) (*models.Image, error)
  37. GetByPlaceID(ctx context.Context, placeID int) ([]models.Image, error)
  38. Delete(ctx context.Context, imageID int, tx *sql.Tx) error
  39. BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error)
  40. CommitTx(tx *sql.Tx) error
  41. }
  42. )
  43. // @Router /api/v1/images [post]
  44. // @Param place_id formData int false "Place ID"
  45. // @Param thing_id formData int false "Thing ID"
  46. // @Param files formData []file true "Files"
  47. // @Success 200 {object} dto.EmptyResponse
  48. // @Failure 400 {object} dto.ErrorResponse
  49. // @Failure 500 {object} dto.ErrorResponse
  50. // @Summary Add images
  51. // @Tags Images
  52. // @security APIKey
  53. // @Accept mpfd
  54. // @Produce json
  55. func AddImageHandler(
  56. fileRepository FileRepository,
  57. thingImageRepository ThingImageRepository,
  58. placeImageRepository PlaceImageRepository,
  59. ) fiber.Handler {
  60. return func(fctx *fiber.Ctx) error {
  61. var form *multipart.Form
  62. var placeID, thingID int
  63. var files []string
  64. var err error
  65. ctx := fctx.Context()
  66. if form, err = fctx.MultipartForm(); err != nil {
  67. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  68. }
  69. if formPlace := form.Value["place_id"]; len(formPlace) > 0 {
  70. placeID, err = strconv.Atoi(formPlace[0])
  71. if err != nil {
  72. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  73. }
  74. }
  75. if formThing := form.Value["thing_id"]; len(formThing) > 0 {
  76. thingID, err = strconv.Atoi(formThing[0])
  77. if err != nil {
  78. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  79. }
  80. }
  81. date := time.Now().Format(fileDateLayout)
  82. for _, file := range form.File["files"] {
  83. filename := "/files/" + date + "_" + helpers.GenerateRandomString(10) + filepath.Ext(file.Filename)
  84. if err = fileRepository.Save(fctx, file, filename); err != nil {
  85. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  86. }
  87. files = append(files, filename)
  88. }
  89. if (placeID == 0 && thingID == 0) || len(files) == 0 {
  90. return fiber.NewError(fiber.StatusBadRequest, "")
  91. }
  92. var tx *sql.Tx
  93. if thingID > 0 {
  94. tx, err = thingImageRepository.BeginTx(ctx, API.DefaultTxLevel)
  95. if err != nil {
  96. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  97. }
  98. for _, file := range files {
  99. if err = thingImageRepository.Add(ctx, mappers.ToAddThingImageRequest(thingID, file), tx); err != nil {
  100. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  101. }
  102. }
  103. if err = thingImageRepository.CommitTx(tx); err != nil {
  104. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  105. }
  106. } else {
  107. tx, err = placeImageRepository.BeginTx(ctx, API.DefaultTxLevel)
  108. if err != nil {
  109. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  110. }
  111. for _, file := range files {
  112. if err = placeImageRepository.Add(ctx, mappers.ToAddPlaceImageRequest(placeID, file), tx); err != nil {
  113. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  114. }
  115. }
  116. if err = placeImageRepository.CommitTx(tx); err != nil {
  117. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  118. }
  119. }
  120. return fctx.JSON(factory.CreateEmptyResponse())
  121. }
  122. }