public IActionResult AddValue(int itemId, [FromBody] TodoItemValue itemValue) { try { if (itemValue == null || itemId.Equals(null)) { return(BadRequest()); } var todo = _context.TodoItems.FirstOrDefault(t => t.Id == itemId); if (todo == null) { return(NotFound()); } if (todo.TodoItemValues == null) { todo.TodoItemValues = new List <TodoItemValue>(); } todo.TodoItemValues.Add(itemValue); _context.TodoItems.Update(todo); _context.SaveChanges(); return(CreatedAtRoute("GetTodo", new { id = todo.Id, name = todo.Name, isComplete = todo.IsComplete, values = todo.TodoItemValues }, _context.TodoItems.Where(item => item.Id == itemId).Select(item => item.TodoItemValues))); } catch (Exception e) { return(BadRequest("Wrong request: " + e.Message)); } }
public IActionResult UpdateValue(int itemId, [FromBody] TodoItemValue value) { try { if (value == null) { return(BadRequest()); } var item = _context.TodoItems.Include(ti => ti.TodoItemValues).FirstOrDefault(t => t.Id == itemId); if (item == null) { return(NotFound()); } var val = item.TodoItemValues.FirstOrDefault(v => v.Id == value.Id); if (val == null) { AddValue(itemId, value); } else { val.Value = value.Value; } _context.TodoItems.Update(item); _context.SaveChanges(); return(new ObjectResult($"New value is '{val?.Value}' in item {item.Id} for value {val?.Id}")); } catch (Exception e) { return(BadRequest("Wrong request: " + e.Message)); } }