Skip to content

Commit

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

type TaskJson struct {
Id string `json:"id"`
TreeId string `json:"treeId,omitempty"`
Name string `json:"name"`
Body string `json:"body"`
Completed bool `json:"completed"`
Members []Member `json:"members"`
Priority string `json:"priority"`
}
1 change: 1 addition & 0 deletions common/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type KeyResultsModel struct {
}

type TasksModel struct {
Id string
Name string
Body string
Completed bool
Expand Down
1 change: 1 addition & 0 deletions handlers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func Run(cfg common.Config) error {
r.POST("/update/objective/properties/:organization/:treeid/:objective/:kr", doWorkResource.UpdateKrProperties)

// tasks
r.POST("/create/kr/:organization/:objective/:kr", doWorkResource.CreateTask)

r.Run(cfg.SvcHost)

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

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

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

org := c.Params.ByName("organization")
obj := c.Params.ByName("objective")
krIndex := c.Params.ByName("kr")
var reqBody common.TaskJson

c.Bind(&reqBody)

arrayOfMembers := []common.Member{}
for _, value := range reqBody.Members {
// Get userId of user
var result common.UsersObj
err := dw.mongo.C("Users").Find(bson.M{"username": value.Username}).One(&result)
if err != nil {
CheckErr(err, "Mongo failed to find the "+value.Username+"'s doc in Users", c)
return
}
memObj := common.Member{value.Username, result.Id.Hex(), value.Role}
arrayOfMembers = append(arrayOfMembers, memObj)
}

treeId := bson.ObjectIdHex(reqBody.TreeId)

kr := common.TasksModel{
Id: reqBody.Id,
Name: reqBody.Name,
Body: reqBody.Body,
Completed: reqBody.Completed,
Members: arrayOfMembers,
Priority: reqBody.Priority,
}

// again, because Mongo doesn't allow for >1 positional operator,
// we must receive the index of the key result from the client and
// use that in our query
findKR := "objectives.$.keyresults." + krIndex + ".tasks"

colQuerier := bson.M{"_id": treeId, "objectives.id": obj}
addKeyResult := bson.M{"$push": bson.M{findKR: kr}}
err := dw.mongo.C(org).Update(colQuerier, addKeyResult)
if err != nil {
CheckErr(err, "Mongo failed to add task", c)
return
}

c.JSON(201, SuccessMsg{"You have successfully added a task to the key result"})
}

0 comments on commit 8e9fbc2

Please sign in to comment.