Skip to content

Commit

Permalink
create endpoint for updating task properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonay committed Jan 12, 2015
1 parent 8e9fbc2 commit f5725d0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
14 changes: 14 additions & 0 deletions common/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,17 @@ type TaskJson struct {
Members []Member `json:"members"`
Priority string `json:"priority"`
}

type KrPropertiesJson struct {
KrName string `json:"krName",omitempty`
KrBody string `json:"krbody",omitempty`
Priority string `json:"priority",omitempty`
Completed interface{} `json:"completed",omitempty`
}

type TaskPropertiesJson struct {
TaskName string `json:"taskname",omitempty`
TaskBody string `json:"taskbody",omitempty`
Priority string `json:"priority",omitempty`
Completed interface{} `json:"completed",omitempty`
}
10 changes: 2 additions & 8 deletions handlers/keyResult_update_props.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"github.com/dmonay/okra/common"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)
Expand All @@ -12,14 +13,7 @@ func (dw *DoWorkResource) UpdateKrProperties(c *gin.Context) {
kr := c.Params.ByName("kr")
id := bson.ObjectIdHex(treeId)

type KrPropertiesJson struct {
KrName string `json:"krName",omitempty`
KrBody string `json:"krbody",omitempty`
Priority string `json:"priority",omitempty`
Completed interface{} `json:"completed",omitempty`
}

var reqBody KrPropertiesJson
var reqBody common.KrPropertiesJson
c.Bind(&reqBody)
newName := reqBody.KrName
newBody := reqBody.KrBody
Expand Down
5 changes: 3 additions & 2 deletions handlers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func Run(cfg common.Config) error {
r.POST("/create/objective/:organization", doWorkResource.CreateObjective)
r.POST("/update/objective/properties/:organization/:treeid/:objective", doWorkResource.UpdateObjProperties)
r.POST("/create/kr/:organization/:objective", doWorkResource.CreateKeyResult)
r.POST("/update/objective/properties/:organization/:treeid/:objective/:kr", doWorkResource.UpdateKrProperties)
r.POST("/update/kr/properties/:organization/:treeid/:objective/:kr", doWorkResource.UpdateKrProperties)

// tasks
r.POST("/create/kr/:organization/:objective/:kr", doWorkResource.CreateTask)
r.POST("/create/task/:organization/:objective/:kr", doWorkResource.CreateTask)
r.POST("/update/task/properties/:organization/:treeid/:objective/:kr/:task", doWorkResource.UpdateTaskProperties)

r.Run(cfg.SvcHost)

Expand Down
62 changes: 62 additions & 0 deletions handlers/task_update_props.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package handlers

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

func (dw *DoWorkResource) UpdateTaskProperties(c *gin.Context) {
org := c.Params.ByName("organization")
treeId := c.Params.ByName("treeid")
obj := c.Params.ByName("objective")
krIndex := c.Params.ByName("kr")
taskIndex := c.Params.ByName("task")
id := bson.ObjectIdHex(treeId)

var reqBody common.TaskPropertiesJson
c.Bind(&reqBody)
newName := reqBody.TaskName
newBody := reqBody.TaskBody
newStatus := reqBody.Completed
newPriority := reqBody.Priority

// instead of using bson.M, manually create the map
// so that we can add fields conditionally
myMap := make(map[string]interface{})

// TEMP workaround. Mongo doesn't support >1 positional operators per query,
// so I can't use the index of the nested array inside the objectives document.
// Thus I need the index of the array beforehand, and this is currently,
// temporarily passed in as the last URL param
nameKey := "objectives.$.keyresults." + krIndex + ".tasks." + taskIndex + ".name"
bodyKey := "objectives.$.keyresults." + krIndex + ".tasks." + taskIndex + ".body"
statusKey := "objectives.$.keyresults." + krIndex + ".tasks." + taskIndex + ".completed"
prioritiesKey := "objectives.$.keyresults." + krIndex + ".tasks." + taskIndex + ".priority"

if newName != "" {
myMap[nameKey] = newName
}

if newBody != "" {
myMap[bodyKey] = newBody
}

if newStatus != "" {
myMap[statusKey] = newStatus
}

if newPriority != "" {
myMap[prioritiesKey] = newPriority
}

querier := bson.M{"_id": id, "objectives.id": obj}
updateName := bson.M{"$set": myMap}
err := dw.mongo.C(org).Update(querier, updateName)
if err != nil {
CheckErr(err, "Mongo failed to update task's properties", c)
return
}

c.JSON(201, SuccessMsg{"Task updated!"})
}

0 comments on commit f5725d0

Please sign in to comment.