Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package data
  2.  
  3. import (
  4. "context"
  5. "time"
  6.  
  7. "bitbucket.org/jumotechnologies/apivideoanuncios/pkg/ad"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. )
  12.  
  13. // AdStorage storage to the ad model.
  14. type AdStorage struct {
  15. c *mongo.Collection
  16. storage *Storage
  17. }
  18.  
  19. // setContext initialize the context to AdStorage.
  20. func (s *AdStorage) setContext() {
  21. s.storage = New()
  22. s.c = s.storage.DBCollection(AdCollection)
  23. }
  24.  
  25. // Create create a new ad.
  26. func (s AdStorage) Create(a *ad.Ad) error {
  27. s.setContext()
  28. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  29. defer cancel()
  30.  
  31. a.CreatedAt = time.Now()
  32. a.UpdatedAt = time.Now()
  33. a.ID = primitive.NewObjectID()
  34.  
  35. _, err := s.c.InsertOne(ctx, a)
  36. if err != nil {
  37. return ErrCouldNotInsert
  38. }
  39.  
  40. return nil
  41. }
  42.  
  43. // Update update ad by ID.
  44. func (s AdStorage) Update(id string, a bson.M) error {
  45. s.setContext()
  46. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  47. defer cancel()
  48.  
  49. objectID, err := primitive.ObjectIDFromHex(id)
  50. if err != nil {
  51. return ErrCouldNotParseID
  52. }
  53.  
  54. delete(a, "userId")
  55. delete(a, "createdAt")
  56. delete(a, "id")
  57.  
  58. a["updatedAt"] = time.Now()
  59.  
  60. r := s.c.FindOneAndUpdate(ctx, bson.M{"_id": objectID}, bson.M{
  61. "$set": a,
  62. })
  63.  
  64. if r.Err() != nil {
  65. return ErrCouldNotUpdate
  66. }
  67.  
  68. return nil
  69. }
  70.  
  71. // Delete remove a ad by ID.
  72. func (s AdStorage) Delete(id string) error {
  73. s.setContext()
  74. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  75. defer cancel()
  76.  
  77. objectID, err := primitive.ObjectIDFromHex(id)
  78. if err != nil {
  79. return ErrCouldNotParseID
  80. }
  81.  
  82. _, err = s.c.DeleteOne(ctx, bson.M{"_id": objectID})
  83. if err != nil {
  84. return ErrCouldNotDelete
  85. }
  86.  
  87. return nil
  88. }
  89.  
  90. // GetAll returns all stored ads.
  91. func (s AdStorage) GetAll() (ad.Ads, error) {
  92. s.setContext()
  93. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  94. defer cancel()
  95.  
  96. ads := ad.Ads{}
  97. cursor, err := s.c.Find(ctx, bson.M{})
  98. if err != nil {
  99. return ad.Ads{}, ErrCouldNotFound
  100. }
  101.  
  102. defer cursor.Close(ctx)
  103.  
  104. for cursor.Next(ctx) {
  105. a := &ad.Ad{}
  106. cursor.Decode(a)
  107. ads = append(ads, *a)
  108. }
  109.  
  110. return ads, nil
  111. }
  112.  
  113. // GetByID returns a ad by ID.
  114. func (s AdStorage) GetByID(id string) (ad.Ad, error) {
  115. s.setContext()
  116. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  117. defer cancel()
  118.  
  119. objectID, err := primitive.ObjectIDFromHex(id)
  120. if err != nil {
  121. return ad.Ad{}, ErrCouldNotParseID
  122. }
  123.  
  124. a := ad.Ad{}
  125. err = s.c.FindOne(ctx, bson.M{"_id": objectID}).Decode(&a)
  126. if err != nil {
  127. return ad.Ad{}, ErrCouldNotFound
  128. }
  129.  
  130. return a, nil
  131. }
  132.  
  133. // GetByUserID returns a ad by UserID.
  134. func (s AdStorage) GetByUserID(uid string) (ad.Ads, error) {
  135. s.setContext()
  136. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  137. defer cancel()
  138.  
  139. ads := ad.Ads{}
  140. cursor, err := s.c.Find(ctx, bson.M{"userId": uid})
  141. if err != nil {
  142. return ad.Ads{}, ErrCouldNotFound
  143. }
  144.  
  145. defer cursor.Close(ctx)
  146.  
  147. for cursor.Next(ctx) {
  148. a := &ad.Ad{}
  149. cursor.Decode(a)
  150. ads = append(ads, *a)
  151. }
  152.  
  153. return ads, nil
  154. }
  155.  
  156. // GetByDistance returns stored users by distance.
  157. func (s AdStorage) GetByDistance(lat, long float64, distance int) (ad.Ads, error) {
  158. s.setContext()
  159. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  160. defer cancel()
  161. var us UserStorage
  162. users, err := us.GetByDistance(lat, long, distance)
  163. if err != nil {
  164. return []ad.Ad{}, ErrCouldNotFound
  165. }
  166.  
  167. uids := bson.A{}
  168. for _, user := range users {
  169. uids = append(uids, user.UID)
  170. }
  171.  
  172. ads := ad.Ads{}
  173. cursor, err := s.c.Find(ctx, bson.M{"userId": bson.M{"$in": uids}})
  174. if err != nil {
  175. return ad.Ads{}, ErrCouldNotFound
  176. }
  177.  
  178. defer cursor.Close(ctx)
  179.  
  180. for cursor.Next(ctx) {
  181. a := &ad.Ad{}
  182. err := cursor.Decode(a)
  183. if err != nil {
  184. continue
  185. }
  186. ads = append(ads, *a)
  187. }
  188.  
  189. return ads, nil
  190.  
  191. }
  192.  
  193. // SearchByText returns all stored ads by text match.
  194. func (s AdStorage) SearchByText(text string) ([]ad.Ad, error) {
  195. s.setContext()
  196. ctx, cancel := context.WithTimeout(s.storage.Context, 10*time.Second)
  197. defer cancel()
  198.  
  199. ads := []ad.Ad{}
  200.  
  201. cursor, err := s.c.Find(ctx, bson.M{
  202. "$text": bson.M{"$search": text},
  203. })
  204. if err != nil {
  205. return []ad.Ad{}, ErrCouldNotFound
  206. }
  207.  
  208. defer cursor.Close(ctx)
  209.  
  210. a := ad.Ad{}
  211. for cursor.Next(ctx) {
  212. cursor.Decode(&a)
  213. ads = append(ads, a)
  214. }
  215.  
  216. return ads, nil
  217. }
  218.  
  219. // WithPagination returns all ads with a pagination limit.
  220. func (s AdStorage) WithPagination(ads []ad.Ad, page, limit int) ([]ad.Ad, int, error) {
  221. total := len(ads)
  222. if limit > total {
  223. limit = total
  224. }
  225. start := (page - 1) * limit
  226.  
  227. ads = ads[start:limit]
  228.  
  229. return ads, total, nil
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement