public async void Create() { var tripServiceMock = new Mock <ITripService> (); var pathServiceMock = new Mock <IPathService> (); string lineID = "Line:1"; string pathID = "Path:1"; string tripDepartureTime = "20:12:10"; var trip = new Trip(lineID, pathID, tripDepartureTime); var tripDTO = new TripDTO(trip.Id.AsGuid(), new LineId(lineID), new PathId(pathID), tripDepartureTime); var path = new PathId(pathID); var pathDTO = new PathDTO(pathID, true, new List <SegmentDTO> ()); var creatingTrip = new CreatingTripDTO(lineID, pathID, tripDepartureTime); pathServiceMock.Setup(_ => _.GetById(path)).ReturnsAsync(pathDTO); tripServiceMock.Setup(_ => _.AddTrip(creatingTrip, new List <CreatingNodePassageDTO> ())).ReturnsAsync(tripDTO); var controller = new TripController(tripServiceMock.Object, pathServiceMock.Object); var actual = await controller.Create(creatingTrip); Assert.NotNull(actual); Assert.NotNull(actual.Result); }
public async Task <TripDTO> AddTrip(CreatingTripDTO creatingTripDTO, List <CreatingNodePassageDTO> creatingNodePassageListDTO) { List <NodePassage> nodePassagesList = new List <NodePassage> (); foreach (var item in creatingNodePassageListDTO) { NodePassage nodePassage = new NodePassage( item.nodeId, item.passageTime ); nodePassagesList.Add(nodePassage); } var trip = new Trip( creatingTripDTO.lineID, creatingTripDTO.pathID, creatingTripDTO.tripDepartureTime, nodePassagesList ); var savedTrip = await this._repo.AddAsync(trip); await this._unitOfWork.CommitAsync(); return(_mapper.DomainToDTO(savedTrip)); }
public async Task <ActionResult <TripDTO> > Create(CreatingTripDTO dto) { try { PathDTO path = await this.pathService.GetById(new PathId (dto.pathID)); var nodePassageList = new List <CreatingNodePassageDTO> (); int departTime = TimeUtils.fromTimeToSec(new Time(dto.tripDepartureTime)); int passageTime = 0; for (int i = 0; i < path.segments.Count; i++) { var segment = path.segments[i]; CreatingNodePassageDTO nodePassage; if (i == 0) { nodePassage = new CreatingNodePassageDTO(segment.firstNodeID.code, dto.tripDepartureTime); passageTime += TimeUtils.fromTimeToSec(new Time(dto.tripDepartureTime)); nodePassageList.Add(nodePassage); } else { var previousSegment = path.segments[i - 1]; int nodePassageSec = passageTime + previousSegment.travelTimeBetweenNodes; passageTime = nodePassageSec; string nodePassageString = TimeUtils.fromSecToString(nodePassageSec); nodePassage = new CreatingNodePassageDTO(segment.firstNodeID.code, nodePassageString); nodePassageList.Add(nodePassage); if (i == path.segments.Count - 1) { nodePassageSec = passageTime + segment.travelTimeBetweenNodes; nodePassageString = TimeUtils.fromSecToString(nodePassageSec); CreatingNodePassageDTO lastNodePassage = new CreatingNodePassageDTO(segment.secondNodeID.code, nodePassageString); nodePassageList.Add(lastNodePassage); } } } var trip = await tripService.AddTrip(dto, nodePassageList); var a = CreatedAtAction( nameof(GetById), new { id = trip.Id }, trip ); return(a); } catch (BusinessRuleValidationException ex) { return(BadRequest(new { Message = ex.Message })); } }
public async Task <List <ActionResult <TripDTO> > > CreateComplex(CreatingComplexTripDTO dto) { List <ActionResult <TripDTO> > trips = new List <ActionResult <TripDTO> > (); int outwardTripDepartTimeSec = 0; for (int i = 0; i < dto.nTrips; i++) { if (i % 2 == 0) { int frequencySec = (dto.frequency * 60) * (i / 2); int departTimeSec = TimeUtils.fromStringToSec(dto.tripDepartureTime); // ======================================================================================== outwardTripDepartTimeSec = departTimeSec + frequencySec; Time outwardTripDepartTime = new Time(outwardTripDepartTimeSec); CreatingTripDTO creatingOutwardTripDTO = new CreatingTripDTO(dto.lineID, dto.outwardPathID, outwardTripDepartTime.ToString()); Task <ActionResult <TripDTO> > outwardTripTask = Create(creatingOutwardTripDTO); if (!outwardTripTask.IsCompletedSuccessfully) { new Exception("Something went wrong during the creation of a trip."); } ActionResult <TripDTO> outwardTripResult = outwardTripTask.Result; trips.Add(outwardTripResult); } else { // ======================================================================================== int outwardTripDurationSec = await this.pathService.GetPathDurationById(new PathId (dto.outwardPathID)); int returnTripDepartTimeSec = outwardTripDepartTimeSec + outwardTripDurationSec; Time returnTripDepartTime = new Time(returnTripDepartTimeSec); CreatingTripDTO creatingReturnTripDTO = new CreatingTripDTO(dto.lineID, dto.returnPathID, returnTripDepartTime.ToString()); Task <ActionResult <TripDTO> > returnTripTask = Create(creatingReturnTripDTO); if (!returnTripTask.IsCompletedSuccessfully) { new Exception("Something went wrong during the creation of a trip."); } ActionResult <TripDTO> returnTripResult = returnTripTask.Result; trips.Add(returnTripResult); } } return(trips); }
private async Task <List <ImportedTripDTO> > ImportTrips(XmlDocument doc) { var importedTripsList = new List <ImportedTripDTO>(); XmlNodeList trips = doc.GetElementsByTagName("Trip"); foreach (XmlNode trip in trips) { List <CreatingNodePassageDTO> nodePassages = new List <CreatingNodePassageDTO> (); string tripKey = trip.Attributes["key"].InnerText; string tripPath = trip.Attributes["Path"].InnerText; string tripLine; if (trip.Attributes["Line"] == null) { tripLine = await this._lineService.GetLineOfPath(tripPath); } else { tripLine = trip.Attributes["Line"].InnerText; } string tripDepartureTime = null; XmlNodeList passingTimes = trip.FirstChild.ChildNodes; for (int i = 0; i < passingTimes.Count; i++) { XmlNode passingTime = passingTimes[i]; int nodePassageTimeSec = Int32.Parse(passingTime.Attributes["Time"].InnerText); string nodePassageNode = passingTime.Attributes["Node"].InnerText; string nodePassageTime = TimeUtils.fromSecToString(nodePassageTimeSec); if (i == 0) { tripDepartureTime = nodePassageTime; } CreatingNodePassageDTO nodePassageDTO = new CreatingNodePassageDTO(nodePassageNode, nodePassageTime); nodePassages.Add(nodePassageDTO); } CreatingTripDTO creatingTrip = new CreatingTripDTO(tripLine, tripPath, tripDepartureTime); var tripDTO = await _tripService.AddTrip(creatingTrip, nodePassages); ImportedTripDTO importedTrip = new ImportedTripDTO(tripKey, tripDTO.Id, tripDTO.lineID, tripDTO.pathID, tripDTO.tripDepartureTime, tripDTO.nodePassageListDTO); importedTripsList.Add(importedTrip); } return(importedTripsList); }
public async void AddTrip() { var repo = new Mock <ITripRepository> (); var uow = new Mock <IUnitOfWork> (); string lineID = "Line:1"; string pathID = "Path:1"; string tripDepartureTime = "20:12:10"; var trip = new Trip(lineID, pathID, tripDepartureTime); var tripDTO = new TripDTO(trip.Id.AsGuid(), new LineId(lineID), new PathId(pathID), tripDepartureTime); var creatingTrip = new CreatingTripDTO(lineID, pathID, tripDepartureTime); repo.Setup(_ => _.AddAsync(trip)).ReturnsAsync(trip); var service = new TripService(repo.Object, uow.Object); var actual = await service.AddTrip(creatingTrip, new List <CreatingNodePassageDTO> ()); Assert.Equal(tripDTO, actual); }