public void AddVehicleToTrack(long trackId, RaceTrackInfo trackInfo) { var track = this.dal.GetRaceTrack(trackId); //this is not thread safe if (this.dal.getTrackVehicleCount(trackId) >= track.Capacity) { throw new Exception("Track is full"); } var requiredConditions = this.dal.getRequiredCondtionsForVehicleType(trackInfo.SelectedType); var inspectingCondtions = trackInfo.Conditions[trackInfo.SelectedType.Value]; if (inspectingCondtions.Any(c => !c.Checked)) { throw new Exception("All inspection conditions must be met"); } if (requiredConditions.Count() != inspectingCondtions.Count() || requiredConditions.Any(c => inspectingCondtions.All(rc => rc.Condition.Id != c.Id))) { throw new Exception("Trying to break the rules?"); } this.dal.AddToTrack(trackId, trackInfo.SelectedType, inspectingCondtions); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] RaceTrackInfo raceTrackInfo) { if (id != raceTrackInfo.Id) { return(NotFound()); } if (ModelState.IsValid) { try { await _service.UpdateAsync(raceTrackInfo); } catch (DbUpdateConcurrencyException) { if (!RaceTrackInfoExists(raceTrackInfo.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(raceTrackInfo)); }
public async Task <IActionResult> Create([Bind("Name")] RaceTrackInfo raceTrackInfo) { if (ModelState.IsValid) { await _service.AddAsync(raceTrackInfo); return(RedirectToAction(nameof(Index))); } return(View(raceTrackInfo)); }
public ActionResult Index(RaceTrackInfo model) { try { this.trackService.AddVehicleToTrack(model.RaceTrackId, model); } catch (Exception e) { ModelState.AddModelError("error", e.Message); return(View(this.trackService.getRaceTrackInfo())); } ModelState.Clear(); return(View(this.trackService.getRaceTrackInfo())); }
/// <summary> /// Updates a record from the database /// </summary> /// <param name="raceTrackInfo">A Race Track model to be modified</param> public async Task UpdateAsync(RaceTrackInfo raceTrackInfo) { try { var e = _context.Update(raceTrackInfo); e.State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (Exception ex) { throw; // In here we would log the exception and return an error message to the user } }
/// <summary> /// Adds the record to the database /// </summary> /// <param name="raceTrackInfo">A Race Track model</param> public async Task AddAsync(RaceTrackInfo raceTrackInfo) { try { await _context.AddAsync(raceTrackInfo); await _context.SaveChangesAsync(); } catch (Exception ex) { throw; // In here we would log the exception and return an error message to the user } }
public RaceTrackInfo getRaceTrackInfo(long?id = null) { var raceTrack = this.dal.GetRaceTrack(id); var requiredInspections = this.dal.GetVehicleTypeCondtionLookUps(); var VehicleTypes = this.dal.getAllowedVehicleTypes(raceTrack.Id); var conditions = this.dal.getConditions(); var vm = new RaceTrackInfo() { VehicleTypes = VehicleTypes, RaceTrackId = raceTrack.Id, RaceTrackCapacity = raceTrack.Capacity }; vm.Conditions = VehicleTypes.GroupJoin(requiredInspections, v => v, i => i.VehicleType, (v, i) => new { Key = v, Value = i }) .ToDictionary(o => o.Key, o => conditions.Where(c => o.Value.Any(r => r.ConditionCode == c.Id && r.VehicleType == o.Key)).Select(c => new CheckedCondition() { VehicleType = o.Key, Condition = c }).ToList()); return(vm); }