public static KanbanizeTask ToKanbanizeTask(this WorkItem wi, string tfsUri,  IEnumerable<WorkItem> relatedWorkItems)
        {
            var assignee = ResolveAssignedTo((string) wi["Assigned To"]);
            var resultTask = new KanbanizeTask
            {
                Title = CompileTitleFromWorkItem(wi),
                Description = wi.Description,
                ExternalLink = String.Format(Properties.Settings.Default.TfsTaskLinkTemplate, tfsUri, wi.Id),
                Size = (double)wi["Est Man Days"],
                Color = Properties.Settings.Default.ColorTask,
                Assignee = assignee,
                Priority = SetPriorityByTfsSeverity(wi),
                Type = "Task"
            };

            #region Add subtasks
            // ATTENTION: for now Kanbanize API doesn't allow include subtasks when creating task
            /*resultTask.SubtaskDetails = relatedWorkItems
                 .Select(x =>
                     new KanbanizeSubtaskDetails()
                     {
                         Title = CompileTitleFromWorkItem(x),
                         Assignee = assignee
                     }
             ).ToArray();//*/
            #endregion

            return resultTask;
        }
        public void Update(Options options)
        {
            var kanbanizeTasks = _kanbanizeRepository.GetAllTasks();
            IEnumerable<KanbanizeTask> qualifiedTasks = kanbanizeTasks;
            if (options.KanbanizeIdStrings != null && options.KanbanizeIdStrings.Count > 0)
            {
                qualifiedTasks = qualifiedTasks.Where(x => options.KanbanizeIds.Contains(x.Id));
            }
            if (!String.IsNullOrWhiteSpace(options.SelectType))
            {
                qualifiedTasks = qualifiedTasks.Where(x => x.Type == options.SelectType);
            }
            if (!String.IsNullOrWhiteSpace(options.SelectNoTfsId))
            {
                qualifiedTasks = qualifiedTasks.Where(x => x.TfsId == null);
            }

            foreach (var kanbanizeTask in qualifiedTasks)
            {
                bool needUpdate = false;
                var updateTask = new KanbanizeTask {Id = kanbanizeTask.Id};
                if (!needUpdate && !String.IsNullOrWhiteSpace(options.UpdateColor) && kanbanizeTask.ColorString != ( "#" + options.UpdateColor))
                {
                    needUpdate = true;
                    updateTask.ColorString = options.UpdateColor;
                }

                if (!needUpdate && !String.IsNullOrWhiteSpace(options.UpdateType) && kanbanizeTask.Type != options.UpdateType)
                {
                    needUpdate = true;
                    updateTask.Type = options.UpdateType;
                }

                if (needUpdate)
                {
                    _kanbanizeRepository.UpdateTask(updateTask);
                }
            }

            /*
            int[] taskIds = options.TfsIds;
            var workItems = tfsRepository.GetWorkItems(taskIds);
            foreach (var workItem in workItems)
            {
                var kanbanizeTask = enumerable.FirstOrDefault(x => x.TfsId == workItem.Id);
                if (kanbanizeTask != null)
                {
                    //kanbanizeTask.UpdateByWorkItem(workItem);
                    _kanbanizeRepository.UpdateTask(kanbanizeTask);
                }
            }
             * */
        }
        private string QueryTask(KanbanizeTask taskToQuery, KanbanizeCommand command, string[] optionalParams = null)
        {
            taskToQuery.BoardId = _boardId;
            var uriBuilder = new UriBuilder(_uri);
            uriBuilder.Path += string.Format(@"/{0}", command.ToString("G"));
            if (optionalParams != null)
            {
                uriBuilder.Path += "/" + string.Join("/", optionalParams);
            }
            uriBuilder.Path += "/format/json";
            var request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
            request.Headers.Add("apikey", _apiKey);
            request.Method = "POST";
            request.ContentType = "application/json";

            UTF8Encoding encoding = new UTF8Encoding();
            var taskJson = JsonConvert.SerializeObject(taskToQuery,
                                                       new JsonSerializerSettings
                                                       {
                                                           Converters =
                                                               new[]
                                                                       {
                                                                           new IsoDateTimeConverter
                                                                               {DateTimeFormat = "yyyy-mm-dd"}
                                                                       },
                                                           NullValueHandling = NullValueHandling.Ignore
                                                       });
            var taskBytes = encoding.GetBytes(taskJson);

            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(taskBytes, 0, taskBytes.Length);
            }

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = GetResponseContent(encoding, response);
                return responseString;
            }
            catch (WebException wex)
            {
                var httpResponse = wex.Response as HttpWebResponse;
                if (httpResponse != null)
                {
                    if (httpResponse.StatusCode == HttpStatusCode.Forbidden)
                    {
                        throw new ApplicationException("API key is forbidden");
                    }

                    var responseString = GetResponseContent(encoding, httpResponse);
                    if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
                    {
                        throw  new ApplicationException(string.Format("Bad request returned error:\n{0}", responseString));
                    }

                    throw new ApplicationException(string.Format(
                        "Remote server call {0} {1} resulted in a http error {2} {3}.",
                        "POST",
                        uriBuilder.Uri,
                        httpResponse.StatusCode,
                        httpResponse.StatusDescription), wex);
                }
                else
                {
                    throw new ApplicationException(string.Format(
                        "Remote server call {0} {1} resulted in an error.",
                        "POST",
                        uriBuilder.Uri), wex);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 public bool UpdateTask(KanbanizeTask kanbanizeTask)
 {
     var responseJson = QueryTask(kanbanizeTask, KanbanizeCommand.edit_task);
     return responseJson == "1";
 }
 public string CreateNewTask(KanbanizeTask newTask)
 {
     //newTask.Template = "Task";
     return QueryTask(newTask, KanbanizeCommand.create_new_task);
 }