public async Task <ActionResult> Edit( string clubInitials, BoatClass model) { try { var clubId = await _clubService.GetClubId(clubInitials); var boatClass = await _classService.GetClass(model.Id); if (!await _authService.CanUserEdit(User, clubId) || boatClass.ClubId != clubId) { return(Unauthorized()); } if (!ModelState.IsValid) { return(View(model)); } await _classService.Update(model); return(RedirectToAction("Index", "Admin")); } catch { return(View()); } }
public BoatClassServiceTests() { var options = new DbContextOptionsBuilder <SailScoresContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; _context = new SailScoresContext(options); var config = new MapperConfiguration(opts => { opts.AddProfile(new DbToModelMappingProfile()); }); _mapper = config.CreateMapper(); _fakeBoatClass = new BoatClass { Id = Guid.NewGuid(), Name = "Fake BoatClass" }; _context.BoatClasses.Add(_mapper.Map <Database.Entities.BoatClass>(_fakeBoatClass)); _context.SaveChanges(); _service = new SailScores.Core.Services.BoatClassService( _context, _mapper ); }
public async Task <ActionResult> Create( string clubInitials, BoatClass model) { try { var clubId = await _clubService.GetClubId(clubInitials); if (!await _authService.CanUserEdit(User, clubId)) { return(Forbid()); } model.ClubId = clubId; if (!ModelState.IsValid) { return(View(model)); } await _classService.SaveNew(model); return(RedirectToAction("Index", "Admin")); } catch { ModelState.AddModelError(String.Empty, "An error occurred saving these changes."); return(View(model)); } }
public async Task SaveNew(BoatClass boatClass) { var dbBoatClass = _mapper.Map <Db.BoatClass>(boatClass); dbBoatClass.Id = Guid.NewGuid(); _dbContext.BoatClasses.Add(dbBoatClass); var defaultShortName = new string( boatClass.Name .Where(c => !Char.IsWhiteSpace(c)) .ToArray()); var fleetId = Guid.NewGuid(); var fleetName = boatClass.Name.EndsWith('s') ? boatClass.Name : $"{boatClass.Name}s"; var dbFleet = new Db.Fleet { Id = fleetId, ClubId = boatClass.ClubId, FleetType = Api.Enumerations.FleetType.SelectedClasses, IsHidden = false, ShortName = defaultShortName, Name = fleetName, FleetBoatClasses = new List <Db.FleetBoatClass> { new Db.FleetBoatClass { BoatClassId = dbBoatClass.Id, FleetId = Guid.NewGuid() } } }; _dbContext.Fleets.Add(dbFleet); await _dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task Create_PostUnauthorized_ReturnForbidden() { var vm = new BoatClass { }; var result = await _controller.Create(_clubInitials, vm); _classServiceMock.Verify(s => s.SaveNew(vm), Times.Never); Assert.IsType <ForbidResult>(result); }
public async Task Update(BoatClass boatClass) { var existingClass = await _dbContext.BoatClasses .SingleAsync(c => c.Id == boatClass.Id) .ConfigureAwait(false); existingClass.Name = boatClass.Name; existingClass.Description = boatClass.Description; await _dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task Create_Post_CallsServiceSaveNew() { SetupAsAuthorized(); var vm = new BoatClass { }; await _controller.Create(_clubInitials, vm); _classServiceMock.Verify(s => s.SaveNew(vm), Times.Once); }
public async Task SaveNew_AddsToDb() { var changedClass = new BoatClass { Id = Guid.NewGuid(), Name = "New" }; await _service.SaveNew(changedClass); Assert.Equal(2, _context.BoatClasses.Count()); }
public async Task Update_CallsDb() { var changedClass = new BoatClass { Id = _fakeBoatClass.Id, Name = "New" }; await _service.Update(changedClass); Assert.Equal("New", _context.BoatClasses.First().Name); }
public async Task Create_Post_Redirects() { SetupAsAuthorized(); var vm = new BoatClass { }; var result = await _controller.Create(_clubInitials, vm); //Assert var redirect = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Admin", redirect.ControllerName); }
public async Task Create_PostInvalidModel_ReturnsModel() { SetupAsAuthorized(); var vm = new BoatClass { }; _controller.ModelState.AddModelError("Name", "The Name field is required."); // Act var result = await _controller.Create(_clubInitials, vm); // Assert _classServiceMock.Verify(s => s.SaveNew(vm), Times.Never); var viewresult = Assert.IsType <ViewResult>(result); Assert.NotNull(viewresult.Model); Assert.Equal(vm, viewresult.Model); }
public async Task CreatePost_ServiceThrows_ReturnsModel() { // Arrange SetupAsAuthorized(); var vm = new BoatClass { }; _classServiceMock.Setup(s => s.SaveNew(It.IsAny <BoatClass>())).Throws(new InvalidOperationException()); // Act var result = await _controller.Create(_clubInitials, vm); // Assert var viewresult = Assert.IsType <ViewResult>(result); Assert.NotNull(viewresult.Model); Assert.Equal(vm, viewresult.Model); }
public static ISailScoresContext GetContext() { var options = new DbContextOptionsBuilder <SailScoresContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new SailScoresContext(options); var club = new Club { Id = Guid.NewGuid(), Name = "Test Club", Initials = "TEST", Competitors = new List <Competitor>() }; context.Clubs.Add(club); var boatClass = new BoatClass { Id = Guid.NewGuid(), Name = "The Boat Class", ClubId = club.Id }; context.BoatClasses.Add(boatClass); var fleet = new Fleet { Id = Guid.NewGuid(), Name = "The Fleet", FleetType = Api.Enumerations.FleetType.AllBoatsInClub, ClubId = club.Id }; context.Fleets.Add(fleet); var fleet2 = new Fleet { Id = Guid.NewGuid(), Name = "A competitor fleet", FleetType = Api.Enumerations.FleetType.SelectedBoats, ClubId = club.Id }; context.Fleets.Add(fleet2); var season = new Season { Id = Guid.NewGuid(), Name = "Test Season", Start = new DateTime(2020, 1, 1), End = new DateTime(2021, 1, 1), UrlName = "TestSeason" }; var competitor = new Competitor { Id = Guid.NewGuid(), Name = "Comp1", BoatName = "Comp1Boat", ClubId = club.Id, BoatClass = boatClass, CompetitorFleets = new List <CompetitorFleet> { new CompetitorFleet { FleetId = fleet2.Id } } }; context.Competitors.Add(competitor); var inactiveCompetitor = new Competitor { Id = Guid.NewGuid(), Name = "Comp12", BoatName = "Comp12Boat", ClubId = club.Id, BoatClass = boatClass, CompetitorFleets = new List <CompetitorFleet> { new CompetitorFleet { FleetId = fleet2.Id } }, IsActive = false }; context.Competitors.Add(inactiveCompetitor); var scoringSystem = new ScoringSystem { Id = Guid.NewGuid(), Name = "Default for fake club", ClubId = club.Id, ScoreCodes = new List <ScoreCode> { new Database.Entities.ScoreCode { Name = "DNC", CameToStart = false } } }; context.ScoringSystems.Add(scoringSystem); club.DefaultScoringSystemId = scoringSystem.Id; var defaultScoringSystem = new ScoringSystem { Id = Guid.NewGuid(), Name = "Appendix A Low Point For Series", ClubId = null, IsSiteDefault = true, ScoreCodes = new List <ScoreCode> { new Database.Entities.ScoreCode { Name = "DNC", CameToStart = false } } }; context.ScoringSystems.Add(defaultScoringSystem); var series = new Series { Name = "Series One", UrlName = "SeriesOne", ClubId = club.Id, Season = season }; context.Series.Add(series); context.Races.Add(new Race { Id = Guid.NewGuid(), Date = DateTime.Now, ClubId = club.Id, Scores = new List <Score> { new Score { CompetitorId = competitor.Id, Place = 1 } }, SeriesRaces = new List <SeriesRace> { new SeriesRace { Series = series } } }); var regattaSeries = new Series { Id = Guid.NewGuid(), Name = "Regatta Series", UrlName = "RegattaurlName" }; var regattaFleet = new Fleet { FleetType = Api.Enumerations.FleetType.AllBoatsInClub }; var regatta = new Regatta { ClubId = club.Id, Season = season, Name = "Test Regatta", RegattaFleet = new List <RegattaFleet> { new RegattaFleet { Fleet = regattaFleet } }, StartDate = season.Start.AddMonths(6), EndDate = season.Start.AddMonths(6).AddDays(3), RegattaSeries = new List <RegattaSeries> { new RegattaSeries { Series = regattaSeries } } }; context.Regattas.Add(regatta); context.SaveChanges(); return(context); }
public async Task GetByCrewAndPosition_WithPositionOverCrewSize_ShouldReturn400(BoatClass boatClass, int position) { using (var context = provider.GetService <TimingSiteContext>()) using (var controller = new HeadRaceTimingSite.Api.Controllers.AthleteController(mapper, context)) { context.Crews.Add(new Crew { BroeCrewId = 1, BoatClass = boatClass }); context.SaveChanges(); var result = await controller.GetByCrewAndPosition(1, position).ConfigureAwait(false); var badRequestResult = result as BadRequestResult; Assert.IsNotNull(badRequestResult, "Boatclass: {0}; Position: {1}", boatClass, position); Assert.AreEqual(400, badRequestResult.StatusCode); } }
public Task Update(BoatClass boatClass) { return(_coreBoatClassService.Update(boatClass)); }
public Task SaveNew(BoatClass boatClass) { return(_coreBoatClassService.SaveNew(boatClass)); }