Пример #1
0
        public Response Add(AddViewModel model)
        {
            if (!User.IsAuthenticated)
            {
                return(Redirect("/Home"));
            }

            var(isValid, errors) = tripService.IsValid(model);

            if (!isValid)
            {
                return(View(errors, "/Error"));
            }

            try
            {
                tripService.AddTrip(model);
            }
            catch (Exception)
            {
                return(View(new List <ErrorViewModel>()
                {
                    new ErrorViewModel("Unexpected error!")
                }));
            }

            return(Redirect("/Trips/All"));
        }
Пример #2
0
        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 }));
            }
        }
Пример #3
0
 public IActionResult AddTrip([FromBody] Trip trip)
 {
     if (trip != null)
     {
         _service.AddTrip(trip);
     }
     return(Ok());
 }
Пример #4
0
        public async Task <ServiceResponse <bool> > AddTrip(TripDTO trip)
        {
            return(await HandleApiOperationAsync(async() => {
                await _tripService.AddTrip(trip);

                return new ServiceResponse <bool>(true);
            }));
        }
Пример #5
0
        public IActionResult AddTrip([FromBody] Trip trip)
        {
            if (trip != null)
            {
                _tripService.AddTrip(trip);
            }

            return(CreatedAtAction("AddTrip", trip));
        }
Пример #6
0
 public IActionResult AddTrip([FromBody] Trip trip)
 {
     // check if the trip is null
     if (trip != null)
     {
         _service.AddTrip(trip);
     }
     return(Ok());
 }
Пример #7
0
        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);
        }
Пример #8
0
 public IActionResult AddTrip([FromBody] Trip trip)
 {
     try
     {
         if (trip != null)
         {
             _service.AddTrip(trip);
             return(Ok(trip));
         }
         return(BadRequest("Trip was not added!"));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Пример #9
0
        public Response Add(TripViewModel model)
        {
            var(isValid, errors) = tripService.ValidateModel(model);

            if (!isValid)
            {
                return(View(errors, "/Error"));
            }

            try
            {
                tripService.AddTrip(model);
            }
            catch (Exception)
            {
                return(View(new List <ErrorViewModel>()
                {
                    new ErrorViewModel("Unexpected Error")
                }, "/Error"));
            }

            return(Redirect("/Trips/All"));
        }
Пример #10
0
        public async Task <IActionResult> CreateNewTrip([FromBody] TripViewModel tripViewModel)
        {
            if (ModelState.IsValid)
            {
                var newTrip = new TripModel
                {
                    Departure     = tripViewModel.Departure,
                    Destination   = tripViewModel.Destination,
                    DepartureTime = tripViewModel.DepartureTime,
                    ArrivalTime   = tripViewModel.ArrivalTime,
                    Cost          = tripViewModel.Cost,
                    Miles         = tripViewModel.Miles
                };

                await _tripService.AddTrip(newTrip);

                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #11
0
 public void PostTrip(Trip trip)
 {
     service.AddTrip(trip);
 }