params consolidation, fixed numeric params

This commit is contained in:
Darko Luketic 2018-03-28 04:24:06 +02:00
parent 80abb6a862
commit 6761f8d666
3 changed files with 99 additions and 134 deletions

View File

@ -28,7 +28,7 @@ func (h *Handler) FindData(cx *gin.Context) {
} }
func (h *Handler) ListData(cx *gin.Context) { func (h *Handler) ListData(cx *gin.Context) {
p := new(repo.DataListParams) p := new(repo.DataParams)
startQuery := cx.Query("start") startQuery := cx.Query("start")
if startQuery != "" { if startQuery != "" {
@ -56,6 +56,7 @@ func (h *Handler) ListData(cx *gin.Context) {
location := cx.Query("location") location := cx.Query("location")
age := cx.Query("age") age := cx.Query("age")
price := cx.Query("price") price := cx.Query("price")
enabled := cx.Query("enabled")
// string // string
if name == "1" { if name == "1" {
@ -120,6 +121,18 @@ func (h *Handler) ListData(cx *gin.Context) {
p.PriceQuery.To = to p.PriceQuery.To = to
} }
if enabled == "1" {
p.Enabled = true
p.EnabledQuery = repo.DataEnabledQuery{}
if cx.Query("enabled.query") == "true" {
p.EnabledQuery.Query = true
} else if cx.Query("enabled.query") == "false" {
p.EnabledQuery.Query = false
} else {
p.Enabled = false
}
}
m, e := h.dataRepo.List(p) m, e := h.dataRepo.List(p)
if e != nil { if e != nil {
cx.AbortWithError(500, e) cx.AbortWithError(500, e)
@ -129,7 +142,7 @@ func (h *Handler) ListData(cx *gin.Context) {
} }
func (h *Handler) GetData(cx *gin.Context) { func (h *Handler) GetData(cx *gin.Context) {
p := new(repo.DataGetParams) p := new(repo.DataParams)
id := cx.Query("id") id := cx.Query("id")
name := cx.Query("name") name := cx.Query("name")
@ -137,6 +150,7 @@ func (h *Handler) GetData(cx *gin.Context) {
location := cx.Query("location") location := cx.Query("location")
age := cx.Query("age") age := cx.Query("age")
price := cx.Query("price") price := cx.Query("price")
enabled := cx.Query("enabled")
// id // id
if id == "1" { if id == "1" {
@ -210,6 +224,18 @@ func (h *Handler) GetData(cx *gin.Context) {
p.PriceQuery.To = to p.PriceQuery.To = to
} }
if enabled == "1" {
p.Enabled = true
p.EnabledQuery = repo.DataEnabledQuery{}
if cx.Query("enabled.query") == "true" {
p.EnabledQuery.Query = true
} else if cx.Query("enabled.query") == "false" {
p.EnabledQuery.Query = false
} else {
p.Enabled = false
}
}
m, e := h.dataRepo.Get(p) m, e := h.dataRepo.Get(p)
if e != nil { if e != nil {
cx.AbortWithError(500, e) cx.AbortWithError(500, e)

View File

@ -49,38 +49,31 @@ type DataPriceQuery struct {
To float64 To float64
} }
type DataListParams struct { // bool
Start int type DataEnabledQuery struct {
Limit int Query bool
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 { type DataParams struct {
Id bool Start int // list only
Limit int // list only
Sort string // list only
Id bool // get only
Name bool Name bool
Address bool Address bool
Location bool Location bool
Age bool Age bool
Price bool Price bool
Enabled bool
IdQuery DataIdQuery IdQuery DataIdQuery // get only
NameQuery DataNameQuery NameQuery DataNameQuery
AddressQuery DataAddressQuery AddressQuery DataAddressQuery
LocationQuery DataLocationQuery LocationQuery DataLocationQuery
AgeQuery DataAgeQuery AgeQuery DataAgeQuery
PriceQuery DataPriceQuery PriceQuery DataPriceQuery
EnabledQuery DataEnabledQuery
} }
type DataRepository struct { type DataRepository struct {
@ -102,12 +95,10 @@ func (r *DataRepository) Close() {
r.ms.Close() r.ms.Close()
} }
func (r *DataRepository) List(p *DataListParams) ([]*model.Data, error) { func (r *DataRepository) List(p *DataParams) ([]*model.Data, error) {
ms := r.ms.Copy() ms := r.ms.Copy()
defer ms.Close() defer ms.Close()
c := ms.DB(r.database).C(r.collection) c := ms.DB(r.database).C(r.collection)
q := bson.M{} q := bson.M{}
// string // string
@ -130,6 +121,16 @@ func (r *DataRepository) List(p *DataListParams) ([]*model.Data, error) {
} }
} }
// 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}
}
}
// int64 // int64
if p.Age { if p.Age {
switch p.AgeQuery.Operation { switch p.AgeQuery.Operation {
@ -143,34 +144,46 @@ func (r *DataRepository) List(p *DataListParams) ([]*model.Data, error) {
q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lt": p.AgeQuery.To} q["age"] = bson.M{"$gte": p.AgeQuery.From, "$lt": p.AgeQuery.To}
case "gt-lte": case "gt-lte":
q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lte": p.AgeQuery.To} q["age"] = bson.M{"$gt": p.AgeQuery.From, "$lte": p.AgeQuery.To}
case "gt":
q["age"] = bson.M{"$gt": p.AgeQuery.From}
case "gte":
q["age"] = bson.M{"$gte": p.AgeQuery.From}
case "lt":
q["age"] = bson.M{"$lt": p.AgeQuery.To}
case "lte":
q["age"] = bson.M{"$lte": p.AgeQuery.To}
} }
} }
// string // float64
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 { if p.Price {
switch p.PriceQuery.Operation { switch p.PriceQuery.Operation {
case "eq": case "eq":
q["age"] = p.PriceQuery.From q["price"] = p.PriceQuery.From
case "gt":
q["price"] = bson.M{"$gt": p.PriceQuery.From}
case "gte":
q["price"] = bson.M{"$gte": p.PriceQuery.From}
case "lt":
q["price"] = bson.M{"$lt": p.PriceQuery.To}
case "lte":
q["price"] = bson.M{"$lte": p.PriceQuery.To}
case "gt-lt": case "gt-lt":
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lt": p.PriceQuery.To} q["price"] = bson.M{"$gt": p.PriceQuery.From, "$lt": p.PriceQuery.To}
case "gte-lte": case "gte-lte":
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lte": p.PriceQuery.To} q["price"] = bson.M{"$gte": p.PriceQuery.From, "$lte": p.PriceQuery.To}
case "gte-lt": case "gte-lt":
q["age"] = bson.M{"$gte": p.PriceQuery.From, "$lt": p.PriceQuery.To} q["price"] = bson.M{"$gte": p.PriceQuery.From, "$lt": p.PriceQuery.To}
case "gt-lte": case "gt-lte":
q["age"] = bson.M{"$gt": p.PriceQuery.From, "$lte": p.PriceQuery.To} q["price"] = bson.M{"$gt": p.PriceQuery.From, "$lte": p.PriceQuery.To}
} }
} }
// bool
if p.Enabled {
q["enabled"] = p.EnabledQuery.Query
}
var m []*model.Data var m []*model.Data
rq := c.Find(q) rq := c.Find(q)
@ -193,7 +206,7 @@ func (r *DataRepository) List(p *DataListParams) ([]*model.Data, error) {
return m, nil return m, nil
} }
func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) { func (r *DataRepository) Get(p *DataParams) (*model.Data, error) {
ms := r.ms.Copy() ms := r.ms.Copy()
defer ms.Close() defer ms.Close()
@ -226,6 +239,16 @@ func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) {
} }
} }
// 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}
}
}
// int64 // int64
if p.Age { if p.Age {
switch p.AgeQuery.Operation { switch p.AgeQuery.Operation {
@ -242,16 +265,7 @@ func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) {
} }
} }
// string // float64
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 { if p.Price {
switch p.PriceQuery.Operation { switch p.PriceQuery.Operation {
case "eq": case "eq":
@ -267,6 +281,11 @@ func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) {
} }
} }
// bool
if p.Enabled {
q["enabled"] = p.EnabledQuery.Query
}
m := new(model.Data) m := new(model.Data)
rq := c.Find(q) rq := c.Find(q)
@ -277,84 +296,6 @@ func (r *DataRepository) Get(p *DataGetParams) (*model.Data, error) {
return m, nil 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 { func (r *DataRepository) Create(m *model.Data) error {
ms := r.ms.Copy() ms := r.ms.Copy()
defer ms.Close() defer ms.Close()

View File

@ -1,13 +1,11 @@
package cmd package cmd
import ( import (
"fmt"
"github.com/spf13/cobra"
"github.com/gin-gonic/gin"
"git.icod.de/dalu/refdata/api/handler" "git.icod.de/dalu/refdata/api/handler"
"git.icod.de/dalu/refdata/api/repo" "git.icod.de/dalu/refdata/api/repo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo" "github.com/globalsign/mgo"
"github.com/spf13/cobra"
) )
// apiCmd represents the api command // apiCmd represents the api command
@ -24,7 +22,7 @@ var apiCmd = &cobra.Command{
r := gin.Default() r := gin.Default()
api := r.Group("/api/v1") api := r.Group("/api/v1")
dataRepo := repo.NewDataRepository(ms, "ApiName", "data") dataRepo := repo.NewDataRepository(ms, "apiname", "data")
h := handler.NewHandler(dataRepo) h := handler.NewHandler(dataRepo)
h.DataRoutes(api) h.DataRoutes(api)