private GameEntity ValidateAddSetToGameCommand(AddSetToGameCommand command) { if (command.GameId == Guid.Empty) { throw new Exception($"{nameof(command.GameId)} cannot be empty!"); } if (command.SetId == Guid.Empty) { throw new Exception($"{nameof(command.SetId)} cannot be empty!"); } var game = _dbContext.Games.Include(x => x.Sets).SingleOrDefault(t => t.Id == command.GameId); if (game == null) { throw new Exception($"The team '{command.GameId}' was not found!"); } if (game.Sets.Count == MaxSets) { throw new Exception($"The game '{command.GameId}' cannot have more than 3 sets!"); } if (game.Sets.Any() && !game.Sets.All(s => s.FirstTeamResult == MaxGoals || s.SecondTeamResult == MaxGoals)) { throw new Exception($"A new set cannot be added to the game '{command.GameId}' because the last one has not beed finished yet!"); } return(game); }
public async Task Execute(AddSetToGameCommand command) { var game = ValidateAddSetToGameCommand(command); game.Sets.Add(new SetEntity { Id = command.SetId, Parent = game, ParentId = game.Id }); await _dbContext.SaveChangesAsync(); }