public async Task UpdateEntityAsync(int id, DepartureDTO departureDTO) { var departure = await departureRepository.GetAsync(id); if (departure == null) { throw new ValidationException($"Departure with this id {id} not found"); } if (departureDTO.FlightNumber > 0) { departure.FlightNumber = departureDTO.FlightNumber; } if (departureDTO.CrewId > 0) { departure.CrewId = departureDTO.CrewId; } if (departureDTO.FlightId > 0) { departure.FlightId = departureDTO.FlightId; } if (departureDTO.PlaneId > 0) { departure.PlaneId = departureDTO.PlaneId; } if (departureDTO.Time != DateTime.MinValue) { departure.Time = departureDTO.Time; } await departureRepository.UpdateAsync(departure).ConfigureAwait(false); }
[Test] // behaviour test public void Create_When_entity_is_created_Then_it_makes_calls_to_repository_and_unit_of_work() { // Arrange var departureDTOToCreate = new DepartureDTO() { Date = new DateTime(2018, 10, 1), FlightId = 1, PlaneId = 1, CrewId = 1 }; var departureRepositoryFake = A.Fake <IDepartureRepository>(); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Departure>()).Returns(departureRepositoryFake); var departureService = new DepartureService(unitOfWorkFake, AlwaysValidValidator); // Act var result = departureService.Create(departureDTOToCreate); // Assert. Just behaviour A.CallTo(() => departureRepositoryFake.Create(A <Departure> ._)).MustHaveHappenedOnceExactly(); A.CallTo(() => unitOfWorkFake.Set <Departure>()).MustHaveHappenedOnceExactly(); A.CallTo(() => unitOfWorkFake.SaveChanges()).MustHaveHappenedOnceExactly(); }
public void Create_When_entity_is_invalid_Then_bad_request_exception_is_thrown() { // Arrange var departureMock = new Departure() { Id = 2, Date = new DateTime(2018, 10, 1), FlightId = 2, PlaneId = 2, CrewId = 2 }; var departureDTOToCreate = new DepartureDTO() { Date = new DateTime(2018, 10, 1), FlightId = 2, PlaneId = 2, CrewId = 2 }; var departureRepositoryFake = A.Fake <IDepartureRepository>(); A.CallTo(() => departureRepositoryFake.Create(A <Departure> ._)).Returns(departureMock); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Departure>()).Returns(departureRepositoryFake); var departureService = new DepartureService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => departureService.Create(departureDTOToCreate), ""); Assert.AreEqual(exception.Message, "Is Invalid"); }
public DepartureDTO AddDeparture(DepartureDTO departure) { Validation(departure); Departure modelDeparture = mapper.Map <DepartureDTO, Departure>(departure); return(mapper.Map <Departure, DepartureDTO>(unitOfWork.Departures.Create(modelDeparture))); }
public void CreateEntity_Should_Create_departure_typeof_Departure() { // Arrange DepartureDTO departureDTO = new DepartureDTO { Id = 1, CrewId = 1, FlightId = 1, FlightNumber = 1111, PlaneId = 1, Time = new DateTime(2018, 07, 12) }; Departure departure = new Departure { Id = 1, CrewId = 1, FlightId = 1, FlightNumber = 1111, PlaneId = 1, Time = new DateTime(2018, 07, 12) }; var departureRepository = new FakeRepository <Departure>(); var departureService = new DepartureService(departureRepository); // Act departureService.CreateEntity(departureDTO); var result = departureRepository.Get(1); // Assert Assert.AreEqual(departure, result); }
public void UpdateEntity(int id, DepartureDTO departureDTO) { var departure = departureRepository.Get(departureDTO.Id); if (departure == null) { throw new ValidationException($"Departure with this id {departureDTO.Id} not found"); } if (departureDTO.FlightNumber > 0) { departure.FlightNumber = departureDTO.FlightNumber; } if (departureDTO.CrewId > 0) { departure.CrewId = departureDTO.CrewId; } if (departureDTO.PlaneId > 0) { departure.PlaneId = departureDTO.PlaneId; } if (departureDTO.Time != DateTime.MinValue) { departure.Time = departureDTO.Time; } departureRepository.Update(); }
public void Post([FromBody] DepartureDTO value) { if (ModelState.IsValid) { Service.Create(value); } }
private void Create_Click(object sender, RoutedEventArgs e) { var turple = RenderCreate(); var btnCreate = turple.Item1; var number = turple.Item2; var time = turple.Item3; var planes = turple.Item4; var crews = turple.Item5; btnCreate.Click += async(object sen, RoutedEventArgs evArgs) => { var plane = new DepartureDTO() { FlightNumber = number.Text, DepartureTime = (new DateTime(2018, 2, 2) + time.Time).ToString(), PlaneId = ((PlaneDTO)planes.SelectedItem).Id, CrewId = ((CrewDTO)crews.SelectedItem).Id }; try { await service.CreateAsync(plane); } catch (Exception) { } departuresList.Add(plane); UpdateList(); SingleItem.Children.Clear(); }; }
[Test] // behaviour test public void Create_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work() { // Arrange var departureDTOToCreate = new DepartureDTO() { Date = new DateTime(2018, 10, 1), FlightId = 2, PlaneId = 2, CrewId = 2 }; var departureRepositoryFake = A.Fake <IDepartureRepository>(); var unitOfWorkFake = A.Fake <IUnitOfWork>(); A.CallTo(() => unitOfWorkFake.Set <Departure>()).Returns(departureRepositoryFake); var departureService = new DepartureService(unitOfWorkFake, AlwaysInValidValidator); // Act + Assert var exception = Assert.Throws <BadRequestException>(() => departureService.Create(departureDTOToCreate)); // Assert. Just behaviour A.CallTo(() => departureRepositoryFake.Create(A <Departure> ._)).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.DepartureRepository).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.Set <Departure>()).MustNotHaveHappened(); A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened(); }
public async Task <IActionResult> Update(int id, [FromBody] DepartureDTO 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()); } }
private bool ValidateBetweenDaysDepartureRun(IEnumerable <DepartureDTO> departureRun) { DepartureDTO previousDeparture = null; bool haveAlreadyBeenOneDepartureBetweenDays = false; foreach (var departure in departureRun) { if (previousDeparture != null) { if (previousDeparture.DepartureTime == departure.DepartureTime) { throw new ArgumentException(ValidationResources.DeparturesTimesCantBeEqual); } if (previousDeparture.DepartureTime > departure.DepartureTime) { if (haveAlreadyBeenOneDepartureBetweenDays == true) { throw new ArgumentException(ValidationResources.DeparturesTimesNotCorrectOnBetweenDays); } else { haveAlreadyBeenOneDepartureBetweenDays = true; } } } previousDeparture = departure; } if (haveAlreadyBeenOneDepartureBetweenDays == false) { throw new ArgumentException(ValidationResources.BetweenDaysNotShouldBeChecked); } return(true); }
public void Put(int id, [FromBody] DepartureDTO departure) { if (ModelState.IsValid) { service.Update(id, departure); } }
public void Post([FromBody] DepartureDTO departure) { if (ModelState.IsValid) { service.Create(departure); } }
private IEnumerable <DepartureDTO> CorrectRun(IEnumerable <DepartureDTO> departureRun) { if (ShoulRunBeCorrected(departureRun)) { DepartureDTO lastDeparture = null; departureRun.Last().IsNextDay = true; bool shouldRestBeOnNextDay = false; foreach (var departure in departureRun) { if (lastDeparture != null) { if (!shouldRestBeOnNextDay) { if (lastDeparture.DepartureTime < departure.DepartureTime) { lastDeparture.IsNextDay = false; } else { lastDeparture.IsNextDay = false; lastDeparture.IsBetweenDays = true; shouldRestBeOnNextDay = true; } } else { lastDeparture.IsNextDay = true; } } lastDeparture = departure; } } return(departureRun); }
public async Task Update(int id, DepartureDTO modelDTO) { var source = uow.Departures.Get(id); var dest = mapper.Map <DepartureDTO, Departure>(modelDTO); await uow.Departures.Update(dest); await uow.SaveAsync(); }
public void UpdateDeparture(DepartureDTO departure) { if (departure != null) { Departure updatedDepart = mapper.Map <DepartureDTO, Departure>(departure); unitOfWork.DepartureRepository.Insert(updatedDepart); } }
private void Validation(DepartureDTO departure) { var validationResult = validator.Validate(departure); if (!validationResult.IsValid) { throw new Exception(validationResult.Errors.First().ToString()); } }
public async Task <IActionResult> Get(int id) { DepartureDTO ticket = await service.GetById(id); if (ticket == null) { return(NotFound()); } return(Ok(ticket)); }
private Departure ConvertToModel(DepartureDTO departureDTO) { if (departureDTO != null) { return(new Departure { DepartureId = departureDTO.DepartureId, PlaneNumber = departureDTO.PlaneNumber, Time = departureDTO.Time }); } return(null); }
public async Task <DepartureDTO> AddDeparture(DepartureDTO departure) { Validation(departure); Departure modelDeparture = mapper.Map <DepartureDTO, Departure>(departure); Departure result = await unitOfWork.Departures.Create(modelDeparture); await unitOfWork.SaveChangesAsync(); return(mapper.Map <Departure, DepartureDTO>(result)); }
public IActionResult ModifyDeparture(long id, [FromBody] DepartureDTO departure) { if (!ModelState.IsValid) { return(BadRequest() as IActionResult); } var entity = service.ModifyDeparture(id, mapper.Map <Departure>(departure)); return(entity == null?StatusCode(304) as IActionResult : Ok(mapper.Map <DepartureDTO>(entity))); }
public void UpdateDeparture(DepartureDTO departure) { if (departure != null) { Departure updatedDepart = mapper.Map <DepartureDTO, Departure>(departure); unit.DeparturesRepo.Update(updatedDepart); } else { throw new ArgumentNullException(); } }
public HttpResponseMessage Put(int id, [FromBody] DepartureDTO departure) { if (ModelState.IsValid && departure != null) { _service.Update <Departure>(id, Mapper.Map <DepartureDTO, Departure>(departure)); return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public JsonResult Put(int id, [FromBody] DepartureDTO departure) { try { return(Json(departureService.UpdateDeparture(id, departure))); } catch (System.Exception ex) { HttpContext.Response.StatusCode = 404; return(Json(ex.Message)); } }
public JsonResult Post([FromBody] DepartureDTO departure) { try { return(Json(departureService.AddDeparture(departure))); } catch (System.Exception ex) { HttpContext.Response.StatusCode = 400; return(Json(ex.Message)); } }
public IActionResult Post([FromBody] DepartureDTO value) { var validationResult = _depatureModelValidator.Validate(value); if (!validationResult.IsValid) { throw new BadRequestException(validationResult.Errors); } var entity = _departureService.Create(value); return(Json(entity)); }
public static Departure Mapp(DepartureDTO entity, IUnitOfWork data) { var crews = data.Set <Crew>().Get(); var airplanes = data.Set <Airplane>().Get(); Mapper.Initialize(cfg => cfg.CreateMap <DepartureDTO, Departure>() .ForMember("Crew", opt => opt.MapFrom(c => crews.FirstOrDefault(a => a.Id == c.CrewId))) .ForMember("Airplane", opt => opt.MapFrom(c => airplanes.FirstOrDefault(a => a.Id == c.AirplaneId)))); var res = Mapper.Map <DepartureDTO, Departure>(entity); return(res); }
public void CreateDeparture(DepartureDTO departure) { if (departure != null) { Departure newDepart = mapper.Map <DepartureDTO, Departure>(departure); unit.DeparturesRepo.Insert(newDepart); unit.SaveChanges(); } else { throw new ArgumentNullException(); } }
public HttpResponseMessage Post([FromBody] DepartureDTO departure) { if (ModelState.IsValid && departure != null) { _service.Post <Departure>(Mapper.Map <DepartureDTO, Departure>(departure)); _service.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public void AddDeparture_When_IdAircraft_empty_Then_throws_exception() { //assign DepartureDTO departure = new DepartureDTO() { FlightNumber = 1, DepartureTime = new DateTime(2018, 2, 2), IdCrew = 1 }; //assert Assert.ThrowsAsync <Exception>(async() => await departureService.AddDeparture(departure)); }