protected virtual async Task <T> Add <T>(T entity) where T : class { _context.Set <T>().Add(entity); await _context.SaveChangesAsync(); return(entity); }
public async Task HandleAsync(PublishJokeCommand command, CancellationToken cancellationToken = default) { IList <Flag> flags = command.Flags != null && command.Flags.Any() ? await _getFlagsByNamesQuery.HandleAsync(new GetFlagsByNamesQuery(command.Flags), cancellationToken) : null; IList <Category> categories = await _getCategoriesByNamesQuery.HandleAsync(new GetCategoriesByNamesQuery(new List <string> { command.Category }), cancellationToken); Joke joke = new Joke { CategoryId = categories.First().Id, CreateDate = DateTime.Now, JokeFlags = flags?.Select(f => new JokeFlag() { FlagId = f.Id, }).ToList(), Parts = command.Parts.Select((p, i) => new Part() { JokePart = p, Order = i + 1 }).ToList() }; await _context.Jokes.AddAsync(joke, cancellationToken); await _context.SaveChangesAsync(cancellationToken); }
public async Task HandleAsync(RateJokeCommand command, CancellationToken cancellationToken = default) { var joke = await _getJokeByIdQueryHandler.HandleAsync(new GetJokeByIdQuery(command.JokeId), cancellationToken); joke.Ratings.Add(new Rating() { JokeId = command.JokeId, JokeRating = command.Rating, CreateDate = DateTime.Now }); await _context.SaveChangesAsync(cancellationToken); }