-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dmonay
committed
Jan 12, 2015
1 parent
c99011d
commit 8e9fbc2
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |