示例#1
0
        /// <summary>
        ///     Seed sample data in the Todo container
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static async Task SeedToDoContainerIfEmptyAsync(this IApplicationBuilder builder)
        {
            using (IServiceScope serviceScope = builder.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                IToDoItemRepository _repo = serviceScope.ServiceProvider.GetService <IToDoItemRepository>();

                // Check if empty
                string sqlQueryText          = "SELECT * FROM c";
                IEnumerable <ToDoItem> todos = await _repo.GetItemsAsync(sqlQueryText);

                if (todos.Count() == 0)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ToDoItem beer = new ToDoItem()
                        {
                            Category = "Grocery",
                            Title    = $"Get {i} beers"
                        };

                        await _repo.AddItemAsync(beer);
                    }
                }
            }
        }
            /// <summary>
            ///     Handle
            /// </summary>
            /// <param name="command"></param>
            /// <param name="cancellationToken"></param>
            /// <returns></returns>
            public async Task <CommandResponse> Handle(CreateCommand command, CancellationToken cancellationToken)
            {
                CommandResponse response = new CommandResponse();

                Core.Entities.ToDoItem entity = _mapper.Map <Core.Entities.ToDoItem>(command);
                await _repo.AddItemAsync(entity);

                response.Id = entity.Id;
                return(response);
            }