public ActionResult DriverSummary()
        {
            ViewBag.Message = "Driver Summary";
            RacingContext        raceContext   = new RacingContext();
            List <RacingSummary> DriverDetails = (from driver in raceContext.Driver
                                                  select new RacingSummary
            {
                DriverId = driver.DriverId,
                DriverName = driver.DriverName,
                DriverAge = driver.DriverAge,
                DriverCountry = driver.DriverCountry,
                RacesLost = driver.RacesLost,
                RacesWon = driver.RacesWon,
            }).ToList();

            return(View(DriverDetails));
        }
示例#2
0
        public async Task AllSupported_NodatimeTypes_AreSavedCorrectly()
        {
            await using var transaction = await this.Db.Database.BeginTransactionAsync();

            var sut = new SupportedNodaTypes();

            await this.Db.AddAsync(sut);

            await this.Db.SaveChangesAsync();

            var newContext = new RacingContext(this.DbContextOptions);
            await newContext.Database.UseTransactionAsync(transaction.GetDbTransaction());

            var raceResultFromDb = await newContext.TypeResults.FirstOrDefaultAsync(x => x.Id == sut.Id);

            Assert.NotNull(raceResultFromDb);

            await transaction.RollbackAsync();
        }
示例#3
0
        public async Task Instant_EnsurePrecisionIsSavedCorrectly()
        {
            using var transaction = await this.Db.Database.BeginTransactionAsync();

            var raceResult = new RaceResult
            {
                StartTime = SystemClock.Instance.GetCurrentInstant(),
            };

            this.Db.Add(raceResult);
            await this.Db.SaveChangesAsync();

            var newContext = new RacingContext(this.DbContextOptions);

            newContext.Database.UseTransaction(transaction.GetDbTransaction());

            var raceResultFromDb = await newContext.RaceResult.FirstOrDefaultAsync(x => x.Id == raceResult.Id);

            var dateTimeString = raceResult.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture);

            Assert.Equal(dateTimeString, raceResultFromDb.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture));

            await transaction.RollbackAsync();
        }
        public ActionResult Index()
        {
            RacingContext raceContext = new RacingContext();

            //Retrive from DB
            //new changes
            List <RacingSummary> racingDetails = (from driver in raceContext.Driver
                                                  join racing in raceContext.Racing
                                                  on driver.DriverId equals racing.DriverId
                                                  join city in raceContext.City
                                                  on racing.CityId equals city.CityId
                                                  select new RacingSummary
            {
                DriverId = driver.DriverId,
                DriverName = driver.DriverName,
                DriverAge = driver.DriverAge,
                DriverCountry = driver.DriverCountry,
                Category = racing.Category,
                Car = racing.Car,
                TrackId = racing.TrackId,
                CityName = city.CityName,
                TopSpeed = racing.TopSpeed,
                CompletionTime = racing.CompletionTime,
                Status = racing.Status,
                RaceDate = racing.RaceDate,
                Rank = 0,
                RacesLost = driver.RacesLost,
                RacesWon = driver.RacesWon
            }).ToList();

            //Ranking based on completion time
            var rank = (racingDetails.GroupBy(d => new { d.CityName, d.RaceDate })
                        .SelectMany(g => g.OrderBy(y => y.CompletionTime)
                                    .Select((x, i) => new { g.Key, Item = x, Rank = i + 1 })));


            var RankedRace = rank.ToList();

            //Update Status, Won and Lose
            for (int i = 0; i < RankedRace.Count; i++)
            {
                if (RankedRace[i].Rank == 1)
                {
                    RankedRace[i].Item.Status    = "Win";
                    RankedRace[i].Item.Rank      = RankedRace[i].Rank;
                    RankedRace[i].Item.RacesWon  = (RankedRace[i].Item.RacesWon) + 1;
                    RankedRace[i].Item.RacesLost = (RankedRace[i].Item.RacesLost);
                }
                else
                {
                    RankedRace[i].Item.Status    = "Lost";
                    RankedRace[i].Item.Rank      = RankedRace[i].Rank;
                    RankedRace[i].Item.RacesLost = (RankedRace[i].Item.RacesLost) + 1;
                    RankedRace[i].Item.RacesWon  = (RankedRace[i].Item.RacesWon);
                }
            }



            var WinLostSummary = racingDetails.GroupBy(d => d.DriverId)
                                 .SelectMany(g => g.OrderBy(y => y.DriverId)
                                             .Select((x, i) => new { g.Key, Item = x, Rank = i + 1 })).ToList();

            //Update in DB
            for (int i = 0; i < WinLostSummary.Count; i++)
            {
                int id         = WinLostSummary[i].Item.DriverId;
                var DriverData = raceContext.Driver.Where(x => x.DriverId == id).FirstOrDefault();
                if (DriverData != null)
                {
                    DriverData.RacesWon  = WinLostSummary[i].Item.RacesWon;
                    DriverData.RacesLost = WinLostSummary[i].Item.RacesLost;
                    raceContext.SaveChanges();
                }
            }

            return(View(racingDetails));
        }
 public TournamentsController(RacingContext context)
 {
     _context = context;
 }