public ToDoItemDTO CreateItemInList(Guid listId, ToDoItemDTO itemDTO) { _logger.LogInformation("ToDoItemService -> Executing CreateItemInList()"); var toDoList = _context.ToDoLists .SingleOrDefault(todo => todo.Id.Equals(listId));; if (toDoList == null) { throw new EntityNotFoundException(); } //create item from dto ToDoItem itemToAdd = itemDTO.CreateEntity(listId); //each new Item receives the lowest position of all items in a list itemToAdd.Position = _context.ToDoItems.Where(x => x.ToDoListId.Equals(listId)).Count(); //add the new item to list and update database toDoList.Items.Add(itemToAdd); _context.SaveChanges(); //update dto data itemDTO.ItemId = itemToAdd.Id; itemDTO.Position = itemToAdd.Position; return(itemDTO); }