/
items.go
157 lines (130 loc) · 4.09 KB
/
items.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package repository
import (
"errors"
"strings"
log "github.com/sirupsen/logrus"
"github.com/talesmud/talesmud/pkg/db"
"github.com/talesmud/talesmud/pkg/entities"
i "github.com/talesmud/talesmud/pkg/entities/items"
)
//--- Interface Definitions
//ItemsRepository repository interface
type ItemsRepository interface {
FindAll(query ItemsQuery) ([]*i.Item, error)
FindByID(id string) (*i.Item, error)
FindByName(name string) ([]*i.Item, error)
Store(item *i.Item) (*i.Item, error)
Import(item *i.Item) (*i.Item, error)
Update(id string, item *i.Item) error
Delete(id string) error
Drop() error
}
//ItemsQuery ...
type ItemsQuery struct {
Name *string `form:"name"`
Description *string `form:"description"`
Detail *string `form:"detail"`
Type *i.ItemType `form:"type"`
SubType *i.ItemSubType `form:"subType"`
Quality *i.ItemQuality `form:"quality"`
Slot *i.ItemSlot `form:"slot"`
Level *int32 `form:"level"`
}
func (query ItemsQuery) matches(item *i.Item) bool {
match := true
if query.Name != nil && !strings.Contains(strings.ToLower(item.Name), strings.ToLower(*query.Name)) {
match = false
}
if match && query.Description != nil && !strings.Contains(strings.ToLower(item.Description), strings.ToLower(*query.Description)) {
match = false
}
if match && query.Detail != nil && !strings.Contains(strings.ToLower(item.Detail), strings.ToLower(*query.Detail)) {
match = false
}
if match && query.Type != nil && !strings.Contains(strings.ToLower(string(item.Type)), strings.ToLower(string(*query.Type))) {
match = false
}
if match && query.Slot != nil && !strings.Contains(strings.ToLower(string(item.Slot)), strings.ToLower(string(*query.Slot))) {
match = false
}
if match && query.SubType != nil && !strings.Contains(strings.ToLower(string(item.SubType)), strings.ToLower(string(*query.SubType))) {
match = false
}
if match && query.Quality != nil && !strings.Contains(strings.ToLower(string(item.Quality)), strings.ToLower(string(*query.Quality))) {
match = false
}
if match && query.Level != nil && item.Level != *query.Level {
match = false
}
return match
}
//--- Implementations
type itemsRepository struct {
*GenericRepo
}
//NewMongoDBItemsRepository creates a new mongodb charactersRepository
func NewMongoDBItemsRepository(db *db.Client) ItemsRepository {
// create index on id
//db.members.createIndex( { "user_id": 1 }, { unique: true } )
ir := &itemsRepository{
GenericRepo: &GenericRepo{
db: db,
collection: "items",
generator: func() interface{} {
return &i.Item{}
},
},
}
ir.CreateIndex()
return ir
}
// Drop ...
func (repo *itemsRepository) Drop() error {
return repo.GenericRepo.DropCollection()
}
func (repo *itemsRepository) FindByID(id string) (*i.Item, error) {
if id == "" {
log.Error("Items::FindByID - id is empty")
return nil, errors.New("Empty id")
}
result, err := repo.GenericRepo.FindByID(id)
if err == nil {
return result.(*i.Item), nil
}
return nil, err
}
func (repo *itemsRepository) FindByName(name string) ([]*i.Item, error) {
results := make([]*i.Item, 0)
repo.GenericRepo.FindAllWithParam(
db.NewQueryParams(db.QueryParam{Key: "name", Value: name}),
func(elem interface{}) {
results = append(results, elem.(*i.Item))
})
return results, nil
}
func (repo *itemsRepository) FindAll(query ItemsQuery) ([]*i.Item, error) {
results := make([]*i.Item, 0)
if err := repo.GenericRepo.FindAll(func(elem interface{}) {
results = append(results, elem.(*i.Item))
}); err != nil {
return nil, err
}
return results, nil
}
func (repo *itemsRepository) Update(id string, item *i.Item) error {
return repo.GenericRepo.Update(item, id)
}
func (repo *itemsRepository) Delete(id string) error {
return repo.GenericRepo.Delete(id)
}
func (repo *itemsRepository) Store(item *i.Item) (*i.Item, error) {
item.Entity = entities.NewEntity()
return repo.Import(item)
}
func (repo *itemsRepository) Import(item *i.Item) (*i.Item, error) {
result, err := repo.GenericRepo.Store(item)
if result == nil {
return nil, err
}
return result.(*i.Item), nil
}