252 lines
5.4 KiB
Go
252 lines
5.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"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.DataListParams)
|
|
|
|
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")
|
|
|
|
// 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")
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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.DataGetParams)
|
|
|
|
id := cx.Query("id")
|
|
name := cx.Query("name")
|
|
address := cx.Query("address")
|
|
location := cx.Query("location")
|
|
age := cx.Query("age")
|
|
price := cx.Query("price")
|
|
|
|
// 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
|
|
}
|
|
|
|
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{}{})
|
|
}
|