public async Task <List <RonSwansonQuoteDetailDto> > BulkCreate( [FromBody] List <CreateRonSwansonQuoteInputDto> inputList, CancellationToken token) { var command = new BulkCreateRonSwansonQuoteCommand(inputList); return(await _commandDispatcher.Execute(command, token)); }
// TODO: FINISH IT!! public async Task <List <RonSwansonQuoteDetailDto> > Handle(BulkCreateRonSwansonQuoteCommand command, CancellationToken token) { //Converting to Hashset gives a slight performance boost and removes any duplicates var quoteSet = command.InputList .Select(a => a.Quote) .ToHashSet(); // Remove any null values (there can be at max one) // Maybe this should be handled with an exception instead? quoteSet.RemoveWhere(string.IsNullOrWhiteSpace); // Gets the last row Id currently in the database var lastId = await _context.RonSwansonQuotes .OrderBy(a => a.Id) .Select(a => a.Id) .LastOrDefaultAsync(token); var quoteList = new List <RonSwansonQuote>(); foreach (var quote in quoteSet) { lastId += 1; quoteList.Add(new RonSwansonQuote { Id = lastId, Quote = quote, Created = DateTime.Today }); } await _context.RonSwansonQuotes.AddRangeAsync(quoteList, token); await _context.SaveChangesAsync(token); return(_mapper.Map <List <RonSwansonQuoteDetailDto> >(quoteList)); }