public HttpResponseMessage CreateList(TodolistModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string accessToken) { return this.ExecuteOperationAndHandleExceptions(() => { var context = new TasksManagerDbContext(); var meUser = this.GetUserByAccessToken(accessToken, context); this.ValidateList(model); var listEntity = model.ToEntity(); listEntity.Owner = meUser; meUser.TodoLists.Add(listEntity); context.SaveChanges(); var responseModel = new ListCreatedModel() { Id = listEntity.Id, Owner = meUser.Username }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { listId = listEntity.Id })); return response; }); }
internal static void CreateNewTodosList(string title, IEnumerable<TodoViewModel> todos) { var listModel = new TodolistModel() { Title = title, Todos = todos.Select(t => new TodoModel() { Text = t.Text }) }; var headers = new Dictionary<string, string>(); headers["X-accessToken"] = AccessToken; var response = HttpRequester.Post<ListCreatedModel>(BaseServicesUrl + "lists", listModel, headers); }
private void ValidateList(TodolistModel model) { if (model == null || string.IsNullOrEmpty(model.Title)) { throw new ArgumentNullException("Invalid TODO list title"); } }