Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
66 changes: 43 additions & 23 deletions app/controllers/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// }
16 changes: 0 additions & 16 deletions app/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,13 @@ 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)

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
Expand Down
15 changes: 0 additions & 15 deletions app/tmp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,13 @@ 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{
},
RenderArgNames: map[int][]string{
},
},
&revel.MethodType{
Name: "Show",
Args: []*revel.MethodArg{
&revel.MethodArg{Name: "locationID", Type: reflect.TypeOf((*int)(nil)) },
},
RenderArgNames: map[int][]string{
},
},

})

Expand Down
4 changes: 2 additions & 2 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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