/
category_service.go
98 lines (85 loc) · 2.98 KB
/
category_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package services
import (
"context"
"errors"
"github.com/cristovaoolegario/aluraflix-api/internal/pkg/http/dto"
"github.com/cristovaoolegario/aluraflix-api/internal/pkg/storage/bson/db/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type CategoryService struct {
categoryCollection *mongo.Collection
videosCollection *mongo.Collection
}
func ProvideCategoryService(database DatabaseService) CategoryService {
return CategoryService{database.Collection(CategoriesCollection), database.Collection(VideoCollection)}
}
func (cs *CategoryService) GetAll(filter string, page int64, pageSize int64) ([]models.Category, error) {
collectionFilter, findOptions := makeFindOptions(filter, page, pageSize)
var Categories []models.Category
cursor, err := cs.categoryCollection.Find(context.TODO(), collectionFilter, findOptions)
if err != nil {
return nil, err
}
_ = cursor.All(context.TODO(), &Categories)
return Categories, err
}
func (cs *CategoryService) GetById(id primitive.ObjectID) (*models.Category, error) {
category := models.Category{}
if err := cs.categoryCollection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(&category); err != nil {
return nil, err
}
return &category, nil
}
func (cs *CategoryService) Create(insertCategory dto.InsertCategory) (*models.Category, error) {
convertedCategory := insertCategory.ConvertToCategory()
_, err := cs.categoryCollection.InsertOne(context.TODO(), &convertedCategory)
if err != nil {
return nil, err
}
return &convertedCategory, nil
}
func (cs *CategoryService) Update(id primitive.ObjectID, newData dto.InsertCategory) (*models.Category, error) {
var category *models.Category
if err := cs.categoryCollection.FindOneAndUpdate(
context.Background(),
bson.D{
primitive.E{Key: "_id", Value: id},
},
bson.D{primitive.E{Key: "$set", Value: newData}},
options.FindOneAndUpdate().SetReturnDocument(1),
).Decode(&category); err != nil {
return nil, err
}
return category, nil
}
func (cs *CategoryService) Delete(id primitive.ObjectID) error {
result, err := cs.categoryCollection.DeleteOne(context.TODO(), bson.M{"_id": id})
if result.DeletedCount == 0 {
return errors.New("no document deleted")
}
return err
}
func (cs *CategoryService) GetVideosByCategoryId(id primitive.ObjectID) ([]models.Video, error) {
var videos []models.Video
cursor, err := cs.videosCollection.Find(context.TODO(), bson.M{"category_id": id})
if err != nil {
return nil, err
}
_ = cursor.All(context.TODO(), &videos)
return videos, err
}
func (cs *CategoryService) GetFreeCategory() *models.Category {
category := models.Category{}
if err := cs.categoryCollection.FindOne(context.TODO(), bson.M{"titulo": "FREE"}).Decode(&category); err != nil {
category = *models.GetFreeCategory()
_, err := cs.categoryCollection.InsertOne(context.TODO(), &category)
if err != nil {
return nil
}
return &category
}
return &category
}