public async Task SaveNew(FleetWithOptionsViewModel fleet) { var coreModel = _mapper.Map <Fleet>(fleet); if (fleet.FleetType == Api.Enumerations.FleetType.SelectedClasses && fleet.BoatClassIds != null) { coreModel.BoatClasses = (await _coreClubService.GetAllBoatClasses(fleet.ClubId)) .Where(c => fleet.BoatClassIds.Contains(c.Id)) .ToList(); } else if (fleet.FleetType == Api.Enumerations.FleetType.SelectedBoats && fleet.CompetitorIds != null) { coreModel.Competitors = (await _coreCompetitorService.GetCompetitorsAsync(fleet.ClubId, null, false)) .Where(c => fleet.CompetitorIds.Contains(c.Id)) .ToList(); } var fleetId = await _coreFleetService.SaveNew(coreModel); if (fleet.RegattaId.HasValue) { await _regattaService.AddFleetToRegattaAsync(fleetId, fleet.RegattaId.Value); } }
public async Task <FleetWithOptionsViewModel> GetBlankFleetWithOptionsAsync( string clubInitials, Guid?regattaId) { var clubId = await _coreClubService.GetClubId(clubInitials); var vm = new FleetWithOptionsViewModel(); vm.ClubId = clubId; vm.BoatClassOptions = await _coreClubService.GetAllBoatClasses(clubId); vm.CompetitorBoatClassOptions = vm.BoatClassOptions.OrderBy(c => c.Name); vm.CompetitorOptions = await _coreCompetitorService.GetCompetitorsAsync(clubId, null, true); vm.RegattaId = regattaId; vm.IsActive = true; if (regattaId.HasValue) { var regatta = await _regattaService.GetRegattaAsync(regattaId.Value); vm.Regatta = _mapper.Map <RegattaSummaryViewModel>(regatta); } return(vm); }
public async Task <ActionResult> Create( string clubInitials, FleetWithOptionsViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; try { var clubId = await _clubService.GetClubId(clubInitials); if (!await _authService.CanUserEdit(User, clubId)) { return(Unauthorized()); } model.ClubId = clubId; if (!ModelState.IsValid) { var vmOptions = await _fleetService.GetBlankFleetWithOptionsAsync( clubInitials, model.RegattaId); model.BoatClassOptions = vmOptions.BoatClassOptions; model.CompetitorOptions = vmOptions.CompetitorOptions; model.CompetitorBoatClassOptions = vmOptions.CompetitorBoatClassOptions; return(View(model)); } await _fleetService.SaveNew(model); if (!string.IsNullOrWhiteSpace(returnUrl)) { return(Redirect(returnUrl)); } return(RedirectToAction("Index", "Admin")); } catch { var vm = await _fleetService.GetBlankFleetWithOptionsAsync( clubInitials, model.RegattaId); model.BoatClassOptions = vm.BoatClassOptions; return(View(model)); } }
public async Task Update(FleetWithOptionsViewModel fleet) { var coreModel = _mapper.Map <Fleet>(fleet); if (fleet.FleetType == Api.Enumerations.FleetType.SelectedClasses && fleet.BoatClassIds != null) { coreModel.BoatClasses = (await _coreClubService.GetAllBoatClasses(fleet.ClubId)) .Where(c => fleet.BoatClassIds.Contains(c.Id)) .ToList(); } else if (fleet.FleetType == Api.Enumerations.FleetType.SelectedBoats && fleet.CompetitorIds != null) { coreModel.Competitors = (await _coreCompetitorService.GetCompetitorsAsync(fleet.ClubId, null, false)) .Where(c => fleet.CompetitorIds.Contains(c.Id)) .ToList(); } await _coreFleetService.Update(coreModel); }