Пример #1
0
        public static void Inititalize(IServiceProvider serviceProvider)
        {
            var context = serviceProvider.GetService<TodoListContext>();
            if (!context.Lists.Any())
            {
                var list = new TodoList
                {
                    Token = Guid.NewGuid().ToString()
                };
                list.TodoListItems.AddRange(new List<TodoListItem> { 
                    new TodoListItem()
                    {
                        LastModified = DateTime.Now,
                        IsCompleted = false,
                        Item = "Test this app!"
                    },
                    new TodoListItem()
                    {
                        LastModified = DateTime.Now,
                        IsCompleted = false,
                        Item = "Can you even mark this completed?"
                    }});

                context.Lists.Add(list);
                context.SaveChanges();
            }
        }
        public TodoListItem Add([FromRoute] string token, [FromBody] TodoListItem item)
        {
            var foundList = TodoContext.Lists.Include(t => t.TodoListItems).FirstOrDefault(t => t.Token == token);
            if (foundList == null)
            {
                foundList = new TodoList
                {
                    Token = token
                };
                TodoContext.Lists.Add(foundList);
            }

            foundList.TodoListItems.Add(item);
            TodoContext.SaveChanges();

            return item;
        }