public List <TripVm> GetTrips()
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var tripList = db.Trips
                               .Include(a => a.Line)
                               .Include(a => a.OperationalInterval)
                               .Include(a => a.Schedules)
                               .ToList()
                               .Select(a => new TripVm
                {
                    Id = a.Id,
                    OperationalIntervalId   = a.OperationalIntervalId,
                    OperationalIntervalName = a.OperationalInterval.Name,
                    LineId        = a.LineId,
                    LineName      = a.Line.LineName,
                    ArrivalTime   = a.ArrivalTime.ToString(@"hh\:mm"),
                    DepartureTime = a.DepartureTime.ToString(@"hh\:mm"),
                    Schedules     = a.Schedules.Select(b => new ScheduleVm
                    {
                        ArrivalTime   = b.ArrivalTime,
                        DepartureTime = b.DepartureTime,
                        Id            = b.Id,
                        StationId     = b.StationId,
                        StationName   = b.Station.StationName
                    }).ToList()
                }).ToList();

                return(tripList);
            }
        }
        public TripVm GetTrip(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var trip = db.Trips.Where(a => a.Id == id).ToList().Select(a => new TripVm
                {
                    Id = a.Id,
                    OperationalIntervalId   = a.OperationalIntervalId,
                    OperationalIntervalName = a.OperationalInterval.Name,
                    LineId        = a.LineId,
                    LineName      = a.Line.LineName,
                    DepartureTime = a.DepartureTime.ToString(@"hh\:mm"),
                    ArrivalTime   = a.ArrivalTime.ToString(@"hh\:mm"),
                    Schedules     = a.Schedules.Select(b => new ScheduleVm
                    {
                        ArrivalTime   = b.ArrivalTime,
                        DepartureTime = b.DepartureTime,
                        Id            = b.Id,
                        StationId     = b.StationId,
                        StationName   = b.Station.StationName
                    }).ToList()
                }).SingleOrDefault();

                return(trip);
            }
        }
        public int AddTrip(TripVm tripVm, string userName)
        {
            var trip = new Trip
            {
                OperationalIntervalId = tripVm.OperationalIntervalId,
                LineId        = tripVm.LineId,
                ArrivalTime   = TimeSpan.Parse(tripVm.ArrivalTime),
                DepartureTime = TimeSpan.Parse(tripVm.DepartureTime)
            };

            if (tripVm.Schedules != null)
            {
                trip.Schedules = tripVm.Schedules.Select(a => new Schedule
                {
                    ArrivalTime   = a.ArrivalTime,
                    DepartureTime = a.DepartureTime,
                    StationId     = a.StationId
                }).ToList();
            }

            using (Oblig1Context db = new Oblig1Context())
            {
                db.Trips.Add(trip);
                db.SaveChanges();
            }

            return(trip.Id);
        }
 public void DeleteStation(int id)
 {
     using (Oblig1Context db = new Oblig1Context())
     {
         var stationRemove = db.Stations.FirstOrDefault(a => a.Id == id);
         db.Stations.Remove(stationRemove);
         db.SaveChanges();
     }
 }
 public void DeleteOis(int id)
 {
     using (Oblig1Context db = new Oblig1Context())
     {
         var oisRemove = db.OperationalIntervals.FirstOrDefault(a => a.Id == id);
         db.OperationalIntervals.Remove(oisRemove);
         db.SaveChanges();
     }
 }
 public void DeleteLine(int?id)
 {
     using (Oblig1Context db = new Oblig1Context())
     {
         var lineRemove = db.Lines.SingleOrDefault(a => a.Id == id.Value);
         db.Lines.Remove(lineRemove);
         db.SaveChanges();
     }
 }
 public StationVm GetStation(int id)
 {
     using (Oblig1Context db = new Oblig1Context())
     {
         var station = db.Stations.Where(a => a.Id == id).Select(a => new StationVm {
             StationId = a.Id, StationName = a.StationName
         }).FirstOrDefault();
         return(station);
     }
 }
        public void UpdateStation(StationVm stationEdit)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var stationDb = db.Stations.Where(a => a.Id == stationEdit.StationId).FirstOrDefault();

                stationDb.StationName = stationEdit.StationName;
                db.SaveChanges();
            }
        }
        public List <StationVm> GetStations()
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var stationsList = db.Stations.Select(a => new StationVm {
                    StationId = a.Id, StationName = a.StationName
                }).ToList();

                return(stationsList);
            }
        }
Beispiel #10
0
        public PriceVm GetPrice(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var price = db.Trips.Where(a => a.Id == id).Select(b => new PriceVm
                {
                    BasePrice  = b.BasePrice,
                    StopsPrice = b.StopsPrice
                }).SingleOrDefault();

                return(price);
            }
        }
Beispiel #11
0
        public List <StationVm> GetStationsByName(string searchterm)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var stations = db.Stations.Where(a => a.StationName.StartsWith(searchterm)).Select(b => new StationVm
                {
                    StationId   = b.Id,
                    StationName = b.StationName
                }).ToList();

                return(stations);
            }
        }
        public void FinalizeTicket(FinalizeTicketVm finalizeTicket)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ticketToFinalize = db.Tickets.Single(a => a.Id == finalizeTicket.TicketId);

                ticketToFinalize.Email      = finalizeTicket.Email;
                ticketToFinalize.CardNumber = finalizeTicket.CardNumber;
                ticketToFinalize.State      = TicketState.Complete;

                db.SaveChanges();
            }
        }
Beispiel #13
0
        public void UpdateLine(LineVm lineEdit)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var lineDb = db.Lines.Where(a => a.Id == lineEdit.Id).SingleOrDefault();

                lineDb.LineName    = lineEdit.LineName;
                lineDb.DepartureId = lineEdit.DepartureStationId;
                lineDb.ArrivalId   = lineEdit.ArrivalStationId;

                db.SaveChanges();
            }
        }
Beispiel #14
0
        public int GetNumberOfStops(int tripId, int departureStationId, int arrivalStationId)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var trip = db.Trips.SingleOrDefault(a => a.Id == tripId);

                var arrivalSchedule   = trip.Schedules.SingleOrDefault(a => a.StationId == arrivalStationId);
                var departureSchedule = trip.Schedules.SingleOrDefault(a => a.StationId == departureStationId);

                return(trip.Schedules
                       .Where(a => a.DepartureTime > departureSchedule.DepartureTime && a.ArrivalTime < arrivalSchedule.ArrivalTime)
                       .Count());
            }
        }
        public int AddStation(StationVm stationVm)
        {
            var station = new Station
            {
                StationName = stationVm.StationName
            };

            using (Oblig1Context db = new Oblig1Context())
            {
                db.Stations.Add(station);
                db.SaveChanges();
            }

            return(station.Id);
        }
Beispiel #16
0
        public LineVm GetLine(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var line = db.Lines.Where(a => a.Id == id).Select(a => new LineVm
                {
                    Id                 = a.Id,
                    LineName           = a.LineName,
                    DepartureStationId = a.DepartureId,
                    ArrivalStationId   = a.ArrivalId,
                    DepartureStation   = a.Departure.StationName,
                    ArrivalStation     = a.Arrival.StationName
                }).SingleOrDefault();

                return(line);
            }
        }
        public TicketSummaryVm GetTicketSummary(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ticket = db.Tickets.Find(id);

                var departureTime = ticket.Trip.Schedules.SingleOrDefault(a => a.StationId == ticket.DepartureStationId);

                return(new TicketSummaryVm
                {
                    ArrivalStation = ticket.ArrivalStation.StationName,
                    DepartureStation = ticket.DepartureStation.StationName,
                    LineName = ticket.Trip.Line.LineName,
                    TicketDateAndTime = ticket.JourneyDate.Date.Add(departureTime.DepartureTime.Value)
                });
            }
        }
        public TicketVm GetTicket(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ticket = db.Tickets.Where(a => a.Id == id).SingleOrDefault();

                return(new TicketVm
                {
                    Id = ticket.Id,
                    TripId = ticket.TripId,
                    Date = ticket.JourneyDate,
                    DepartureStationId = ticket.DepartureStationId,
                    ArrivalStationId = ticket.ArrivalStationId,
                    Price = ticket.Price
                });
            }
        }
Beispiel #19
0
        public int AddLine(LineVm newLine)
        {
            var line = new Line
            {
                LineName    = newLine.LineName,
                DepartureId = newLine.DepartureStationId,
                ArrivalId   = newLine.ArrivalStationId
            };

            using (Oblig1Context db = new Oblig1Context())
            {
                db.Lines.Add(line);
                db.SaveChanges();
            }

            return(line.Id);
        }
Beispiel #20
0
        public List <LineVm> GetLines()
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var linesList = db.Lines.Select(a => new LineVm
                {
                    Id                 = a.Id,
                    LineName           = a.LineName,
                    DepartureStationId = a.DepartureId,
                    ArrivalStationId   = a.ArrivalId,
                    DepartureStation   = a.Departure.StationName,
                    ArrivalStation     = a.Arrival.StationName
                }).ToList();

                return(linesList);
            }
        }
        public void UpdateOperationalInterval(OperationalIntervalVm ois)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var oisDb = db.OperationalIntervals.Where(a => a.Id == ois.Id).FirstOrDefault();

                oisDb.Name      = ois.Name;
                oisDb.StartDate = ois.StartDate;
                oisDb.EndDate   = ois.EndDate;
                oisDb.Monday    = ois.Monday;
                oisDb.Tuesday   = ois.Tuesday;
                oisDb.Wednesday = ois.Wednesday;
                oisDb.Thursday  = ois.Thursday;
                oisDb.Friday    = ois.Friday;
                oisDb.Saturday  = ois.Saturday;
                oisDb.Sunday    = ois.Sunday;

                db.SaveChanges();
            }
        }
Beispiel #22
0
        public void DeleteTrip(int id, string userName)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var tripDelete = db.Trips.SingleOrDefault(a => a.Id == id);

                //Husk å legge if på flere singleordefault

                if (tripDelete != null)
                {
                    foreach (var schedule in tripDelete.Schedules.ToList())
                    {
                        db.Schedules.Remove(schedule);
                    }

                    db.Trips.Remove(tripDelete);
                    db.SaveChanges();
                }
            }
        }
        public int AddTicket(TicketVm ticket)
        {
            var travelTicket = new Ticket
            {
                ArrivalStationId   = ticket.ArrivalStationId,
                DepartureStationId = ticket.DepartureStationId,
                TripId             = ticket.TripId,
                JourneyDate        = ticket.Date,
                Price = ticket.Price,
                State = TicketState.Pending
            };

            using (Oblig1Context db = new Oblig1Context())
            {
                db.Tickets.Add(travelTicket);
                db.SaveChanges();
            }

            return(travelTicket.Id);
        }
        public List <OperationalIntervalVm> GetOperationalIntervals()
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ois = db.OperationalIntervals.Select(a => new OperationalIntervalVm
                {
                    Id        = a.Id,
                    Name      = a.Name,
                    StartDate = a.StartDate,
                    EndDate   = a.EndDate,
                    Monday    = a.Monday,
                    Tuesday   = a.Tuesday,
                    Wednesday = a.Wednesday,
                    Thursday  = a.Thursday,
                    Friday    = a.Friday,
                    Saturday  = a.Saturday,
                    Sunday    = a.Sunday
                }).ToList();

                return(ois);
            }
        }
        public OperationalIntervalVm GetOperationalInterval(int id)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ois = db.OperationalIntervals.Where(a => a.Id == id).Select(a => new OperationalIntervalVm
                {
                    Id        = a.Id,
                    Name      = a.Name,
                    StartDate = a.StartDate,
                    EndDate   = a.EndDate,
                    Monday    = a.Monday,
                    Tuesday   = a.Tuesday,
                    Wednesday = a.Wednesday,
                    Thursday  = a.Thursday,
                    Friday    = a.Friday,
                    Saturday  = a.Saturday,
                    Sunday    = a.Sunday
                }).FirstOrDefault();

                return(ois);
            }
        }
        public List <TripVm> GetTrips()
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var tripList = new List <TripVm>();
                var tripItem = new TripVm()
                {
                    Id = 1,
                    OperationalIntervalId   = 1,
                    OperationalIntervalName = "Sommerruter",
                    LineId        = 1,
                    LineName      = "Stavanger - Audnedal",
                    DepartureTime = "12:15",
                    ArrivalTime   = "14:35"
                };
                tripList.Add(tripItem);
                tripList.Add(tripItem);
                tripList.Add(tripItem);

                return(tripList);
            }
        }
        public int AddOis(OperationalIntervalVm oisVm)
        {
            var ois = new OperationalInterval
            {
                Name      = oisVm.Name,
                StartDate = oisVm.StartDate,
                EndDate   = oisVm.EndDate,
                Monday    = oisVm.Monday,
                Tuesday   = oisVm.Tuesday,
                Wednesday = oisVm.Wednesday,
                Thursday  = oisVm.Thursday,
                Friday    = oisVm.Friday,
                Saturday  = oisVm.Saturday,
                Sunday    = oisVm.Sunday
            };

            using (Oblig1Context db = new Oblig1Context())
            {
                db.OperationalIntervals.Add(ois);
                db.SaveChanges();
            }

            return(ois.Id);
        }
Beispiel #28
0
        public void UpdateTrip(TripVm tripVm, string userName)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var tripDb = db.Trips.Where(a => a.Id == tripVm.Id).SingleOrDefault();

                if (tripDb != null)
                {
                    var auditLog = new AuditLog
                    {
                        TimeStamp = DateTime.Now,
                        Model     = nameof(Trip),
                        Type      = AuditType.Updated,
                        User      = userName
                    };

                    var sb = new StringBuilder();

                    if (tripDb.LineId != tripVm.LineId)
                    {
                        sb.AppendLine($"Updated Line from: {tripDb.Line.LineName} To: {tripVm.LineName}");
                    }

                    if (tripDb.OperationalIntervalId != tripVm.OperationalIntervalId)
                    {
                        sb.AppendLine($"Updated OperationalInterval from: {tripDb.OperationalInterval.Name} To: {tripVm.OperationalIntervalName}");
                    }

                    if (tripDb.LineId != tripVm.LineId)
                    {
                        sb.AppendLine($"Updated Line from: {tripDb.Line.LineName} To: {tripVm.LineName}");
                    }

                    var arrivalTime   = TimeSpan.Parse(tripVm.ArrivalTime);
                    var departureTime = TimeSpan.Parse(tripVm.DepartureTime);

                    if (tripDb.ArrivalTime != arrivalTime)
                    {
                        sb.AppendLine($"Updated ArrivalTime from: {tripDb.ArrivalTime} To: {arrivalTime}");
                    }

                    if (tripDb.DepartureTime != departureTime)
                    {
                        sb.AppendLine($"Updated DepartureTime from: {tripDb.DepartureTime} To: {departureTime}");
                    }

                    auditLog.Log = sb.ToString();

                    tripDb.OperationalIntervalId = tripVm.OperationalIntervalId;
                    tripDb.LineId        = tripVm.LineId;
                    tripDb.ArrivalTime   = arrivalTime;
                    tripDb.DepartureTime = departureTime;

                    if (tripVm.Schedules == null || tripVm.Schedules.Count == 0)
                    {
                        db.Schedules.RemoveRange(tripDb.Schedules);
                    }
                    else
                    {
                        var scheduleIds       = tripVm.Schedules.Select(a => a.Id).ToList();
                        var schedulesToRemove = tripDb.Schedules.Where(a => !scheduleIds.Contains(a.Id));
                        var schedulesToUpdate = tripDb.Schedules.Where(a => scheduleIds.Contains(a.Id));
                        var schedulesToAdd    = tripVm.Schedules.Where(a => a.Id == 0);

                        if (schedulesToRemove.Any())
                        {
                            db.Schedules.RemoveRange(schedulesToRemove);
                        }

                        foreach (var schedule in schedulesToUpdate)
                        {
                            var scheduleVm = tripVm.Schedules.Single(a => a.Id == schedule.Id);

                            schedule.StationId     = scheduleVm.StationId;
                            schedule.ArrivalTime   = scheduleVm.ArrivalTime;
                            schedule.DepartureTime = scheduleVm.DepartureTime;
                        }

                        foreach (var schedule in schedulesToAdd)
                        {
                            tripDb.Schedules.Add(new Schedule
                            {
                                StationId     = schedule.StationId,
                                ArrivalTime   = schedule.ArrivalTime,
                                DepartureTime = schedule.DepartureTime
                            });
                        }
                    }

                    if (!string.IsNullOrEmpty(auditLog.Log))
                    {
                        db.AuditLogs.Add(auditLog);
                    }

                    db.SaveChanges();
                }
            }
        }
Beispiel #29
0
        public List <DepartureTimeVm> GetDepartureTimes(TravelSearchVm travelSearch)
        {
            using (Oblig1Context db = new Oblig1Context())
            {
                var ois = db.OperationalIntervals
                          .Where(a => a.StartDate <= travelSearch.Date && a.EndDate >= travelSearch.Date);

                switch (travelSearch.Date.DayOfWeek)
                {
                case DayOfWeek.Sunday:
                    ois = ois.Where(a => a.Sunday);
                    break;

                case DayOfWeek.Monday:
                    ois = ois.Where(a => a.Monday);
                    break;

                case DayOfWeek.Tuesday:
                    ois = ois.Where(a => a.Tuesday);
                    break;

                case DayOfWeek.Wednesday:
                    ois = ois.Where(a => a.Wednesday);
                    break;

                case DayOfWeek.Thursday:
                    ois = ois.Where(a => a.Thursday);
                    break;

                case DayOfWeek.Friday:
                    ois = ois.Where(a => a.Friday);
                    break;

                case DayOfWeek.Saturday:
                    ois = ois.Where(a => a.Saturday);
                    break;
                }

                var trips = ois.SelectMany(a => a.Trips)
                            .Where(a => a.Schedules.Any(b => b.StationId == travelSearch.DepartureId) && a.Schedules.Any(b => b.StationId == travelSearch.ArrivalId))
                            .ToList();

                var departureTimeList = new List <DepartureTimeVm>();

                foreach (var trip in trips)
                {
                    var arrivalSchedule   = trip.Schedules.SingleOrDefault(a => a.StationId == travelSearch.ArrivalId);
                    var departureSchedule = trip.Schedules.SingleOrDefault(a => a.StationId == travelSearch.DepartureId);

                    if (departureSchedule.DepartureTime < arrivalSchedule.ArrivalTime)
                    {
                        var stops = trip.Schedules
                                    .Where(a => a.DepartureTime > departureSchedule.DepartureTime && a.ArrivalTime < arrivalSchedule.ArrivalTime)
                                    .OrderBy(a => a.DepartureTime)
                                    .Select(a => new DepartureTimeStop
                        {
                            Name          = a.Station.StationName,
                            DepartureTime = a.DepartureTime.Value
                        }).ToList();

                        departureTimeList.Add(new DepartureTimeVm
                        {
                            TripId               = trip.Id,
                            DepartureStationId   = travelSearch.DepartureId,
                            DepartureStation     = departureSchedule.Station.StationName,
                            DepartureStationTime = departureSchedule.DepartureTime.Value,
                            ArrivalStationId     = travelSearch.ArrivalId,
                            ArrivalStation       = arrivalSchedule.Station.StationName,
                            ArrivalStationTime   = arrivalSchedule.ArrivalTime.Value,
                            Stops = stops
                        });
                    }
                }

                return(departureTimeList);
            }
        }