public IActionResult CreateStop
            ([Bind(Prefix = nameof(TimeTableIndexWithRoutesViewModel.CreateStop))] CreateStopInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.RedirectToAction(nameof(DetailsRoute), new { id = model.RouteId }));
            }

            var result = this.TimeTableService.CreateStop(model);

            if (!result.HasError)
            {
                this.TempData.Put(MessageConstants.Name, new MessageModel
                {
                    Type    = MessageType.Success,
                    Message = result.Message
                });
            }
            else
            {
                this.TempData.Put(MessageConstants.Name, new MessageModel
                {
                    Type    = MessageType.Danger,
                    Message = result.Message
                });
            }

            return(this.RedirectToAction(nameof(DetailsRoute), new { id = model.RouteId }));
        }
Exemplo n.º 2
0
        public TimeTableIndexWithRoutesViewModel GetModelWithRouteStops(int?id)
        {
            var route = this.Db.Routes
                        .Where(x => x.Id == id)
                        .Include(x => x.Line)
                        .Include(x => x.TimeTables)
                        .FirstOrDefault();

            if (route == null)
            {
                return(null);
            }

            var allStations = this.Db.Stations.ToList();
            var timeTables  = route.TimeTables.Select(x => new TimeTableViewModel
            {
                Id            = x.Id,
                StationName   = allStations.FirstOrDefault(s => s.Id == x.StationId).StationName,
                DepartureTime = x.DepartureTime
            }).ToList();

            //TODO: GET ONLY STATIONS THAT LINE HAVE
            var stations = allStations
                           .Select(x => new SelectListItem
            {
                Text  = x.StationName,
                Value = x.StationName
            })
                           .ToList();

            var createStopModel = new CreateStopInputModel
            {
                RouteId  = route.Id,
                Stations = stations
            };

            var orderTimeTables = timeTables.OrderBy(x => x.DepartureTime).ToList();
            var model           = new TimeTableIndexWithRoutesViewModel
            {
                LineId     = route.LineId,
                Tab        = "2",
                Day        = route.DayType.ToString(),
                Direction  = route.Direction.ToString(),
                TimeTables = orderTimeTables,
                CreateStop = createStopModel
            };

            return(model);
        }
Exemplo n.º 3
0
        public BaseModel CreateStop(CreateStopInputModel model)
        {
            var route = this.Db.Routes
                        .Where(x => x.Id == model.RouteId)
                        .Include(x => x.TimeTables)
                        .Include(x => x.Line).FirstOrDefault();

            if (route == null)
            {
                this.BaseModel.HasError = true;
                this.BaseModel.Message  = MessageConstants.InvalidRouteId;
                return(this.BaseModel);
            }

            var stationId = this.Db.Stations.FirstOrDefault(x => x.StationName == model.StationName).Id;

            var hasStation = route.TimeTables.Any(x => x.StationId == stationId);
            var hasDepTime = route.TimeTables.Any(x => x.DepartureTime == model.DepartureTime);

            if (hasStation)
            {
                this.BaseModel.HasError = true;
                this.BaseModel.Message  = string.Format(MessageConstants.HaveStopName, model.StationName);
                return(this.BaseModel);
            }

            if (hasDepTime)
            {
                this.BaseModel.HasError = true;
                this.BaseModel.Message  = MessageConstants.HaveStopHour;
                return(this.BaseModel);
            }

            //create TT
            var stop = new TimeTable
            {
                StationId     = stationId,
                DepartureTime = model.DepartureTime,
                RouteId       = route.Id
            };

            try
            {
                this.Db.TimeTables.Add(stop);
                this.Db.SaveChanges();

                this.BaseModel.HasError = false;
                this.BaseModel.Message  = MessageConstants.CreateStop;
            }
            catch (Exception)
            {
                this.BaseModel.HasError = true;
                this.BaseModel.Message  = MessageConstants.NoCreateStop;
                return(this.BaseModel);
            }

            var lineId = route.LineId;
            var allStationsIdsForLineId = this.Db.LineStations
                                          .Where(x => x.LineId == lineId)
                                          .Select(x => x.StationId).ToList();

            foreach (var timeTable in route.TimeTables)
            {
                var containsStation = allStationsIdsForLineId.Contains(timeTable.StationId);
                if (!containsStation)
                {
                    var lineStation = new LineStation
                    {
                        LineId    = lineId,
                        StationId = timeTable.StationId
                    };

                    this.Db.LineStations.Add(lineStation);
                    this.Db.SaveChanges();
                }
            }

            return(this.BaseModel);
        }