384 lines
8.0 KiB
Go
384 lines
8.0 KiB
Go
|
package repo
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
|
||
|
"git.icod.de/dalu/refdata/model"
|
||
|
"github.com/globalsign/mgo"
|
||
|
"github.com/globalsign/mgo/bson"
|
||
|
)
|
||
|
|
||
|
//
|
||
|
type DataIdQuery struct {
|
||
|
Id string
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
type DataNameQuery struct {
|
||
|
Operation string
|
||
|
Query string
|
||
|
Options string
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
type DataAddressQuery struct {
|
||
|
Operation string
|
||
|
Query string
|
||
|
Options string
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
type DataLocationQuery struct {
|
||
|
Operation string
|
||
|
Query string
|
||
|
Options string
|
||
|
}
|
||
|
|
||
|
// int
|
||
|
type DataAgeQuery struct {
|
||
|
Operation string
|
||
|
From int64
|
||
|
To int64
|
||
|
}
|
||
|
|
||
|
// float64
|
||
|
type DataPriceQuery struct {
|
||
|
Operation string
|
||
|
From float64
|
||
|
To float64
|
||
|
}
|
||
|
|
||
|
type DataListParams struct {
|
||
|
Start int
|
||
|
Limit int
|
||
|
Sort string
|
||
|
|
||
|
Name bool
|
||
|
Address bool
|
||
|
Location bool
|
||
|
Age bool
|
||
|
Price bool
|
||
|
|
||
|
NameQuery DataNameQuery
|
||
|
AddressQuery DataAddressQuery
|
||
|
LocationQuery DataLocationQuery
|
||
|
AgeQuery DataAgeQuery
|
||
|
PriceQuery DataPriceQuery
|
||
|
}
|
||
|
|
||
|
type DataGetParams struct {
|
||
|
Id bool
|
||
|
Name bool
|
||
|
Address bool
|
||
|
Location bool
|
||
|
Age bool
|
||
|
Price bool
|
||
|
|
||
|
IdQuery DataIdQuery
|
||
|
NameQuery DataNameQuery
|
||
|
AddressQuery DataAddressQuery
|
||
|
LocationQuery DataLocationQuery
|
||
|
AgeQuery DataAgeQuery
|
||
|
PriceQuery DataPriceQuery
|
||
|
}
|
||
|
|
||
|
type DataRepository struct {
|
||
|
ms *mgo.Session
|
||
|
|
||
|
database string
|
||
|
collection string
|
||
|
}
|
||
|
|
||
|
func NewDataRepository(ms *mgo.Session, db, c string) *DataRepository {
|
||
|
r := new(DataRepository)
|
||
|
r.ms = ms.Clone()
|
||
|
r.database = db
|
||
|
r.collection = c
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) Close() {
|
||
|
r.ms.Close()
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) List(p *DataListParams) ([]*model.Data, error) {
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
|
||
|
q := bson.M{}
|
||
|
|
||
|
// string
|
||
|
if p.Name {
|
||
|
switch p.NameQuery.Operation {
|
||
|
case "eq":
|
||
|
q["name"] = p.NameQuery.Query
|
||
|
case "regex":
|
||
|
q["name"] = bson.RegEx{Pattern: p.NameQuery.Query, Options: p.NameQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Address {
|
||
|
switch p.AddressQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.AddressQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.AddressQuery.Query, Options: p.AddressQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// int64
|
||
|
if p.Age {
|
||
|
switch p.AgeQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.AgeQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Location {
|
||
|
switch p.LocationQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.LocationQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.LocationQuery.Query, Options: p.LocationQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if p.Price {
|
||
|
switch p.PriceQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.PriceQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var m []*model.Data
|
||
|
rq := c.Find(q)
|
||
|
|
||
|
if p.Sort != "" {
|
||
|
rq = rq.Sort(p.Sort)
|
||
|
}
|
||
|
|
||
|
if p.Start > 0 && p.Limit > 0 {
|
||
|
rq = rq.Skip(p.Start).Limit(p.Limit)
|
||
|
} else if p.Start > 0 && p.Limit < 1 {
|
||
|
rq = rq.Skip(p.Start)
|
||
|
} else if p.Start < 1 && p.Limit > 0 {
|
||
|
rq = rq.Limit(p.Limit)
|
||
|
}
|
||
|
|
||
|
if e := rq.All(&m); e != nil {
|
||
|
return nil, e
|
||
|
}
|
||
|
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) {
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
|
||
|
q := bson.M{}
|
||
|
|
||
|
// id
|
||
|
if p.Id {
|
||
|
q["_id"] = bson.ObjectIdHex(p.IdQuery.Id)
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Name {
|
||
|
switch p.NameQuery.Operation {
|
||
|
case "eq":
|
||
|
q["name"] = p.NameQuery.Query
|
||
|
case "regex":
|
||
|
q["name"] = bson.RegEx{Pattern: p.NameQuery.Query, Options: p.NameQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Address {
|
||
|
switch p.AddressQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.AddressQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.AddressQuery.Query, Options: p.AddressQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// int64
|
||
|
if p.Age {
|
||
|
switch p.AgeQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.AgeQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Location {
|
||
|
switch p.LocationQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.LocationQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.LocationQuery.Query, Options: p.LocationQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if p.Price {
|
||
|
switch p.PriceQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.PriceQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
m := new(model.Data)
|
||
|
rq := c.Find(q)
|
||
|
|
||
|
if e := rq.All(m); e != nil {
|
||
|
return nil, e
|
||
|
}
|
||
|
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) GetCount(p *DataGetParams) (int, error) {
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
|
||
|
q := bson.M{}
|
||
|
|
||
|
// id
|
||
|
if p.Id {
|
||
|
q["_id"] = bson.ObjectIdHex(p.IdQuery.Id)
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Name {
|
||
|
switch p.NameQuery.Operation {
|
||
|
case "eq":
|
||
|
q["name"] = p.NameQuery.Query
|
||
|
case "regex":
|
||
|
q["name"] = bson.RegEx{Pattern: p.NameQuery.Query, Options: p.NameQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Address {
|
||
|
switch p.AddressQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.AddressQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.AddressQuery.Query, Options: p.AddressQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// int64
|
||
|
if p.Age {
|
||
|
switch p.AgeQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.AgeQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lt": p.AgeQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lte": p.AgeQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// string
|
||
|
if p.Location {
|
||
|
switch p.LocationQuery.Operation {
|
||
|
case "eq":
|
||
|
q["address"] = p.LocationQuery.Query
|
||
|
case "regex":
|
||
|
q["address"] = bson.RegEx{Pattern: p.LocationQuery.Query, Options: p.LocationQuery.Options}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if p.Price {
|
||
|
switch p.PriceQuery.Operation {
|
||
|
case "eq":
|
||
|
q["age"] = p.PriceQuery.From
|
||
|
case "gt-lt":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gte-lte":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
case "gte-lt":
|
||
|
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lt": p.PriceQuery.To}
|
||
|
case "gt-lte":
|
||
|
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lte": p.PriceQuery.To}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
rq := c.Find(q)
|
||
|
return rq.Count()
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) Create(m *model.Data) error {
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
return c.Insert(m)
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) UpdateId(id string, m *model.Data) error {
|
||
|
if !bson.IsObjectIdHex(id) {
|
||
|
return errors.New(fmt.Sprintf("%s is not an ObjectId", id))
|
||
|
}
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
return c.UpdateId(bson.ObjectIdHex(id), m)
|
||
|
}
|
||
|
|
||
|
func (r *DataRepository) RemoveId(id string) error {
|
||
|
if !bson.IsObjectIdHex(id) {
|
||
|
return errors.New(fmt.Sprintf("%s is not an ObjectId", id))
|
||
|
}
|
||
|
ms := r.ms.Copy()
|
||
|
defer ms.Close()
|
||
|
c := ms.DB(r.database).C(r.collection)
|
||
|
return c.RemoveId(bson.ObjectIdHex(id))
|
||
|
}
|