Improve attribute name handling

This commit is contained in:
Nise Void 2018-05-28 14:17:59 +02:00
parent 9374108288
commit df44cd0038
Signed by: NiseVoid
GPG key ID: FBA14AC83EA602F3
3 changed files with 39 additions and 55 deletions

View file

@ -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
}

View file

@ -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))
}
}