public CrewDTO AddCrew(CrewDTO crew) { Validation(crew); Crew modelCrew = mapper.Map <CrewDTO, Crew>(crew); return(mapper.Map <Crew, CrewDTO>(unitOfWork.Crews.Create(modelCrew))); }
public async void SaveButton_Click(object sender, RoutedEventArgs e) { bool isNumber = int.TryParse(pilotId.Text, out int n); bool isNumberStew = int.TryParse(stewardessId.Text, out int s); if (isNumber && isNumberStew) { CrewDTO crewDto = new CrewDTO { PilotId = n, StewardessId = s }; if (ViewModel.IsValid(crewDto)) { if ((string)Save.Content == "Create") { await ViewModel.Create(crewDto); WrongInput.Visibility = Visibility.Collapsed; Form.Visibility = Visibility.Collapsed; return; } await ViewModel.UpdateSelected(crewDto); WrongInput.Visibility = Visibility.Collapsed; return; } } WrongInput.Visibility = Visibility.Visible; }
[Test] // behaviour test public void Create_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work() { // Arrange var crewDTOToCreate = new CrewDTO() { PilotId = 2 }; var crewRepositoryFake = A.Fake <ICrewRepository>(); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Crew>()).Returns(crewRepositoryFake); var crewService = new CrewService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => crewService.Create(crewDTOToCreate)); // Assert. Just behaviour A.CallTo(() => crewRepositoryFake.Create(A <Crew> ._)).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.CrewRepository).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.Set <Crew>()).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened(); }
public async Task Create(CrewDTO crewDto) { Pilot pilot; Stewardess stewardess; try { pilot = await _pilotService.Get(crewDto.PilotId); stewardess = await _stewardessService.Get(crewDto.StewardessId); } catch (System.Exception) { return; } pilot.Id = 0; stewardess.Id = 0; Crew crew = new Crew { Pilot = pilot, Stewardesses = new List <Stewardess> { stewardess } }; await _service.Create(crew); await UpdateList(); }
public void Post([FromBody] CrewDTO value) { if (ModelState.IsValid) { Service.Create(value); } }
public async Task <bool> CreateObjectAsync(CrewDTO obj) { var stewardesses = (await db.Stewardess.GetAllAsync()) .Where(i => obj.StewardessesId?.Contains(i.Id) == true).ToList(); var pilot = await db.Pilots.GetAsync(obj.PilotId.Value); if (stewardesses.Count == 0 || pilot == null) { return(false); } var crew = _mapper.Map <Crew>(obj); crew.Pilot = pilot; crew.Stewardesses = stewardesses; await db.Crews.CreateAsync(crew); try { await db.SaveAsync(); } catch (Exception) { return(false); } return(true); }
public async Task WriteToDB() { var result = await GetCrew(); if (result == null) { throw new System.Exception("Bad request"); } foreach (var item in result) { Aviator currentAviator = null; List <Stewardess> currentStewardess = new List <Stewardess>(); foreach (var pilot in item.pilot) { currentAviator = new Aviator { Name = pilot.Name, Surname = pilot.Surname, Experience = pilot.Experience, DateOfBirthday = pilot.DateOfBirthday }; } foreach (var stewardess in item.stewardess) { currentStewardess.Add(new Stewardess { Name = stewardess.Name, Surname = stewardess.Surname, DateOfBirthday = stewardess.DateOfBirthday }); } CrewDTO crew = new CrewDTO { aviator = currentAviator, stewardesses = currentStewardess }; await service.Create(crew); } }
public void CreateCrew(CrewDTO item) { Crew temp = new Crew(); temp.PilotId = IunitOfWork.PilotRepository.Get(item.PilotId); List <Stewardess> tempS = IunitOfWork.StewardessRepository.GetAll(); List <Stewardess> selected = new List <Stewardess>(); foreach (Stewardess s in tempS) { foreach (int i in item.StewardessIds) { if (s.Id == i) { selected.Add(s); } } } temp.StewardessIds = selected; if (temp.StewardessIds.Count == item.StewardessIds.Count() && temp.PilotId != null) { IunitOfWork.CrewRepository.Create(temp); } }
public void UpdateEntity_Should_Update_crew_typeof_Crew() { // Arrange CrewDTO crewDTO = new CrewDTO { Id = 1, PilotId = 1 }; Crew crew = new Crew { Id = 1, PilotId = 1 }; var crewRepository = A.Fake <IRepository <Crew> >(); A.CallTo(() => crewRepository.Get(A <int> ._)).Returns(new Crew { Id = 1 }); var crewService = new CrewService(crewRepository); //Act crewService.UpdateEntity(1, crewDTO); var result = crewRepository.Get(1); // Assert Assert.AreEqual(crew, result); }
public void CreateCrew(CrewDTO item) { //Crew temp = new Crew(); //temp.PilotId = IunitOfWork.PilotRepository.Get(item.PilotId); //List<Stewardess> tempS = IunitOfWork.StewardessRepository.GetAll(); //List<Stewardess> selected = new List<Stewardess>(); //foreach (Stewardess s in tempS) //{ // foreach (int i in item.StewardessIds) // { // if (s.Id == i) // selected.Add(s); //} //} //temp.StewardessIds = selected; //if (temp.StewardessIds.Count == item.StewardessIds.Count() && temp.PilotId != null) //{ // IunitOfWork.CrewRepository.Create(temp); //} IunitOfWork.CrewRepository.Create(Mapper.Map <CrewDTO, Crew>(item)); }
public void Create_When_entity_is_invalid_Then_bad_request_exception_is_thrown() { // Arrange var crewMock = new Crew() { Id = 2, PilotId = 2 }; var crewDTOToCreate = new CrewDTO() { PilotId = 2 }; var crewRepositoryFake = A.Fake <ICrewRepository>(); A.CallTo(() => crewRepositoryFake.Create(A <Crew> ._)).Returns(crewMock); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Crew>()).Returns(crewRepositoryFake); var crewService = new CrewService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => crewService.Create(crewDTOToCreate), ""); Assert.AreEqual(exception.Message, "Is Invalid"); }
private void Create_Click(object sender, RoutedEventArgs e) { var turple = RenderCreate(); Button btnCreate = turple.Item1; ComboBox Pilot = turple.Item2; btnCreate.Click += async(object sen, RoutedEventArgs evArgs) => { var crew = new CrewDTO() { PilotId = ((PilotDTO)Pilot.SelectedItem).Id, StewardressIds = selectedStews }; try { await service.CreateAsync(crew); } catch (Exception) { } crewsList.Add(crew); UpdateList(); SingleItem.Children.Clear(); }; }
private void CustomMapper(List <ExternalCrewDTO> external, out List <CrewDTO> crew) { crew = new List <CrewDTO>(); foreach (var item in external) { var stews = new List <StewardessDTO>(); foreach (var externalStew in item.stewardess) { var stewDto = new StewardessDTO { DateOfBirth = externalStew.birthDate, CrewId = externalStew.crewId, FirstName = externalStew.firstName, LastName = externalStew.lastName }; stews.Add(stewDto); } var crewDto = new CrewDTO { Id = item.id, Pilot = new PilotDTO { DateOfBirth = item.pilot[0].birthDate, Experience = item.pilot[0].exp, FirstName = item.pilot[0].firstName, LastName = item.pilot[0].lastName }, Stewardesses = new List <StewardessDTO>(stews) }; crew.Add(crewDto); } }
public void Put(int id, [FromBody] CrewDTO value) { if (ModelState.IsValid) { Service.Update(id, value); } }
public async Task UpdateSelected(CrewDTO crewDto) { var crew = await _service.Get(SelectedItem.Id); Pilot pilot; Stewardess stewardess; try { pilot = await _pilotService.Get(crewDto.PilotId); stewardess = await _stewardessService.Get(crewDto.StewardessId); } catch (System.Exception) { return; } pilot.Id = 0; stewardess.Id = 0; crew.Pilot = pilot; crew.Stewardesses = new List <Stewardess> { stewardess }; await _service.Update(SelectedItem.Id, crew); SelectedItem = null; await UpdateList(); }
public async Task <IActionResult> Update(int id, [FromBody] CrewDTO 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 void Put(int id, [FromBody] CrewDTO сrew) { if (ModelState.IsValid) { service.Update(id, сrew); } }
public void Post([FromBody] CrewDTO сrew) { if (ModelState.IsValid) { service.Create(сrew); } }
public void UpdateCrew_WhenInvalid_ThrowsArgumentNullException() { var crew = new CrewDTO { Stewardesses = new List <StewardessDTO> { new StewardessDTO { FirstName = "Maria", LastName = "Petrova", DateOfBirth = new DateTime(1970, 05, 03) }, new StewardessDTO { FirstName = "Anna", LastName = "Ivanova", DateOfBirth = new DateTime(1990, 11, 09) } } }; var service = new CrewService(fakeUnitOfWork, mapper); Assert.ThrowsAsync <ArgumentNullException>( () => service.Update(crew)); }
public void TestSetup() { crew = new CrewDTO() { IdPilot = 1, idStewardess = new int[] { 1, 2 } }; }
public async Task Update(int id, CrewDTO modelDTO) { var source = uow.Crews.Get(id); var dest = mapper.Map <CrewDTO, Crew>(modelDTO); await uow.Crews.Update(dest); await uow.SaveAsync(); }
private string StewardessesId(CrewDTO crew) { var result = "count : " + crew.StewardessesId.Count + " ("; crew.StewardessesId.ToList().ForEach(x => result += " Id:" + x.ToString()); result += " )"; return(result); }
public async Task Update(CrewDTO entity) { if (entity.Pilot == null || entity.Stewardesses == null) { throw new ArgumentNullException(nameof(entity)); } await unitOfWork.CrewRepository.Update(mapper.Map <CrewDTO, Crew>(entity)); }
public bool IsValid(CrewDTO crew) { if (crew.PilotId <= 0 || crew.StewardessId <= 0) { return(false); } return(true); }
private void Validation(CrewDTO crew) { var validationResult = validator.Validate(crew); if (!validationResult.IsValid) { throw new Exception(validationResult.Errors.First().ToString()); } }
public async Task <IActionResult> Put(int id, [FromBody] CrewDTO crew) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _service.UpdateObjectAsync(id, crew); return(result == true?StatusCode(200) : StatusCode(500)); }
public async Task <CrewDTO> AddCrew(CrewDTO crew) { Validation(crew); Crew modelCrew = mapper.Map <CrewDTO, Crew>(crew); Crew result = await unitOfWork.Crews.Create(modelCrew); await unitOfWork.SaveChangesAsync(); return(mapper.Map <Crew, CrewDTO>(result)); }
public async Task DeleteCrew() { //act await crewService.DeleteCrew(crew.Id); CrewDTO result = await crewService.GetCrew(crew.Id); //assert Assert.IsNull(result); }
public IActionResult Put(int id, [FromBody] CrewDTO crew) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _service.Update(id, crew); return(result == true?StatusCode(200) : StatusCode(500)); }
public IActionResult Post([FromBody] CrewDTO crew) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _service.AddObject(crew); return(result == true?StatusCode(200) : StatusCode(404)); }