Example #1
0
// WorkItemSave saves a WorkItem record to Pharos. If the WorkItems's ID
// is zero, this performs a POST to create a new record. For non-zero IDs, this
// performs a PUT to update the existing record. The response object
// will include a new copy of the WorkItem if it was saved successfully.
func (client *PharosClient) WorkItemSave(obj *models.WorkItem) *PharosResponse {
	// Set up the response object
	resp := NewPharosResponse(PharosWorkItem)
	resp.workItems = make([]*models.WorkItem, 1)

	// URL and method
	relativeUrl := fmt.Sprintf("/api/%s/items/", client.apiVersion)
	httpMethod := "POST"
	if obj.Id > 0 {
		// URL should look like /api/v2/items/46956/
		relativeUrl = fmt.Sprintf("%s%d/", relativeUrl, obj.Id)
		httpMethod = "PUT"
	}
	absoluteUrl := client.BuildUrl(relativeUrl)

	// Prepare the JSON data
	postData, err := obj.SerializeForPharos()
	if err != nil {
		resp.Error = err
	}

	// Run the request
	client.DoRequest(resp, httpMethod, absoluteUrl, bytes.NewBuffer(postData))
	if resp.Error != nil {
		return resp
	}

	// Parse the JSON from the response body
	workItem := &models.WorkItem{}
	resp.Error = json.Unmarshal(resp.data, workItem)
	if resp.Error == nil {
		resp.workItems[0] = workItem
	}
	return resp
}