public async Task <bool> AddSeason(SeasonResource seasonResource) { var exist = await _context.Seasons.AnyAsync(x => x.Name == seasonResource.Name); if (exist) { return(false); } var season = _mapper.Map <Season>(seasonResource); season.DateStart = DateTime.Now; season.Active = true; var seasonSelected = await IsSelected(); if (seasonSelected == null) { season.Selected = true; } else if (season.Selected) { seasonSelected.Selected = false; } _context.Seasons.Add(season); return(await _context.SaveChangesAsync() > 0); }
private static Season MapSeason(SeasonResource seasonResource) { return(new Season { SeasonNumber = seasonResource.SeasonNumber, Images = seasonResource.Images.Select(MapImage).ToList() }); }
public IActionResult Add(SeasonResource season) { if (!ModelState.IsValid) { return(RedirectToAction("New")); } return(RedirectToAction("Seasons")); }
public async Task <IActionResult> Create([FromBody] SeasonResource model) { try { bool dochange = false; if (!_context.Seasons.Any(x => x.Key == model.SeasonKey)) { dochange = true; var season = new Season() { Key = model.SeasonKey, Name = model.Name, TournamentName = model.TournamentName, StartDate = model.StartDate, EndDate = model.EndDate, TournamentKey = model.TournamentKey, CountryKey = model.CountryKey, RegionKey = model.RegionKey, StageKey = model.StageKey }; _context.Seasons.Add(season); await _newSeasonRegistrationBus.SendEvent(BuildNewSeasonEvent(model.TournamentKey, model.SeasonKey, model.StageKey, model.RegionKey, model.CountryKey, model.Name)); } else { Conflict($"The key '{model.SeasonKey}' already exists!"); } if (dochange) { await _context.SaveChangesAsync(); } return(Ok(model)); } catch (DbUpdateException pkex) { // TODO: we are seeing this occaisionally due to async processing from multiple instances // its ok to swallow as we dont support data updates and if the key exists there is no need for dupe store return(Conflict($"A primary key violation occured while saving player data: { pkex.Message }")); } }
// Add new season public async Task <IActionResult> New(SeasonResource season) { var listSeasons = await _seasonRepo.GetSeasons(); ViewBag.Currency = await _currencyRepository.GetCurrency(); if (!ModelState.IsValid) { ViewBag.Error = "Season name is required."; return(View("Seasons", listSeasons)); } if (!await _seasonRepo.AddSeason(season)) { ViewBag.Error = "There is already a season with the same name in database."; return(View("Seasons", listSeasons)); } return(RedirectToAction("Seasons")); }