public async Task <IActionResult> Edit(int id, [Bind("JockeyId,Name,Address,Age,Rating")] Jockeys jockeys) { if (id != jockeys.JockeyId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(jockeys); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!JockeysExists(jockeys.JockeyId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(jockeys)); }
/// <summary> /// subscribed to event /// loads horses from the file data services /// async void eventhandler testing credits: https://stackoverflow.com/a/19415703/11027921 /// delegates https://docs.microsoft.com/en-us/dotnet/api/system.eventhandler?view=netframework-4.8 /// </summary> public async void OnLoadAllDataAsync() { string callersName = GetCallerName(); CommandStartedControlsSetup(callersName); if (Horses.Count == 0 && Jockeys.Count == 0 && Races.Count == 0) { List <LoadedHorse> horses = await _dataServices.GetAllHorsesAsync(); List <LoadedJockey> jockeys = await _dataServices.GetAllJockeysAsync(); List <RaceDetails> races = await _dataServices.GetAllRacesAsync(); foreach (var horse in horses) { Horses.Add(new LoadedHorse { Name = horse.Name, Age = horse.Age, AllRaces = horse.AllRaces, AllChildren = horse.AllChildren, Father = horse.Father, Link = horse.Link, FatherLink = horse.FatherLink }); } Horses = Horses.OrderBy(o => o.Age).ToList(); foreach (var jockey in jockeys) { Jockeys.Add(new LoadedJockey { Name = jockey.Name, AllRaces = jockey.AllRaces, Link = jockey.Link }); } foreach (var race in races) { Races.Add(new RaceDetails { RaceCategory = race.RaceCategory, RaceDate = race.RaceDate, RaceDistance = race.RaceDistance, RaceLink = race.RaceLink, HorseList = race.HorseList }); } } PopulateLists(); CommandCompletedControlsSetup(); ResetControls(); }
/// <summary> /// updates list of jockey races, if found some new /// </summary> /// <param name="doubledJockey">jockey for Jockeys</param> /// <param name="jockey">found doubler</param> public void MergeJockeysData(LoadedJockey doubledJockey, LoadedJockey jockey) { if (jockey.AllRaces != null) { doubledJockey.AllRaces = doubledJockey.AllRaces.Union(jockey.AllRaces).ToList(); } Jockeys.Add(doubledJockey); }
public async Task <IActionResult> Create([Bind("JockeyId,Name,Address,Age,Rating")] Jockeys jockeys) { if (ModelState.IsValid) { _context.Add(jockeys); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(jockeys)); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { Contests.Dispose(); ContestParticipants.Dispose(); Horses.Dispose(); HorseOwners.Dispose(); Racecourses.Dispose(); HorseFactories.Dispose(); Jockeys.Dispose(); Trainers.Dispose(); }
/// <summary> /// updates single jockey object /// gets data from scrap service /// compares with objects in current database /// calls merge method if object already in database /// </summary> /// <param name="jobType">type of scrapping</param> /// <param name="id">id on the website</param> /// <returns></returns> private async Task UpdateJockeysAsync(string jobType, int id) { LoadedJockey jockey = new LoadedJockey(); jockey = await _scrapDataService.ScrapGenericObject <LoadedJockey>(id, jobType); if (jockey != null) { lock (((ICollection)Jockeys).SyncRoot) { //if objects are already in the List if (Jockeys.Any(h => h.Name.ToLower() == jockey.Name.ToLower())) { LoadedJockey doubledJockey = Jockeys.Where(h => h.Name.ToLower() == jockey.Name.ToLower()).FirstOrDefault(); Jockeys.Remove(doubledJockey); MergeJockeysData(doubledJockey, jockey); } else { Jockeys.Add(jockey); } } } }
/// <summary> /// updates collections of generic type /// I/O operations /// creates loads of parallel tasks to improve performance speed /// use of SemaphoreSlim to control tasks /// SemaphoreSlim credits: https://blog.briandrupieski.com/throttling-asynchronous-methods-in-csharp /// SemaphoreSlim corrections: https://stackoverflow.com/a/56641448/11027921 /// Task.Run corrections: https://stackoverflow.com/a/56631208/11027921 /// credits for SemaphoreSlim cancellation: https://stackoverflow.com/a/24099764/11027921 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="genericCollection">collection parameter</param> /// <param name="idFrom">object id limitation</param> /// <param name="idTo">object id limitation</param> /// <param name="jobType">type of scrapping</param> /// <returns></returns> public async Task <List <T> > UpdateDataAsync <T>(List <T> genericCollection, int idFrom, int idTo, string jobType) { ProgressBarData progressBar; int loopCounterProgressBar = 0; string jobTypeProgressBar = ""; //parse collections, display job if (typeof(T) == typeof(LoadedHorse)) { Horses = new List <LoadedHorse>(genericCollection.Cast <LoadedHorse>()); jobTypeProgressBar = "Updating horse data"; } else if (typeof(T) == typeof(LoadedJockey)) { Jockeys = new List <LoadedJockey>(genericCollection.Cast <LoadedJockey>()); jobTypeProgressBar = "Updating jockey data"; } else if (typeof(T) == typeof(RaceDetails)) { Races = new List <RaceDetails>(genericCollection.Cast <RaceDetails>()); jobTypeProgressBar = "Updating historic data"; } //initial SemaphoreSlim throttler = new SemaphoreSlim(_degreeOfParallelism); List <Task> tasks = new List <Task>(); TokenSource = new CancellationTokenSource(); CancellationToken = TokenSource.Token; progressBar = GetProgressBar(jobType, idFrom, idTo, loopCounterProgressBar); progressBar = new ProgressBarData() { JobType = jobType, FromId = idFrom, ToId = idTo, LoopCouner = loopCounterProgressBar }; _eventAggregator.GetEvent <ProgressBarEvent>().Publish(progressBar); //run loop for (int i = idFrom; i < idTo + 1; i++) { int id = i; if (CancellationToken.IsCancellationRequested) { break; } //create loads of parallel tasks tasks.Add(Task.Run(async() => { await throttler.WaitAsync(); try { if (CancellationToken.IsCancellationRequested) { return; } if (typeof(T) == typeof(LoadedHorse)) { await UpdateHorsesAsync(jobType, id); } else if (typeof(T) == typeof(LoadedJockey)) { await UpdateJockeysAsync(jobType, id); } else if (typeof(T) == typeof(RaceDetails)) { await UpdateRacesAsync(jobType, id); } } catch { MessageBox.Show(id.ToString()); } finally { loopCounterProgressBar++; progressBar = GetProgressBar(jobTypeProgressBar, idFrom, idTo, loopCounterProgressBar); _eventAggregator.GetEvent <ProgressBarEvent>().Publish(progressBar); throttler.Release(); } })); } try { await Task.WhenAll(tasks); } finally { //save when finish if (typeof(T) == typeof(LoadedJockey)) { await _dataServices.SaveAllJockeysAsync(Jockeys.ToList()); } else if (typeof(T) == typeof(LoadedHorse)) { await _dataServices.SaveAllHorsesAsync(Horses.ToList()); } else if (typeof(T) == typeof(RaceDetails)) { if (jobType.Contains("Historic")) { await _dataServices.SaveAllRaces(Races.ToList()); } else if (jobType.Contains("testRaces")) { await _dataServices.SaveRaceSimulatedResultsAsync(Races.ToList()); } } } if (typeof(T) == typeof(LoadedHorse)) { return((List <T>)Convert.ChangeType(Horses, typeof(List <T>))); } else if (typeof(T) == typeof(LoadedJockey)) { return((List <T>)Convert.ChangeType(Jockeys, typeof(List <T>))); } else if (typeof(T) == typeof(RaceDetails)) { return((List <T>)Convert.ChangeType(Races, typeof(List <T>))); } else { throw new ArgumentException(); } }