places.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package services
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/models"
  11. )
  12. func encodeCursor(p *models.Place) string {
  13. s := p.ID + "|" + strconv.FormatInt(p.CreatedAt.UnixNano(), 10) + "|" + fmt.Sprintf("%.6f", p.Rating)
  14. return base64.RawURLEncoding.EncodeToString([]byte(s))
  15. }
  16. func DecodeCursor(cursor string) (id string, createdAt time.Time, rating float64, err error) {
  17. b, err := base64.RawURLEncoding.DecodeString(cursor)
  18. if err != nil {
  19. return "", time.Time{}, 0, fmt.Errorf("decode cursor: %w", err)
  20. }
  21. parts := strings.SplitN(string(b), "|", 3)
  22. if len(parts) < 2 {
  23. return "", time.Time{}, 0, fmt.Errorf("invalid cursor format")
  24. }
  25. id = parts[0]
  26. createdAtUnix, err := strconv.ParseInt(parts[1], 10, 64)
  27. if err != nil {
  28. return "", time.Time{}, 0, fmt.Errorf("parse cursor created_at: %w", err)
  29. }
  30. createdAt = time.Unix(0, createdAtUnix)
  31. if len(parts) > 2 {
  32. rating, _ = strconv.ParseFloat(parts[2], 64)
  33. }
  34. return
  35. }
  36. type ObjectStorager interface {
  37. DeleteObjects(ctx context.Context, urls []string) error
  38. }
  39. var (
  40. ErrNotYourPlace = errors.New("not your place")
  41. )
  42. type PlaceRepo interface {
  43. Create(ctx context.Context, place *models.Place) error
  44. GetByID(ctx context.Context, id string) (*models.Place, error)
  45. GetByIDRaw(ctx context.Context, id string) (*models.Place, error)
  46. List(ctx context.Context, filter models.PlaceFilter) ([]*models.Place, error)
  47. Update(ctx context.Context, place *models.Place) error
  48. UpdateStatus(ctx context.Context, id, status, comment string) error
  49. SoftDelete(ctx context.Context, id string) error
  50. HardDelete(ctx context.Context, id string) error
  51. GetPlaceImages(ctx context.Context, placeID string) ([]models.PlaceImage, error)
  52. SetTags(ctx context.Context, placeID string, tags []models.Tag) error
  53. SetFeatures(ctx context.Context, placeID string, features []models.Feature) error
  54. GetTags(ctx context.Context, placeID string) ([]models.Tag, error)
  55. GetFeatures(ctx context.Context, placeID string) ([]models.Feature, error)
  56. GetTagsBatch(ctx context.Context, placeIDs []string) (map[string][]models.Tag, error)
  57. GetFeaturesBatch(ctx context.Context, placeIDs []string) (map[string][]models.Feature, error)
  58. }
  59. type PlaceService struct {
  60. placeRepo PlaceRepo
  61. storage ObjectStorager
  62. }
  63. func NewPlaceService(placeRepo PlaceRepo, storage ObjectStorager) *PlaceService {
  64. return &PlaceService{placeRepo: placeRepo, storage: storage}
  65. }
  66. type CreatePlaceInput struct {
  67. OwnerID string
  68. Type string
  69. Title string
  70. Description *string
  71. Address *string
  72. Lat float64
  73. Lng float64
  74. CoverImage *string
  75. AccessInfo *string
  76. HourlyRate *int
  77. Currency string
  78. MinHours int
  79. Tags []models.Tag
  80. Features []models.Feature
  81. Status string
  82. }
  83. func (s *PlaceService) Create(ctx context.Context, input CreatePlaceInput) (*models.Place, error) {
  84. status := input.Status
  85. if status == "" {
  86. status = "pending_moderation"
  87. }
  88. place := &models.Place{
  89. Type: input.Type,
  90. OwnerID: input.OwnerID,
  91. Title: input.Title,
  92. Description: input.Description,
  93. Address: input.Address,
  94. Lat: input.Lat,
  95. Lng: input.Lng,
  96. CoverImage: input.CoverImage,
  97. AccessInfo: input.AccessInfo,
  98. Status: status,
  99. HourlyRate: input.HourlyRate,
  100. Currency: input.Currency,
  101. MinHours: input.MinHours,
  102. Tags: input.Tags,
  103. Features: input.Features,
  104. }
  105. if err := s.placeRepo.Create(ctx, place); err != nil {
  106. return nil, fmt.Errorf("create place: %w", err)
  107. }
  108. return place, nil
  109. }
  110. func (s *PlaceService) GetByID(ctx context.Context, id string, fetchTags bool) (*models.Place, error) {
  111. place, err := s.placeRepo.GetByID(ctx, id)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if place == nil {
  116. return nil, nil
  117. }
  118. if fetchTags {
  119. tags, err := s.placeRepo.GetTags(ctx, id)
  120. if err != nil {
  121. return nil, err
  122. }
  123. place.Tags = tags
  124. features, err := s.placeRepo.GetFeatures(ctx, id)
  125. if err != nil {
  126. return nil, err
  127. }
  128. place.Features = features
  129. }
  130. return place, nil
  131. }
  132. func (s *PlaceService) List(ctx context.Context, filter models.PlaceFilter) (*models.PaginatedPlaces, error) {
  133. places, err := s.placeRepo.List(ctx, filter)
  134. if err != nil {
  135. return nil, err
  136. }
  137. if filter.IncludeTagsFeatures && len(places) > 0 {
  138. placeIDs := make([]string, len(places))
  139. for i, p := range places {
  140. placeIDs[i] = p.ID
  141. }
  142. tagsMap, err := s.placeRepo.GetTagsBatch(ctx, placeIDs)
  143. if err != nil {
  144. return nil, err
  145. }
  146. featuresMap, err := s.placeRepo.GetFeaturesBatch(ctx, placeIDs)
  147. if err != nil {
  148. return nil, err
  149. }
  150. for _, p := range places {
  151. if tags, ok := tagsMap[p.ID]; ok {
  152. p.Tags = tags
  153. }
  154. if features, ok := featuresMap[p.ID]; ok {
  155. p.Features = features
  156. }
  157. }
  158. }
  159. limit := filter.Limit()
  160. hasMore := len(places) > limit
  161. if hasMore {
  162. places = places[:limit]
  163. }
  164. var nextCursor string
  165. if hasMore && len(places) > 0 {
  166. nextCursor = encodeCursor(places[len(places)-1])
  167. }
  168. return &models.PaginatedPlaces{
  169. Data: places,
  170. NextCursor: nextCursor,
  171. HasMore: hasMore,
  172. }, nil
  173. }
  174. type UpdatePlaceInput struct {
  175. ID string
  176. OwnerID string
  177. Title *string
  178. Description *string
  179. Address *string
  180. Lat *float64
  181. Lng *float64
  182. CoverImage *string
  183. AccessInfo *string
  184. HourlyRate *int
  185. Currency *string
  186. MinHours *int
  187. Tags []models.Tag
  188. Features []models.Feature
  189. }
  190. func (s *PlaceService) Update(ctx context.Context, input UpdatePlaceInput, isModerator bool) (*models.Place, error) {
  191. place, err := s.placeRepo.GetByID(ctx, input.ID)
  192. if err != nil {
  193. return nil, err
  194. }
  195. if place == nil {
  196. return nil, nil
  197. }
  198. if !isModerator && place.OwnerID != input.OwnerID {
  199. return nil, fmt.Errorf("%w: user %s tried to update place %s", ErrNotYourPlace, input.OwnerID, input.ID)
  200. }
  201. if input.Title != nil { place.Title = *input.Title }
  202. if input.Description != nil { place.Description = input.Description }
  203. if input.Address != nil { place.Address = input.Address }
  204. if input.Lat != nil { place.Lat = *input.Lat }
  205. if input.Lng != nil { place.Lng = *input.Lng }
  206. if input.CoverImage != nil { place.CoverImage = input.CoverImage }
  207. if input.AccessInfo != nil { place.AccessInfo = input.AccessInfo }
  208. if input.HourlyRate != nil { place.HourlyRate = input.HourlyRate }
  209. if input.Currency != nil { place.Currency = *input.Currency }
  210. if input.MinHours != nil { place.MinHours = *input.MinHours }
  211. if !isModerator {
  212. place.Status = "pending_moderation"
  213. }
  214. if err := s.placeRepo.Update(ctx, place); err != nil {
  215. return nil, err
  216. }
  217. if input.Tags != nil {
  218. if err := s.placeRepo.SetTags(ctx, place.ID, input.Tags); err != nil {
  219. return nil, err
  220. }
  221. place.Tags = input.Tags
  222. }
  223. if input.Features != nil {
  224. if err := s.placeRepo.SetFeatures(ctx, place.ID, input.Features); err != nil {
  225. return nil, err
  226. }
  227. place.Features = input.Features
  228. }
  229. return place, nil
  230. }
  231. func (s *PlaceService) Moderate(ctx context.Context, id, action, comment, moderatorID string) error {
  232. var status string
  233. switch action {
  234. case "approve":
  235. status = "published"
  236. case "reject":
  237. status = "rejected"
  238. case "rework":
  239. status = "revision"
  240. case "revoke":
  241. status = "pending_moderation"
  242. default:
  243. return fmt.Errorf("unknown action: %s", action)
  244. }
  245. return s.placeRepo.UpdateStatus(ctx, id, status, comment)
  246. }
  247. func (s *PlaceService) Delete(ctx context.Context, id string) error {
  248. return s.placeRepo.SoftDelete(ctx, id)
  249. }
  250. func (s *PlaceService) HardDelete(ctx context.Context, id string) error {
  251. place, err := s.placeRepo.GetByIDRaw(ctx, id)
  252. if err != nil {
  253. return err
  254. }
  255. if place == nil {
  256. return nil
  257. }
  258. var urls []string
  259. if place.CoverImage != nil {
  260. urls = append(urls, *place.CoverImage)
  261. }
  262. images, err := s.placeRepo.GetPlaceImages(ctx, id)
  263. if err != nil {
  264. return err
  265. }
  266. for _, img := range images {
  267. urls = append(urls, img.URL)
  268. }
  269. if len(urls) > 0 {
  270. if err := s.storage.DeleteObjects(ctx, urls); err != nil {
  271. return fmt.Errorf("delete place files: %w", err)
  272. }
  273. }
  274. return s.placeRepo.HardDelete(ctx, id)
  275. }