This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
category.go
92 lines (74 loc) · 2.68 KB
/
category.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
//go:generate mockgen -destination=./mock/category_mock.go github.com/nomkhonwaan/myblog/pkg/blog CategoryRepository
package blog
import (
"context"
"encoding/json"
"github.com/nomkhonwaan/myblog/pkg/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Category is a group of posts regarded as having particular shared characteristics
type Category struct {
// Identifier of the category
ID primitive.ObjectID `bson:"_id" json:"id" graphql:"-"`
// Name of the category
Name string `bson:"name" json:"name" graphql:"name"`
// Valid URL string composes with name and ID
Slug string `bson:"slug" json:"slug" graphql:"slug"`
}
// MarshalJSON is a custom JSON marshaling function of category entity
func (cat Category) MarshalJSON() ([]byte, error) {
type Alias Category
return json.Marshal(&struct {
ID string `json:"id"`
*Alias
}{
ID: cat.ID.Hex(),
Alias: (*Alias)(&cat),
})
}
// A CategoryRepository interface
type CategoryRepository interface {
FindAll(ctx context.Context) ([]Category, error)
FindAllByIDs(ctx context.Context, ids interface{}) ([]Category, error)
FindByID(ctx context.Context, id interface{}) (Category, error)
}
// NewCategoryRepository returns a MongoCategoryRepository instance
func NewCategoryRepository(db mongo.Database) MongoCategoryRepository {
return MongoCategoryRepository{col: mongo.NewCollection(db.Collection("categories"))}
}
// MongoCategoryRepository implements CategoryRepository interface
type MongoCategoryRepository struct{ col mongo.Collection }
// FindAll returns list of categories
func (repo MongoCategoryRepository) FindAll(ctx context.Context) ([]Category, error) {
opts := options.Find().SetSort(bson.D{{"name", 1}})
cur, err := repo.col.Find(ctx, bson.D{}, opts)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
var categories []Category
err = cur.Decode(&categories)
return categories, err
}
// FindAllByIDs returns list of categories from list of IDs
func (repo MongoCategoryRepository) FindAllByIDs(ctx context.Context, ids interface{}) ([]Category, error) {
filter := bson.M{"_id": bson.M{"$in": ids.([]primitive.ObjectID)}}
opts := options.Find().SetSort(bson.D{{"name", 1}})
cur, err := repo.col.Find(ctx, filter, opts)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
var categories []Category
err = cur.Decode(&categories)
return categories, err
}
// FindByID returns a single category from its ID
func (repo MongoCategoryRepository) FindByID(ctx context.Context, id interface{}) (Category, error) {
r := repo.col.FindOne(ctx, bson.M{"_id": id.(primitive.ObjectID)})
var cat Category
err := r.Decode(&cat)
return cat, err
}