Skip to content

Commit

Permalink
add hanlder to update tree name
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonay committed Jan 11, 2015
1 parent 965c1c5 commit b36cd54
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
5 changes: 0 additions & 5 deletions common/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ type UserJson struct {
Trees []string `bson:"trees" omitempty`
}

type MissionJson struct {
Mission string `json:"mission"`
TreeId string `json:"treeId,omitempty"`
}

type OrganizationJson struct {
Organization string `json:"organization"`
UserId string `json:"userId" bson:"_id,omitempty"`
Expand Down
8 changes: 6 additions & 2 deletions handlers/mission_update.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package handlers

import (
"github.com/dmonay/okra/common"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)

func (dw *DoWorkResource) UpdateMission(c *gin.Context) {

type MissionJson struct {
Mission string `json:"mission"`
TreeId string `json:"treeId,omitempty"`
}

org := c.Params.ByName("organization")
var reqBody common.MissionJson
var reqBody MissionJson

c.Bind(&reqBody)

Expand Down
2 changes: 1 addition & 1 deletion handlers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Run(cfg common.Config) error {

// trees
r.POST("/create/tree/:organization", doWorkResource.CreateTree)
r.POST("/update/tree/name/:organization/:treeid", doWorkResource.UpdateTreeName)
r.GET("/get/trees/:organization/:treeid", doWorkResource.GetTree)
r.GET("/get/trees/:organization", doWorkResource.GetAllTrees)

Expand All @@ -84,7 +85,6 @@ func InitMongo() (*mgo.Database, error) {
CheckErr(err, "mongo connection failed")

db := session.DB("testing")
CheckErr(err, "mongo opening database failed")

return db, nil
}
Expand Down
27 changes: 27 additions & 0 deletions handlers/tree_update_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handlers

import (
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)

func (dw *DoWorkResource) UpdateTreeName(c *gin.Context) {
org := c.Params.ByName("organization")
treeId := c.Params.ByName("treeid")
id := bson.ObjectIdHex(treeId)

type TreeNameJson struct {
TreeName string `json:"treename"`
}

var reqBody TreeNameJson
c.Bind(&reqBody)
newName := reqBody.TreeName

querier := bson.M{"_id": id}
updateName := bson.M{"$set": bson.M{"treename": newName}}
err := dw.mongo.C(org).Update(querier, updateName)
CheckErr(err, "Mongo failed to update tree name")

c.JSON(201, "Success")
}

0 comments on commit b36cd54

Please sign in to comment.