示例#1
0
        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);
        }
示例#2
0
        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));
        }
示例#3
0
        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()));
 }
示例#5
0
 /// <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
     }
 }
示例#6
0
        /// <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
            }
        }
示例#7
0
        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);
        }