public async Task <string> AddTask(ToodleDoTask toodleDoTask) { this.LogDebugFormat("Adding tasks {0}", toodleDoTask.Title); string apiCall = null; TaskChangeRequest request = this.GetTaskChangeRequest(toodleDoTask, TaskProperties.All, false); apiCall = string.Format("{0}/tasks/add.php?key={1};tasks=[{2}];fields={3};f=xml;t={4}", this.serverUrl, this.Key, request.JsonData, request.Options, DateTime.UtcNow.Ticks); if (apiCall.Length > MaxLengthUri && toodleDoTask.Note != null) { // ToodleDo sends an error if the whole uri is more than 8 200 characters while (apiCall.Length > MaxLengthUri) { toodleDoTask.Note = toodleDoTask.Note.Substring(0, toodleDoTask.Note.Length - 100); request = this.GetTaskChangeRequest(toodleDoTask, TaskProperties.All, false); apiCall = string.Format("{0}/tasks/add.php?key={1};tasks=[{2}];fields={3};f=xml;t={4}", this.serverUrl, this.Key, request.JsonData, request.Options, DateTime.UtcNow.Ticks); } } var result = await this.DownloadDataAsync(apiCall); if (result.HasError || !result.Document.Descendants("tasks").Any()) { return(null); } string id = null; List <string> ids = result.Document.Descendants("task").Select(x => x.Element("id").Value).ToList(); if (ids.Count > 0 && ids[0] != "-1") { id = ids[0]; } else { this.LogDebugFormat("Error while adding task {0}", toodleDoTask.Title); } return(id); }
/// <summary> Change task </summary> public async Task <ActionResult> ChangeTaskAsync(int id, TaskChangeRequest taskChangeRequest) { var task = await _taskRepository.GetByIdAsync(id); if (task == null) { return(new NotFoundObjectResult($"Task is not found with id:{id}")); } bool?isAllowedStatus = null; if (task.StatusId == 1) { isAllowedStatus = taskChangeRequest.StatusId == 2 || taskChangeRequest.StatusId == 3; } else if (task.StatusId == 2) { isAllowedStatus = taskChangeRequest.StatusId == 1 || taskChangeRequest.StatusId == 3; } else if (task.StatusId == 3) { return(new UnprocessableEntityObjectResult("Task changing is not allowed")); } if (isAllowedStatus == null) { return(new NotFoundObjectResult($"Status is not found with id:{id}")); } if (isAllowedStatus == false) { return(new UnprocessableEntityObjectResult($"Status is not allowed with id:{id}")); } task.Name = taskChangeRequest.Name; task.Description = taskChangeRequest.Description; task.StatusId = taskChangeRequest.StatusId; await _taskRepository.UpdateAsync(task); return(new OkResult()); }
public async Task <ActionResult> Put(int id, [FromBody] TaskChangeRequest taskChangeRequest) { return(await _taskService.ChangeTaskAsync(id, taskChangeRequest)); }
private TaskChangeRequest GetTaskChangeRequest(ToodleDoTask toodleDoTask, TaskProperties properties, bool includeId) { var request = new TaskChangeRequest(); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.None; jsonWriter.WriteStartObject(); if (includeId) { jsonWriter.WritePropertyName("id"); jsonWriter.WriteValue(toodleDoTask.Id); } if ((properties & TaskProperties.Title) == TaskProperties.Title) { jsonWriter.WritePropertyName("title"); jsonWriter.WriteValue(toodleDoTask.Title); } if ((properties & TaskProperties.Note) == TaskProperties.Note) { string note = string.Empty; if (!string.IsNullOrEmpty(toodleDoTask.Note)) { note = toodleDoTask.Note; } jsonWriter.WritePropertyName("note"); jsonWriter.WriteValue(note); request.Options += "note,"; } if ((properties & TaskProperties.Tags) == TaskProperties.Tags) { if (!string.IsNullOrEmpty(toodleDoTask.Tags)) { jsonWriter.WritePropertyName("tag"); jsonWriter.WriteValue(toodleDoTask.Tags); request.Options += "tag,"; } } if ((properties & TaskProperties.Folder) == TaskProperties.Folder) { jsonWriter.WritePropertyName("folder"); jsonWriter.WriteValue(toodleDoTask.FolderId); request.Options += "folder,"; } if ((properties & TaskProperties.Context) == TaskProperties.Context) { string contextId = toodleDoTask.ContextId; if (string.IsNullOrEmpty(contextId)) { contextId = "0"; } jsonWriter.WritePropertyName("context"); jsonWriter.WriteValue(contextId); request.Options += "context,"; } if ((properties & TaskProperties.Due) == TaskProperties.Due) { int duedate = 0; if (toodleDoTask.Due.HasValue) { duedate = toodleDoTask.Due.Value.DateTimeToTimestamp(); } jsonWriter.WritePropertyName("duedate"); jsonWriter.WriteValue(duedate); request.Options += "duedate,"; } if ((properties & TaskProperties.Start) == TaskProperties.Start) { int startdate = 0; int starttime = 0; if (toodleDoTask.Start.HasValue) { startdate = toodleDoTask.Start.Value.Date.DateTimeToTimestamp(); starttime = (int)toodleDoTask.Start.Value.TimeOfDay.TotalSeconds; } jsonWriter.WritePropertyName("startdate"); jsonWriter.WriteValue(startdate); request.Options += "starttime,"; jsonWriter.WritePropertyName("starttime"); jsonWriter.WriteValue(starttime); request.Options += "starttime,"; } if ((properties & TaskProperties.Frequency) == TaskProperties.Frequency) { jsonWriter.WritePropertyName("repeat"); jsonWriter.WriteValue(toodleDoTask.Repeat); request.Options += "repeat,"; } if ((properties & TaskProperties.RepeatFrom) == TaskProperties.RepeatFrom) { jsonWriter.WritePropertyName("repeatfrom"); jsonWriter.WriteValue(toodleDoTask.RepeatFrom); request.Options += "repeatfrom,"; } if ((properties & TaskProperties.Completed) == TaskProperties.Completed) { if (toodleDoTask.Completed.HasValue) { jsonWriter.WritePropertyName("completed"); jsonWriter.WriteValue(toodleDoTask.Completed.Value.DateTimeToTimestamp()); } else { jsonWriter.WritePropertyName("completed"); jsonWriter.WriteValue(0); } } if ((properties & TaskProperties.Added) == TaskProperties.Added) { jsonWriter.WritePropertyName("added"); jsonWriter.WriteValue(toodleDoTask.Added.DateTimeToTimestamp()); request.Options += "added,"; } if ((properties & TaskProperties.Priority) == TaskProperties.Priority) { jsonWriter.WritePropertyName("priority"); jsonWriter.WriteValue(toodleDoTask.Priority); request.Options += "priority,"; } if ((properties & TaskProperties.Parent) == TaskProperties.Parent) { jsonWriter.WritePropertyName("parent"); jsonWriter.WriteValue(toodleDoTask.ParentId); request.Options += "parent,"; } jsonWriter.WriteEndObject(); } request.JsonData = Escape(sb.ToString()); return(request); }