/
rooms.go
146 lines (121 loc) · 3.44 KB
/
rooms.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
package repository
import (
"errors"
"reflect"
"strings"
log "github.com/sirupsen/logrus"
"github.com/talesmud/talesmud/pkg/db"
r "github.com/talesmud/talesmud/pkg/entities/rooms"
)
//--- Interface Definitions
//RoomsRepository repository interface
type RoomsRepository interface {
FindAll() ([]*r.Room, error)
FindAllWithQuery(query RoomsQuery) ([]*r.Room, error)
FindByID(id string) (*r.Room, error)
FindByName(name string) ([]*r.Room, error)
Store(room *r.Room) (*r.Room, error)
Import(room *r.Room) (*r.Room, error)
Update(id string, room *r.Room) error
Delete(id string) error
Drop() error
}
//RoomsQuery ...
type RoomsQuery struct {
Name string `form:"name"`
Description string `form:"description"`
Detail string `form:"detail"`
RoomType string `form:"roomType"`
Area string `form:"area"`
AreaType string `form:"areaType"`
}
//--- Implementations
type roomsRepository struct {
*GenericRepo
}
// Drop ...
func (repo *roomsRepository) Drop() error {
return repo.GenericRepo.DropCollection()
}
//NewMongoDBRoomsRepository creates a new mongodb repoRepository
func NewMongoDBRoomsRepository(db *db.Client) RoomsRepository {
r := &roomsRepository{
GenericRepo: &GenericRepo{
db: db,
collection: "rooms",
generator: func() interface{} {
return &r.Room{}
},
},
}
r.CreateIndex()
return r
}
func (repo *roomsRepository) FindByID(id string) (*r.Room, error) {
if id == "" {
log.Error("Rooms::FindByID - id is empty")
return nil, errors.New("Empty id")
}
result, err := repo.GenericRepo.FindByID(id)
if err == nil {
return result.(*r.Room), nil
}
return nil, err
}
func (repo *roomsRepository) FindByName(name string) ([]*r.Room, error) {
results := make([]*r.Room, 0)
if err := repo.GenericRepo.FindAllWithParam(
db.NewQueryParams(db.QueryParam{Key: "name", Value: name}),
func(elem interface{}) {
results = append(results, elem.(*r.Room))
}); err != nil {
return nil, err
}
return results, nil
}
func (repo *roomsRepository) FindAll() ([]*r.Room, error) {
results := make([]*r.Room, 0)
if err := repo.GenericRepo.FindAll(func(elem interface{}) {
results = append(results, elem.(*r.Room))
}); err != nil {
return nil, err
}
return results, nil
}
func (repo *roomsRepository) FindAllWithQuery(query RoomsQuery) ([]*r.Room, error) {
results := make([]*r.Room, 0)
params := []db.QueryParam{}
// get all field names and string values and add to prams slice
v := reflect.ValueOf(query)
for i := 0; i < v.NumField(); i++ {
if v.Field(i).Interface() != nil {
p := db.QueryParam{Key: strings.ToLower(v.Type().Field(i).Name), Value: v.Field(i).Interface()}
if p.Value != "" {
params = append(params, p)
}
}
}
if e := repo.GenericRepo.FindAllWithParam(db.NewQueryParams(params...), func(elem interface{}) {
results = append(results, elem.(*r.Room))
}); e != nil {
return results, nil
}
return results, nil
}
func (repo *roomsRepository) Update(id string, charachterSheet *r.Room) error {
return repo.GenericRepo.Update(charachterSheet, id)
}
func (repo *roomsRepository) Delete(id string) error {
return repo.GenericRepo.Delete(id)
}
func (repo *roomsRepository) Store(rep *r.Room) (*r.Room, error) {
//rep.Entity = e.NewEntity()
return repo.Import(rep)
}
func (repo *roomsRepository) Import(rep *r.Room) (*r.Room, error) {
if result, err := repo.GenericRepo.Store(rep); err == nil {
return result.(*r.Room), nil
} else {
return result.(*r.Room), nil
}
}