public override async Task <ApplicationRequestResult> Handle( CreateRacePickCommand request, CancellationToken cancellationToken) { var raceId = request.RaceId; var ownerId = request.OwnerId; if (RaceEntryExists(raceId, ownerId)) { return(BadRequest()); } var raceExists = await _dbContext.Races.AnyAsync(x => x.Id == raceId, cancellationToken : cancellationToken); var ownerExists = await _dbContext.Owners.AnyAsync(x => x.Id == ownerId, cancellationToken : cancellationToken); if (!raceExists || !ownerExists) { return(NotFound()); } var racePick = new RacePick() { RaceId = raceId, OwnerId = ownerId, }; await _dbContext.RacePicks.AddAsync(racePick, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); return(Success()); }
public override async Task <ApplicationRequestResult> Handle(SetRacePickCommand request, CancellationToken cancellationToken) { var ownerId = request.OwnerId; var raceId = request.RaceId; //var tier = request.Tier; //var newEntryId = request.NewEntryId; //var oldEntryId = request.OldEntryId; var raceEntryIds = request.RaceEntryIds; Console.WriteLine($"SetRacePickCommand {request}"); //if (string.IsNullOrWhiteSpace(tier)) return BadRequest(); if (raceEntryIds == null) { return(BadRequest()); } var raceOk = await _dbContext.Races.AnyAsync(x => x.Id == raceId, cancellationToken); var ownerOk = await _dbContext.Owners.AnyAsync(x => x.Id == ownerId, cancellationToken); //var entry = await _dbContext.RaceEntries.FindAsync(newEntryId); //var entryOk = newEntryId == null || entry != null; List <RaceEntry> entries = await _dbContext.RaceEntries .Where(x => raceEntryIds.Contains(x.Id)) .ToListAsync(); var entriesOk = raceEntryIds.Count == entries.Count; if (!raceOk || !ownerOk || !entriesOk) { return(NotFound()); } var racePick = await _dbContext.RacePicks .Include(x => x.RaceEntries) .Where(x => x.OwnerId == ownerId && x.RaceId == raceId) .FirstOrDefaultAsync(); if (racePick == null) { racePick = new RacePick() { RaceId = raceId, OwnerId = ownerId, RaceEntries = entries }; await _dbContext.RacePicks.AddAsync(racePick, cancellationToken); } else { racePick.RaceEntries = entries; } await _dbContext.SaveChangesAsync(cancellationToken); return(Success()); }