diff --git a/Dockerfile b/Dockerfile index c2bb716..0f04bf5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,8 @@ RUN set -ex \ # Install go libraries RUN set -ex \ && go get github.com/revel/revel \ - && go get github.com/revel/cmd/revel + && go get github.com/revel/cmd/revel \ + && go get github.com/flimzy/kivik # && wget -O glide.zip https://github.com/Masterminds/glide/releases/download/0.10.2/glide-0.10.2-linux-386.zip \ # && unzip glide.zip \ # && rm glide.zip \ diff --git a/app/controllers/locations.go b/app/controllers/locations.go index f4c852e..78ba7bf 100644 --- a/app/controllers/locations.go +++ b/app/controllers/locations.go @@ -2,41 +2,61 @@ package controllers import ( "github.com/revel/revel" - "github.com/lunchqian-api/app/models" + // "github.com/lunchqian-api/app/models" + "github.com/flimzy/kivik" + _ "github.com/go-kivik/couchdb" "math/rand" "time" + "strconv" + "context" ) type Locations struct { *revel.Controller } -var locations = []models.Location{ - models.Location{1, "Timbre+"}, - models.Location{2, "B1"}, - models.Location{3, "Star Vista"}, -} - -func (c Locations) List() revel.Result { - return c.RenderJSON(locations) -} +// func (c Locations) List() revel.Result { +// TODO: Update to list out all the location in db +// return c.RenderJSON(locations) +// } func (c Locations) Random() revel.Result { - rand.Seed(time.Now().UnixNano()) - return c.RenderJSON(locations[rand.Intn(len(locations))]) -} + //TODO: shift the username and password to another place + client, err := kivik.New(context.TODO(), "couch", "http://admin:password@localhost:5984/") + if err != nil { + panic(err) + } -func (c Locations) Show(locationID int) revel.Result { - var res models.Location - for _, location := range locations { - if location.ID == locationID { - res = location + db, err := client.DB(context.TODO(), "lunch_qian") + if err != nil { + panic(err) + } + rand.Seed(time.Now().UnixNano()) + num := strconv.Itoa(rand.Intn(10)) + row, err := db.Get(context.TODO(), num) + if err != nil { + panic(num) } - } - if res.ID == 0 { - return c.NotFound("Could not find location") - } + var locale Locations + if err = row.ScanDoc(&locale); err != nil { + panic(err) + } - return c.RenderJSON(res) + return c.RenderJSON(locale.Name) } + +// func (c Locations) Show(locationID int) revel.Result { +// var res models.Location +// for _, location := range locations { +// if location.ID == locationID { +// res = location +// } +// } + +// if res.ID == 0 { +// return c.NotFound("Could not find location") +// } + +// return c.RenderJSON(res) +// } diff --git a/app/routes/routes.go b/app/routes/routes.go index 3983775..f9d1cf0 100644 --- a/app/routes/routes.go +++ b/app/routes/routes.go @@ -20,13 +20,6 @@ type tLocations struct {} var Locations tLocations -func (_ tLocations) List( - ) string { - args := make(map[string]string) - - return revel.MainRouter.Reverse("Locations.List", args).URL -} - func (_ tLocations) Random( ) string { args := make(map[string]string) @@ -34,15 +27,6 @@ func (_ tLocations) Random( return revel.MainRouter.Reverse("Locations.Random", args).URL } -func (_ tLocations) Show( - locationID int, - ) string { - args := make(map[string]string) - - revel.Unbind(args, "locationID", locationID) - return revel.MainRouter.Reverse("Locations.Show", args).URL -} - type tStatic struct {} var Static tStatic diff --git a/app/tmp/main.go b/app/tmp/main.go index ec46d6a..fd44f41 100644 --- a/app/tmp/main.go +++ b/app/tmp/main.go @@ -45,13 +45,6 @@ func main() { revel.RegisterController((*controllers.Locations)(nil), []*revel.MethodType{ - &revel.MethodType{ - Name: "List", - Args: []*revel.MethodArg{ - }, - RenderArgNames: map[int][]string{ - }, - }, &revel.MethodType{ Name: "Random", Args: []*revel.MethodArg{ @@ -59,14 +52,6 @@ func main() { RenderArgNames: map[int][]string{ }, }, - &revel.MethodType{ - Name: "Show", - Args: []*revel.MethodArg{ - &revel.MethodArg{Name: "locationID", Type: reflect.TypeOf((*int)(nil)) }, - }, - RenderArgNames: map[int][]string{ - }, - }, }) diff --git a/conf/routes b/conf/routes index 36eb73f..f7614bd 100644 --- a/conf/routes +++ b/conf/routes @@ -18,6 +18,6 @@ GET /public/*filepath Static.Serve("public") # Catch all * /:controller/:action :controller.:action -GET /locations Locations.List -GET /locations/:locationID Locations.Show +#GET /locations Locations.List +#GET /locations/:locationID Locations.Show GET /locations/random Locations.Random