public static HistoryModel ToHistoryModel(this IMapper mapper, CreateHistoryDto historyDto) { var historyModel = mapper.Map <CreateHistoryDto, HistoryModel>(historyDto); historyModel.ResultKindAwayTeam = GetAwayTeamResultByScore(historyModel.GoalsHomeTeam, historyModel.GoalsAwayTeam); historyModel.ResultKindHomeTeam = GetHomeTeamResultByScore(historyModel.GoalsHomeTeam, historyModel.GoalsAwayTeam); return(historyModel); }
public async Task <HistoryDto> CreateHistory(CreateHistoryDto createHistoryDto) { if (false == IsModelValid(createHistoryDto, out string issues)) { throw new ArgumentException($"History model is not valid. Issues: {issues}"); } var awayTeam = await _context.Teams .FirstOrDefaultAsync((team) => team.Id == createHistoryDto.AwayTeamId); var homeTeam = await _context.Teams .FirstOrDefaultAsync((team) => team.Id == createHistoryDto.HomeTeamId); var playerAwayTeam = await _context.Players .FirstOrDefaultAsync((player) => player.Id == createHistoryDto.PlayerAwayTeamId); var playerHomeTeam = await _context.Players .FirstOrDefaultAsync((player) => player.Id == createHistoryDto.PlayerHomeTeamId); if (null == createHistoryDto.GameDateUTC) { createHistoryDto.GameDateUTC = DateTime.UtcNow; } var historyToBeCreated = _mapper.ToHistoryModel(createHistoryDto); var createdHistory = await _context.Histories.AddAsync(historyToBeCreated); await _context.SaveChangesAsync(); var historyDto = _mapper.ToHistoryDto(createdHistory.Entity); historyDto.AwayTeam = _mapper.Map <TeamModel, TeamDto>(awayTeam); historyDto.HomeTeam = _mapper.Map <TeamModel, TeamDto>(homeTeam); historyDto.PlayerAwayTeam = _mapper.Map <PlayerModel, PlayerDto>(playerAwayTeam); historyDto.PlayerHomeTeam = _mapper.Map <PlayerModel, PlayerDto>(playerHomeTeam); return(historyDto); }
private bool IsModelValid(CreateHistoryDto createHistoryDto, out string issues) { issues = ""; return(true); }