private void Create_Click(object sender, RoutedEventArgs e) { var turple = RenderCreate(); var btnCreate = turple.Item1; var firstname = turple.Item2; var lastname = turple.Item3; var expirience = turple.Item4; btnCreate.Click += async(object sen, RoutedEventArgs evArgs) => { var pilot = new PilotDTO() { FirstName = firstname.Text, LastName = lastname.Text, Experience = int.Parse(expirience.Text) }; try { await service.CreateAsync(pilot); } catch (Exception) { } pilotsList.Add(pilot); UpdateList(); SingleItem.Children.Clear(); }; }
public async Task <PilotDTO> CreatePilot(PilotDTO pilot) { if (pilot != null) { if (await unit.PilotsRepo.GetEntityById(pilot.Id) != null) { throw new ArgumentOutOfRangeException("Such user exsists!"); } Pilot newPilot = mapper.Map <PilotDTO, Pilot>(pilot); if (newPilot == null || newPilot.Name == null || newPilot.Surname == null) { throw new AutoMapperMappingException("Couldn't map PilotDTO into Pilot"); } var result = await unit.PilotsRepo.Insert(newPilot); await unit.SaveChangesAsync(); return(mapper.Map <Pilot, PilotDTO>(result) ?? throw new AutoMapperMappingException("Error: Can't map the pilot into pilotDTO")); } else { throw new ArgumentNullException(); } }
public bool Update(int id, PilotDTO obj) { var result = db.Pilots.UpdateObject(id, _mapper.Map <Pilot>(obj)); db.Save(); return(result); }
public async Task <IActionResult> Update(int id, [FromBody] PilotDTO item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { item.Id = id; await service.GetById(id); await service.Update(item); await service.SaveChanges(); return(Ok(item)); } catch (NotFoundException e) { return(NotFound(e.Message)); } catch (Exception) { return(BadRequest()); } }
public PilotDTO AddPilot(PilotDTO pilot) { Validation(pilot); Pilot modelPilot = mapper.Map <PilotDTO, Pilot>(pilot); return(mapper.Map <Pilot, PilotDTO>(unitOfWork.Pilots.Create(modelPilot))); }
public void AddDeleteNewPilotTest_Returns_CreatedResult_And_Pilot_ShoudBe_AddedTo_Database_And_Then_ShouldBe_Deleted() { // Arrange MSSQLContext context = new MSSQLContext(); CrewsRepository crewsRepository = new CrewsRepository(); PilotsRepository pilotsRepository = new PilotsRepository(); StewardessesRepository stewardessesRepository = new StewardessesRepository(); CrewingUnitOfWork uow = new CrewingUnitOfWork(crewsRepository, pilotsRepository, stewardessesRepository, context); CrewingService service = new CrewingService(uow); PilotsController controller = new PilotsController(mapper.GetDefaultMapper(), service); // add act var newPilotDTO = new PilotDTO() { Birth = new DateTime(1985, 5, 12, 0, 0, 0), ExperienceYears = 15, Name = "Grisha", Surname = "Kramer" }; var addResult = controller.AddPilot(newPilotDTO); // add assert Assert.IsInstanceOf <CreatedResult>(addResult); Assert.IsInstanceOf <PilotDTO>((addResult as CreatedResult).Value); // delete act var addedPilotDTO = (addResult as CreatedResult).Value as PilotDTO; var deleteResult = controller.DeletePilot(addedPilotDTO.Id); // delete assert Assert.IsInstanceOf <OkResult>(deleteResult); Assert.IsInstanceOf <NotFoundObjectResult>(controller.GetPilot(addedPilotDTO.Id)); }
public void UpdateEntity(int id, PilotDTO pilotDTO) { var pilot = pilotRepository.Get(id); if (pilot == null) { throw new ValidationException($"Pilot with this id {pilotDTO.Id} not found"); } if (pilotDTO.FirstName != null) { pilot.FirstName = pilotDTO.FirstName; } if (pilotDTO.LastName != null) { pilot.LastName = pilotDTO.LastName; } if (pilotDTO.BirthDate != DateTime.MinValue) { pilot.BirthDate = pilotDTO.BirthDate; } if (pilotDTO.Experience != null) { pilot.Experience = pilotDTO.Experience.Value; } pilotRepository.Update(pilot); }
public async Task <bool> UpdateObjectAsync(int id, PilotDTO obj) { var result = db.Pilots.Update(id, _mapper.Map <Pilot>(obj)); await db.SaveAsync(); return(result); }
public void Post([FromBody] PilotDTO value) { if (ModelState.IsValid) { Service.Create(value); } }
[Test] // behaviour test public void Create_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work() { // Arrange var pilotDTOToCreate = new PilotDTO() { FirstName = "Pilot2", LastName = "Pilot2", BirthDate = new DateTime(1970, 10, 1), Experience = 15 }; var pilotRepositoryFake = A.Fake <IPilotRepository>(); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake); var pilotService = new PilotService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => pilotService.Create(pilotDTOToCreate)); // Assert. Just behaviour A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.PilotRepository).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.Set <Pilot>()).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened(); }
[Test] // behaviour test public void Create_When_entity_is_created_Then_it_makes_calls_to_repository_and_unit_of_work() { // Arrange var pilotDTOToCreate = new PilotDTO() { FirstName = "Pilot1", LastName = "Pilot1", BirthDate = new DateTime(1970, 10, 1), Experience = 15 }; var pilotRepositoryFake = A.Fake <IPilotRepository>(); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake); var pilotService = new PilotService(unitOfWorkFake, AlwaysValidValidator); // Act var result = pilotService.Create(pilotDTOToCreate); // Assert. Just behaviour A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).MustHaveHappenedOnceExactly(); A.CallTo(() => unitOfWorkFake.Set <Pilot>()).MustHaveHappenedOnceExactly(); A.CallTo(() => unitOfWorkFake.SaveChanges()).MustHaveHappenedOnceExactly(); }
public void UpdateEntity_Should_Update_pilot_typeof_Pilot() { // Arrange PilotDTO pilotDTO = new PilotDTO { Id = 1, FirstName = "Bob", LastName = "Henk", Experience = 10, BirthDate = new DateTime(1998, 07, 12) }; Pilot pilot = new Pilot { Id = 1, FirstName = "Bob", LastName = "Henk", Experience = 10, BirthDate = new DateTime(1998, 07, 12) }; var pilotRepository = A.Fake <IRepository <Pilot> >(); A.CallTo(() => pilotRepository.Get(A <int> ._)).Returns(new Pilot { Id = 1 }); var pilotService = new PilotService(pilotRepository); //Act pilotService.UpdateEntity(1, pilotDTO); var result = pilotRepository.Get(1); // Assert Assert.AreEqual(pilot, result); }
public void Create_When_entity_is_invalid_Then_bad_request_exception_is_thrown() { // Arrange var pilotMock = new Pilot() { Id = 2, FirstName = "Pilot2", LastName = "Pilot2", BirthDate = new DateTime(1970, 10, 1), Experience = 15 }; var pilotDTOToCreate = new PilotDTO() { FirstName = "Pilot2", LastName = "Pilot2", BirthDate = new DateTime(1970, 10, 1), Experience = 15 }; var pilotRepositoryFake = A.Fake <IPilotRepository>(); A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).Returns(pilotMock); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake); var pilotService = new PilotService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => pilotService.Create(pilotDTOToCreate), ""); Assert.AreEqual(exception.Message, "Is Invalid"); }
public void CreateEntity_Should_Create_pilot_typeof_Pilot() { // Arrange PilotDTO pilotDTO = new PilotDTO { Id = 1, FirstName = "Bob", LastName = "Henk", Experience = 10, BirthDate = new DateTime(1998, 07, 12) }; Pilot pilot = new Pilot { Id = 1, FirstName = "Bob", LastName = "Henk", Experience = 10, BirthDate = new DateTime(1998, 07, 12) }; var pilotRepository = new FakeRepository <Pilot>(); var pilotService = new PilotService(pilotRepository); // Act pilotService.CreateEntity(pilotDTO); var result = pilotRepository.Get(1); // Assert Assert.AreEqual(pilot, result); }
public void UpdateEntity_Should_Update_pilot_in_db() { // Arrange var pilot = dispatcherContext.Pilots .FirstOrDefault(c => c.FirstName == "TestName1" && c.LastName == "TestName1"); PilotDTO pilotDTO = new PilotDTO { FirstName = "TestName1", LastName = "TestName1", BirthDate = DateTime.Parse("12.06.1994"), Experience = 20 }; pilot = new Pilot { Id = pilot.Id, FirstName = "TestName1", LastName = "TestName1", BirthDate = DateTime.Parse("12.06.1994"), Experience = 20 }; // Act _pilotService.UpdateEntity(pilot.Id, pilotDTO); var pilotResult = _pilotRepository.Get(pilot.Id); // Assert Assert.AreEqual(pilot, pilotResult); }
public async Task UpdateEntityAsync(int id, PilotDTO pilotDTO) { var pilot = await pilotRepository.GetAsync(id); if (pilot == null) { throw new ValidationException($"Pilot with this id {id} not found"); } if (pilotDTO.CrewId > 0) { pilot.CrewId = pilotDTO.CrewId; } if (pilotDTO.FirstName != null) { pilot.FirstName = pilotDTO.FirstName; } if (pilotDTO.LastName != null) { pilot.LastName = pilotDTO.LastName; } if (pilotDTO.BirthDate != DateTime.MinValue) { pilot.BirthDate = pilotDTO.BirthDate; } if (pilotDTO.Experience != null) { pilot.Experience = pilotDTO.Experience.Value; } await pilotRepository.UpdateAsync(pilot).ConfigureAwait(false); }
public async Task Update(int id, PilotDTO modelDTO) { var source = uow.Pilots.Get(id); var dest = mapper.Map <PilotDTO, Pilot>(modelDTO); await uow.Pilots.Update(dest); await uow.SaveAsync(); }
public void UpdatePilot(PilotDTO pilot) { if (pilot != null) { Pilot updtPilot = mapper.Map <PilotDTO, Pilot>(pilot); unitOfWork.PilotRepository.Insert(updtPilot); } }
public void CreatePilot(PilotDTO pilot) { if (pilot != null) { Pilot newPilot = mapper.Map <PilotDTO, Pilot>(pilot); unitOfWork.PilotRepository.Create(newPilot); } }
private void Validation(PilotDTO pilot) { var validationResult = validator.Validate(pilot); if (!validationResult.IsValid) { throw new Exception(validationResult.Errors.First().ToString()); } }
public bool AddObject(PilotDTO obj) { if (obj != null) { db.Pilots.Add(_mapper.Map <Pilot>(obj)); return(true); } return(false); }
public async Task <IActionResult> Get(int id) { PilotDTO ticket = await service.GetById(id); if (ticket == null) { return(NotFound()); } return(Ok(ticket)); }
public IActionResult Post([FromBody] PilotDTO Pilot) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _service.AddObject(Pilot); return(result == true?StatusCode(200) : StatusCode(404)); }
public void TestSetup() { pilot = new PilotDTO() { Name = "Yan", Surname = "Gorshkov", Birthday = new DateTime(1998, 8, 21), Experience = 2 }; }
public async Task DeletePilot() { //act await pilotService.DeletePilot(pilot.Id); PilotDTO result = await pilotService.GetPilot(pilot.Id); //assert Assert.IsNull(result); }
public IActionResult Put(int id, [FromBody] PilotDTO Pilot) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _service.Update(id, Pilot); return(result == true?StatusCode(200) : StatusCode(500)); }
public async Task <IActionResult> Put(int id, [FromBody] PilotDTO Pilot) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _service.UpdateObjectAsync(id, Pilot); return(result == true?StatusCode(200) : StatusCode(500)); }
public async Task <PilotDTO> AddPilot(PilotDTO pilot) { Validation(pilot); Pilot modelPilot = mapper.Map <PilotDTO, Pilot>(pilot); Pilot result = await unitOfWork.Pilots.Create(modelPilot); await unitOfWork.SaveChangesAsync(); return(mapper.Map <Pilot, PilotDTO>(result)); }
public void PostPilotTestGoodResult_when_post_correct_then_HttpOK() { PilotDTO flight = new PilotDTO() { Name = "Name", Experience = 2, Surname = "Surname" }; Assert.AreEqual(new HttpResponseMessage(HttpStatusCode.OK).StatusCode, _controller.Post(flight).StatusCode); }
public async Task AddPilot_When_correct_data_Then_check_exists() { //assing pilot.Id = pilotService.AddPilot(pilot).Result.Id; //act PilotDTO checkPilot = await pilotService.GetPilot(pilot.Id); //assert Assert.IsNotNull(checkPilot); }