public static IEnumerable <RaceDetail> ToRaceDetails(this IEnumerable <Race> races, IEnumerable <Bet> bets) { foreach (var race in races) { if (bets == null) { yield break; } var raceBets = bets.Where(x => x.RaceId == race.Id); var newRaceDetail = new RaceDetail(race, raceBets) { HorseRaces = race.Horses.Select(x => { var raceBetsOnHorse = raceBets.Where(y => y.HorseId == x.Id).ToList(); var horseRace = new HorseRace { Horse = x, RaceId = race.Id, NumberOfBets = raceBetsOnHorse.Count, TotalBet = raceBetsOnHorse.Sum(i => i.Stake) }; horseRace.OwingOnWin = horseRace.TotalBet * x.Odds; return(horseRace); }) }; yield return(newRaceDetail); } }
public int Update_RaceResultsView(int employeeid, int raceid, List <RaceResultsView> items) { using (var context = new RaceContext()) { foreach (RaceResultsView item in items) { if (item.Time == null) { errors.Add("Times must be in the format hh:mm:ss."); break; } } foreach (RaceResultsView item in items) { if (item.Time != null & item.Time < new TimeSpan(00, 00, 00)) { errors.Add("Times must be positive."); break; } } if (errors.Count == 0) { items = items.OrderBy(x => x.Time).ToList(); int?placement = 0; int?penalties = null; foreach (RaceResultsView item in items) { if (item.Time != new TimeSpan(00, 00, 00)) { placement++; } else { item.Time = null; } if (item.Penalties != 0) { penalties = item.Penalties; } RaceDetail newitem = (from x in context.RaceDetails where x.RaceDetailID == item.RaceDetailID select x).FirstOrDefault(); newitem.Place = placement == 0 ? null : placement; newitem.RunTime = item.Time; newitem.PenaltyID = penalties; context.Entry(newitem).State = System.Data.Entity.EntityState.Modified; } } if (errors.Count == 0) { return(context.SaveChanges()); } else { throw new BusinessRuleException("Race Time Validation Error.", errors); } } }
public int Driver_Update(RaceDetail driver) { using (var context = new eRaceContext()) { context.Entry(driver).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } }
public int Driver_Add(RaceDetail driver) { using (var context = new eRaceContext()) { //yet to develop the business logic and error handling context.RaceDetails.Add(driver); //staging context.SaveChanges(); //committed return(driver.RaceDetailID); //return new id value } }
public InvoiceViewModel Add_Racer(RosterViewModel roster, string employeeName) { using (var context = new ERaceingSystemContext()) { decimal rentalFee = (from x in context.Cars where x.CarID == roster.CarID select x.CarClass.RaceRentalFee).FirstOrDefault(); decimal subTotal = roster.RaceFee + rentalFee; decimal GST = decimal.Multiply(subTotal, (decimal)0.05); int?employeeID = (from x in context.AspNetUsers where x.UserName == employeeName select x.EmployeeId).FirstOrDefault(); Invoice invoice = new Invoice() { EmployeeID = (int)employeeID, InvoiceDate = DateTime.Now, SubTotal = subTotal, GST = GST, Total = decimal.Add(subTotal, GST) }; context.Invoices.Add(invoice); RaceDetail info = new RaceDetail() { InvoiceID = invoice.InvoiceID, RaceID = roster.RaceID, MemberID = roster.MemberID, RaceFee = roster.RaceFee, RentalFee = rentalFee, }; if (roster.CarID != 0) { var fee = (from x in context.Cars where x.CarID == roster.CarID select x.CarClass.RaceRentalFee).FirstOrDefault(); info.RentalFee = fee; info.CarID = roster.CarID; } context.RaceDetails.Add(info); context.SaveChanges(); InvoiceViewModel createdInvoice = new InvoiceViewModel() { InvoiceID = invoice.InvoiceID, EmployeeID = (int)employeeID, Subtotal = invoice.SubTotal, GST = invoice.GST, Total = decimal.Add(invoice.SubTotal, invoice.GST) }; return(createdInvoice); } }
public static RaceBetViewModel ToViewModel(this RaceDetail raceDetail) { return((raceDetail == null) ? null : new RaceBetViewModel { Id = raceDetail.Race.Id, Name = raceDetail.Race.Name, Status = raceDetail.Race.Status, RaceTotalBet = raceDetail.RaceTotalBet, HorseRace = raceDetail.HorseRaces.Select(x => new HorseRaceViewModel { HorseId = x.Horse.Id, HorseName = x.Horse.Name, RaceId = x.RaceId, NumberOfBets = x.NumberOfBets, TotalBet = x.TotalBet, OwingOnWin = x.OwingOnWin }).ToList() }); }
public void Add_DriverToRaceAndGenerateInvoice(int raceid, int memberid, int carid, int employeeid, decimal racefee, decimal rentalfee) { using (var context = new eRaceContext()) { List <string> reasons = new List <string>(); RaceDetail exists = (from x in context.RaceDetails where x.MemberID == memberid select x).FirstOrDefault(); if (exists == null) { exists = new RaceDetail(); exists.RaceID = raceid; exists.MemberID = memberid; exists.RaceFee = racefee; exists.RentalFee = rentalfee; exists.CarID = carid; exists = context.RaceDetails.Add(exists); } else { reasons.Add("Driver already registered for this race"); } if (reasons.Count() > 0) { throw new BusinessRuleException("Adding driver to race", reasons); } else { Invoice newInvoice = new Invoice(); newInvoice.EmployeeID = employeeid; newInvoice.InvoiceDate = DateTime.Today; newInvoice.SubTotal = racefee + rentalfee; context.Invoices.Add(newInvoice); context.SaveChanges(); } } }
public void Edit_Roster(RosterViewModel roster) { using (var context = new ERaceingSystemContext()) { RaceDetail oldRaceDetail = (from x in context.RaceDetails where x.RaceDetailID == roster.RaceDetailID select x).FirstOrDefault(); if (roster.CarID != 0) { var fee = (from x in context.Cars where x.CarID == roster.CarID select x.CarClass.RaceRentalFee).SingleOrDefault(); oldRaceDetail.RentalFee = fee; } oldRaceDetail.CarID = roster.CarID; oldRaceDetail.Comment = roster.RaceDetailComment; oldRaceDetail.RefundReason = roster.RefundReason; oldRaceDetail.Refund = roster.Refund; context.SaveChanges(); } }
public int Update_RosterView(int employeeid, int raceid, RosterView item) { using (var context = new RaceContext()) { RaceDetail newitem = new RaceDetail(); Invoice newinvoice = new Invoice(); if (item.CarID != 0 && !(from x in context.Cars where x.CarClass.CertificationLevel == (from y in context.RaceDetails where y.RaceDetailID == item.RaceDetailID select y.Race.CertificationLevel).FirstOrDefault() select x.CarID).Contains((int)item.CarID)) { errors.Add("CarID must have proper certification level."); } if (item.Refunded) { if (string.IsNullOrEmpty(item.RefundReason)) { errors.Add("Refunds require a refund reason."); } else { newitem = new RaceDetail { RaceDetailID = item.RaceDetailID, RaceID = raceid, MemberID = (from x in context.Members where x.FirstName + " " + x.LastName == item.Name select x.MemberID).FirstOrDefault(), RaceFee = 0, CarID = null, RentalFee = 0, Place = null, Refund = true, Comment = item.Comment, RefundReason = item.RefundReason }; decimal subtotal = item.RaceFee; newinvoice = new Invoice { InvoiceDate = DateTime.Now.Date, EmployeeID = employeeid, SubTotal = subtotal, GST = subtotal * (decimal)0.05, Total = subtotal * (decimal)1.05 }; context.Invoices.Add(newinvoice); } } else { newitem = (from x in context.RaceDetails where x.RaceDetailID == item.RaceDetailID select x).FirstOrDefault(); newitem.CarID = item.CarID == 0 ? null : item.CarID; newitem.RentalFee = item.CarID == 0 ? 0 : (from x in context.CarClasses where x.CarClassID == (from y in context.Cars where y.CarID == item.CarID select y.CarClassID).FirstOrDefault() select x.RaceRentalFee).FirstOrDefault(); newitem.Refund = false; newitem.Comment = item.Comment; newitem.RefundReason = item.RefundReason; } if (errors.Count == 0) { context.Entry(newitem).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } else { throw new BusinessRuleException("Update Validation Error", errors); } } }
public int Insert_RosterView(int employeeid, int raceid, RosterView item) { using (var context = new RaceContext()) { RaceDetail newitem = new RaceDetail(); Invoice newinvoice = new Invoice(); if ((from x in context.RaceDetails where x.RaceID == raceid && x.Refund == false select x).Count() >= (from x in context.Races where x.RaceID == raceid select x.NumberOfCars).FirstOrDefault()) { errors.Add("Race contestant limit has already been reached."); } if (item.Name == "0") { errors.Add("Please select a driver."); } List <int> members = (from x in context.RaceDetails where x.RaceID == raceid && x.Refund == false select x.MemberID).ToList(); if (members.Contains(int.Parse(item.Name))) { errors.Add("Member cannot be entered in a race twice."); } string certif = (from y in context.Races where y.RaceID == raceid select y.CertificationLevel).FirstOrDefault(); if (item.CarID != 0 && !(from x in context.Cars where x.CarClass.CertificationLevel == certif select x.CarID).Contains((int)item.CarID)) { errors.Add("CarID must have proper certification level."); } if (errors.Count == 0) { newitem = new RaceDetail { RaceID = raceid, MemberID = int.Parse(item.Name), RaceFee = (decimal)(from x in context.RaceDetails where x.RaceID == raceid && x.RaceFee != 0 select x.RaceFee).FirstOrDefault(), CarID = item.CarID == 0 ? null : item.CarID, RentalFee = item.CarID == 0 ? 0 : (from x in context.CarClasses where x.CarClassID == (from y in context.Cars where y.CarID == item.CarID select y.CarClassID).FirstOrDefault() select x.RaceRentalFee).FirstOrDefault(), Place = item.Placement, Refund = false, Comment = item.Comment, RefundReason = item.RefundReason }; decimal subtotal = (from x in context.RaceDetails where x.RaceDetailID == item.RaceDetailID select x.Invoice.SubTotal).FirstOrDefault(); newinvoice = new Invoice { InvoiceDate = DateTime.Now.Date, EmployeeID = employeeid, SubTotal = subtotal, GST = subtotal * (decimal)0.05, Total = subtotal * (decimal)1.05 }; context.RaceDetails.Add(newitem); context.Invoices.Add(newinvoice); context.SaveChanges(); return(newitem.RaceDetailID); } else { throw new BusinessRuleException("Insert Validation Error", errors); } } }