public async Task<RonSwansonQuoteDetailDto> Handle(CreateRonSwansonQuoteCommand command, CancellationToken token) { var quoteToCreate = command.Input.Quote; var lastId = await _context.RonSwansonQuotes .OrderBy(a => a.Id) .Select(a => a.Id) .LastAsync(token); //TODO: Add Custom Error Handling Here if (string.IsNullOrWhiteSpace(quoteToCreate)) { throw new Exception("Cannot create a quote from an empty string!"); } var quoteDto = new RonSwansonQuote { Id = lastId + 1, Quote = quoteToCreate, Created = Today, }; await _context.RonSwansonQuotes.AddAsync(quoteDto, token); await _context.SaveChangesAsync(token); return _mapper.Map<RonSwansonQuoteDetailDto>(quoteDto); }
// 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)); }