refdata/api/handler/data.go

304 lines
6.5 KiB
Go
Raw Permalink Normal View History

2018-03-28 01:29:06 +02:00
package handler
import (
"strconv"
2018-04-03 22:11:59 +02:00
"time"
2018-03-28 01:29:06 +02:00
"git.icod.de/dalu/refdata/api/repo"
"git.icod.de/dalu/refdata/model"
"github.com/gin-gonic/gin"
)
func (h *Handler) DataRoutes(api *gin.RouterGroup) {
dataRoutes := api.Group("/data")
dataRoutes.GET("/", h.FindData)
dataRoutes.POST("/", h.CreateData)
dataRoutes.PUT("/:id", h.UpdateData)
dataRoutes.DELETE("/:id", h.RemoveData)
}
func (h *Handler) FindData(cx *gin.Context) {
switch cx.Query("response_type") {
case "list":
h.ListData(cx)
case "one":
h.GetData(cx)
default:
cx.AbortWithStatus(400)
}
}
func (h *Handler) ListData(cx *gin.Context) {
p := new(repo.DataParams)
2018-03-28 01:29:06 +02:00
startQuery := cx.Query("start")
if startQuery != "" {
start, e := strconv.Atoi(startQuery)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.Start = start
}
limitQuery := cx.Query("limit")
if limitQuery != "" {
limit, e := strconv.Atoi(limitQuery)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.Limit = limit
}
p.Sort = cx.Query("sort")
name := cx.Query("name")
address := cx.Query("address")
location := cx.Query("location")
age := cx.Query("age")
price := cx.Query("price")
enabled := cx.Query("enabled")
2018-04-03 22:11:59 +02:00
when := cx.Query("when")
2018-03-28 01:29:06 +02:00
// string
if name == "1" {
p.Name = true
p.NameQuery = repo.DataNameQuery{}
p.NameQuery.Operation = cx.Query("name.operation")
p.NameQuery.Query = cx.Query("name.query")
p.NameQuery.Options = cx.Query("name.options")
}
// string
if address == "1" {
p.Address = true
p.AddressQuery = repo.DataAddressQuery{}
p.AddressQuery.Operation = cx.Query("address.operation")
p.AddressQuery.Query = cx.Query("address.query")
p.AddressQuery.Options = cx.Query("address.options")
}
// string
if location == "1" {
p.Location = true
p.LocationQuery = repo.DataLocationQuery{}
p.LocationQuery.Operation = cx.Query("location.operation")
p.LocationQuery.Query = cx.Query("location.query")
p.LocationQuery.Options = cx.Query("location.options")
}
2018-03-29 14:06:57 +02:00
// int64
2018-03-28 01:29:06 +02:00
if age == "1" {
p.Age = true
p.AgeQuery = repo.DataAgeQuery{}
p.AgeQuery.Operation = cx.Query("age.operation")
from, e := strconv.ParseInt(cx.Query("age.from"), 10, 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
to, e := strconv.ParseInt(cx.Query("age.from"), 10, 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.AgeQuery.From = from
p.AgeQuery.To = to
}
2018-03-29 14:06:57 +02:00
// float64
2018-03-28 01:29:06 +02:00
if price == "1" {
p.Price = true
p.PriceQuery = repo.DataPriceQuery{}
2018-04-03 22:11:59 +02:00
p.PriceQuery.Operation = cx.Query("price.operation")
2018-03-28 01:29:06 +02:00
from, e := strconv.ParseFloat(cx.Query("age.from"), 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
to, e := strconv.ParseFloat(cx.Query("age.from"), 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.PriceQuery.From = from
p.PriceQuery.To = to
}
2018-03-29 14:06:57 +02:00
// bool
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
}
}
2018-04-03 22:11:59 +02:00
// time
if when == "1" {
p.When = true
p.WhenQuery = repo.DataWhenQuery{}
p.WhenQuery.Operation = cx.Query("when.operation")
from, e := time.Parse(time.RFC3339, cx.Query("when.from"))
if e != nil {
cx.AbortWithStatus(400)
return
}
to, e := time.Parse(time.RFC3339, cx.Query("when.to"))
if e != nil {
cx.AbortWithStatus(400)
return
}
p.WhenQuery.From = from
p.WhenQuery.To = to
}
2018-03-28 01:29:06 +02:00
m, e := h.dataRepo.List(p)
if e != nil {
cx.AbortWithError(500, e)
return
}
cx.SecureJSON(200, m)
}
func (h *Handler) GetData(cx *gin.Context) {
p := new(repo.DataParams)
2018-03-28 01:29:06 +02:00
id := cx.Query("id")
name := cx.Query("name")
address := cx.Query("address")
location := cx.Query("location")
age := cx.Query("age")
price := cx.Query("price")
enabled := cx.Query("enabled")
2018-03-28 01:29:06 +02:00
// id
if id == "1" {
p.Id = true
p.IdQuery = repo.DataIdQuery{}
p.IdQuery.Id = cx.Query("id.query")
}
// string
if name == "1" {
p.Name = true
p.NameQuery = repo.DataNameQuery{}
p.NameQuery.Operation = cx.Query("name.operation")
p.NameQuery.Query = cx.Query("name.query")
p.NameQuery.Options = cx.Query("name.options")
}
// string
if address == "1" {
p.Address = true
p.AddressQuery = repo.DataAddressQuery{}
p.AddressQuery.Operation = cx.Query("address.operation")
p.AddressQuery.Query = cx.Query("address.query")
p.AddressQuery.Options = cx.Query("address.options")
}
// string
if location == "1" {
p.Location = true
p.LocationQuery = repo.DataLocationQuery{}
p.LocationQuery.Operation = cx.Query("location.operation")
p.LocationQuery.Query = cx.Query("location.query")
p.LocationQuery.Options = cx.Query("location.options")
}
// int64
if age == "1" {
p.Age = true
p.AgeQuery = repo.DataAgeQuery{}
p.AgeQuery.Operation = cx.Query("age.operation")
from, e := strconv.ParseInt(cx.Query("age.from"), 10, 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
to, e := strconv.ParseInt(cx.Query("age.from"), 10, 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.AgeQuery.From = from
p.AgeQuery.To = to
}
// float64
if price == "1" {
p.Price = true
p.PriceQuery = repo.DataPriceQuery{}
from, e := strconv.ParseFloat(cx.Query("age.from"), 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
to, e := strconv.ParseFloat(cx.Query("age.from"), 64)
if e != nil {
cx.AbortWithStatus(400)
return
}
p.PriceQuery.From = from
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
}
}
2018-03-28 01:29:06 +02:00
m, e := h.dataRepo.Get(p)
if e != nil {
cx.AbortWithError(500, e)
return
}
cx.SecureJSON(200, m)
}
func (h *Handler) CreateData(cx *gin.Context) {
m := new(model.Data)
if e := cx.BindJSON(m); e != nil {
return
}
if e := h.dataRepo.Create(m); e != nil {
cx.AbortWithError(500, e)
return
}
cx.SecureJSON(201, map[string]interface{}{})
}
func (h *Handler) UpdateData(cx *gin.Context) {
m := new(model.Data)
if e := cx.BindJSON(m); e != nil {
return
}
if e := h.dataRepo.UpdateId(cx.Param("id"), m); e != nil {
cx.AbortWithError(500, e)
return
}
cx.SecureJSON(200, map[string]interface{}{})
}
func (h *Handler) RemoveData(cx *gin.Context) {
if e := h.dataRepo.RemoveId(cx.Param("id")); e != nil {
cx.AbortWithError(500, e)
return
}
cx.SecureJSON(200, map[string]interface{}{})
}