Example #1
0
        public async Task <IHttpActionResult> Update(int id, [FromBody] TodoUpdate todoUpdate)
        {
            if (todoUpdate == null || ModelState.IsValid == false)
            {
                return(BadRequest());
            }
            var foundTodo = await _todoDbContext.Todoes.SingleOrDefaultAsync(todo => todo.Id == id);

            if (foundTodo == null)
            {
                return(NotFound());
            }
            _todoDbContext.Entry(foundTodo).CurrentValues.SetValues(todoUpdate);
            await _todoDbContext.SaveChangesAsync();

            return(ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)));
        }
Example #2
0
        public IActionResult Put(int id, [FromBody] TodoUpdate model)
        {
            if (model == null || string.IsNullOrEmpty(model.Name))
            {
                return(BadRequest());
            }

            var todo = TodoList.FirstOrDefault(t => t.Id == id);

            if (todo == null)
            {
                return(NotFound());
            }

            todo.Name      = model.Name;
            todo.Completed = model.Completed;

            return(Ok(todo));
        }
Example #3
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Description,IsCompleted")] Todo todo)
        {
            if (!ModelState.IsValid)
            {
                View(todo);
            }
            TodoUpdate todoUpdate = new TodoUpdate
            {
                IsCompleted = todo.IsCompleted,
                Description = todo.Description
            };

            var         todoUpdateSerialized = JsonConvert.SerializeObject(todoUpdate);
            HttpContent content = new StringContent(todoUpdateSerialized, Encoding.UTF8, MimeTypeApplicationJson);
            await _httpClient.SendAsync(
                new HttpRequestMessage(new HttpMethod("PATCH"), $"{TodoResourceName}/{todo.Id}")
            {
                Content = content
            });

            return(RedirectToAction("Index"));
        }