Improve attribute name handling
This commit is contained in:
parent
9374108288
commit
df44cd0038
3 changed files with 39 additions and 55 deletions
|
@ -29,7 +29,10 @@ func main() {
|
|||
e.Use(recoverMiddleware)
|
||||
|
||||
e.GET(`/count`, count)
|
||||
e.GET(`/:attr/:date`, pmax)
|
||||
|
||||
for _, v := range app.GetAttributes() {
|
||||
e.GET(`/`+strings.ToLower(v.Name)+`/:date`, getAttr(v.Index))
|
||||
}
|
||||
|
||||
panic(e.Start(`localhost:33333`))
|
||||
}
|
||||
|
@ -38,31 +41,13 @@ func count(c echo.Context) error {
|
|||
return c.JSON(200, app.Count())
|
||||
}
|
||||
|
||||
func pmax(c echo.Context) error {
|
||||
attr := c.Param(`attr`)
|
||||
if len(attr) < 2 {
|
||||
return c.NoContent(400)
|
||||
}
|
||||
attr = convertAttribute(attr)
|
||||
func getAttr(index int) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
date, err := time.Parse(time.RFC3339, c.Param(`date`))
|
||||
if err != nil {
|
||||
return c.NoContent(400)
|
||||
}
|
||||
|
||||
date, err := time.Parse(time.RFC3339, c.Param(`date`))
|
||||
if err != nil {
|
||||
return c.NoContent(400)
|
||||
return c.JSON(200, app.GetAttribute(index, date))
|
||||
}
|
||||
|
||||
data := app.GetAttribute(attr, date)
|
||||
if data == nil {
|
||||
return c.NoContent(404)
|
||||
}
|
||||
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func convertAttribute(attr string) string {
|
||||
attr = strings.ToUpper(attr[:1]) + strings.ToLower(attr[1:])
|
||||
|
||||
attr = strings.Replace(attr, `gem`, `Gem`, -1)
|
||||
attr = strings.Replace(attr, `max`, `Max`, -1)
|
||||
|
||||
return attr
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestConvertAttribute(t *testing.T) {
|
||||
var data = map[string]string{
|
||||
`cgem`: `CGem`,
|
||||
`ep`: `Ep`,
|
||||
`PMAX`: `PMax`,
|
||||
`Test`: `Test`,
|
||||
}
|
||||
|
||||
for in, expected := range data {
|
||||
assert.Equal(t, expected, convertAttribute(in))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue