Esempio n. 1
0
 public static List<Schedule> ListSchedule()
 {
     using (var db = new TrainContext())
     {
         return db.Schedules.ToList();
     }
 }
Esempio n. 2
0
 public static void BuyTicket(int userID, int ticketID)
 {
     using (var db = new TrainContext())
     {
         if (ExistsUser(userID) && ExistsTicket(ticketID))
         {
             User temp = GetUser(userID);
             var ticket = from t in db.Tickets
                          where t.ID == ticketID
                          select t;
             foreach (var item in ticket)
             {
                 if (!item.Available)
                 {
                     throw new Exception("Ticket is unavailable");
                 }
                 else
                 {
                     UpdateTicketAvailbality(ticketID);
                     UpdateMoneyInUserCreditcard(userID, ticketID);
                 }
             }
         }
         else
         {
             throw new Exception("Ticket or User not found");
         }
     }
 }
Esempio n. 3
0
 static void Main(string[] args)
 {
     using (var db = new TrainContext())
     {
         UserWise.BuyTicket(134, 41);
     }
 }
Esempio n. 4
0
 public static List<City> GetAllCities()
 {
     List<City> cities = new List<City>();
     using (var dbcontext = new TrainContext())
     {
         cities = dbcontext.Cities.ToList();
     }
     return cities;
 }
Esempio n. 5
0
 public static void AddUser(User user)
 {
     using (var db = new TrainContext())
     {
         var us = db.Set<User>();
         us.Add(user);
         db.SaveChanges();
     }
 }
Esempio n. 6
0
 public static void AddCity(City city)
 {
     using (var context = new TrainContext())
     {
         var citi = context.Set<City>();
         citi.Add(city);
         context.SaveChanges();
     }
 }
Esempio n. 7
0
 public static List<Train> ListAllTrains()
 {
     List<Train> trains = new List<Train>();
     using (var db = new TrainContext())
     {
         trains = db.Trains.ToList();
     }
     return trains;
 }
Esempio n. 8
0
 public static void DeleteTrain(Train train)
 {
     using (var db = new TrainContext())
     {
         db.Trains.Attach(train);
         db.Trains.Remove(train);
         db.SaveChanges();
     }
 }
Esempio n. 9
0
 public static void AddTrain(Train train)
 {
     using (var db = new TrainContext())
     {
         var tr = db.Set<Train>();
         tr.Add(train);
         db.SaveChanges();
     }
 }
Esempio n. 10
0
 public static void DeleteCity(City city)
 {
     using (var dbContext = new TrainContext())
     {
         dbContext.Cities.Attach(city);
         dbContext.Cities.Remove(city);
         dbContext.SaveChanges();
     }
 }
Esempio n. 11
0
 public static void EditTrain(Train train)
 {
     using (var db = new TrainContext())
     {
         var result = db.Trains.SingleOrDefault(t => t.ID == train.ID);
         if (result != null)
         {
             result.Info = train.Info;
             result.NumberOfSeats = train.NumberOfSeats;
             result.Seats = train.Seats;
             db.SaveChanges();
         }
     }
 }
Esempio n. 12
0
 public static void EditSchedule(Schedule sch)
 {
     using (var db = new TrainContext())
     {
         var result = db.Schedules.SingleOrDefault(s => s.ID == sch.ID);
         if (result != null)
         {
             result.StartingCity = sch.StartingCity;
             result.FinalCity = sch.FinalCity;
             result.TicketPrice = sch.TicketPrice;
             result.Train = sch.Train;
             result.TripDuration = sch.TripDuration;
             result.DateAndTime = sch.DateAndTime;
             db.SaveChanges();
         }
     }
 }
Esempio n. 13
0
        private static bool ExistsTicket(int ticketID)
        {
            using (var db = new TrainContext())
            {
                var result = from t in db.Tickets
                             where t.ID == ticketID
                             select t;
                foreach (var item in result)
                {
                    if (item.TripDateTime == null)
                    {
                        return false;
                    }

                }
            }
            return true;
        }
Esempio n. 14
0
 private static bool ExistsUser(int userID)
 {
     using (var db = new TrainContext())
     {
         var result = from u in db.Users
                      where u.ID == userID
                      select u;
         foreach (var item in result)
         {
             if (item.FirstName == null )
             {
                 return false;
             }
         
         }
     }
     return true;
 }
Esempio n. 15
0
 public static void AddSchedule(Schedule sch)
 {
     List<Schedule> otherSchedules = ListSchedule();
     using (var db = new TrainContext())
     {
         foreach (var item in otherSchedules.Where(s => s.Train == sch.Train))
         {
             if (item.Train == sch.Train)
             {
                 if (!IntersectDates(sch.DateAndTime, item.DateAndTime, sch.TripDuration, item.TripDuration))
                 {
                     throw new Exception("Train is unvailable for this trip");
                 }
             }
         }
         var addshedule = db.Set<Schedule>();
         addshedule.Add(sch);
         db.SaveChanges();
     }
 }
Esempio n. 16
0
 private static void UpdateMoneyInUserCreditcard(int userID, int ticketID)
 {
     using (var db = new TrainContext())
     {
         double ticketPrice = GetTicketPrice(ticketID);
         double discount = Discout(userID);
         int userCreditcardID = 0;
         var credicardID = (from u in db.Users
                            where u.ID == userID
                            select u.CreditCardID);
         foreach (var item in credicardID)
         {
             userCreditcardID = item;
         }
         var update = db.CreditCards.SingleOrDefault(c => c.ID == userCreditcardID);
         if (update != null)
         {
             update.Amount -= (ticketPrice - (ticketPrice * discount));
             db.SaveChanges();
         }
     }
 }
Esempio n. 17
0
 private static User GetUser(int id)
 {
     User temp = new User();
     using (var db = new TrainContext())
     {
         var user = from u in db.Users
                    where u.ID == id
                    select u;
         foreach (var item in user)
         {
             temp = item;
         }
     }
     return temp;
 }
Esempio n. 18
0
 private static double Discout(int userID)
 {
     double disc = 0;
     using (var db = new TrainContext())
     {
         var discount = from u in db.Users
                        join d in db.Discounts on u.DiscountNumber equals d.ID
                        where u.ID == userID
                        select new { Discout = d.DiscountAmount };
         foreach (var item in discount)
         {
             disc = item.Discout;
         }
     }
     return disc;
 }
Esempio n. 19
0
 private static void UpdateTicketAvailbality(int ticketID)
 {
     using (var db = new TrainContext())
     {
         var availble = db.Tickets.SingleOrDefault(t => t.ID == ticketID);
         if (availble != null)
         {
             availble.Available = false;
             db.SaveChanges();
         }
     }
 }
Esempio n. 20
0
 private static double GetTicketPrice(int ticketID)
 {
     double price = 0;
     using (var db = new TrainContext())
     {
         var getPrice = from t in db.Tickets
                        where t.ID == ticketID
                        select t;
         foreach (var item in getPrice)
         {
             price = item.Price;
         }
     }
     return price;
 }
Esempio n. 21
0
        public static void GetAllTrips(City start, City end)
        {
            using (var db = new TrainContext())
            {
                var startC = (from s in db.Schedules
                              join c in db.Cities on s.StartingCity equals c.ID
                              join v in db.Cities on s.FinalCity equals v.ID
                              where (start.ID.Equals(c.ID) && end.ID.Equals(v.ID))
/*                              where start.ID.Equals(c.ID)
                              let cityTwo = (from s in db.Schedules
                                             join c in db.Cities on s.FinalCity equals c.ID
                                             where end.ID.Equals(c.ID)
                                             select c.Name)*/
                              select new { firstCity = c.Name, finalCity = v.Name,PRiCe=s.TicketPrice,DUration=s.TripDuration, DATE = s.DateAndTime});
                foreach (var item in startC)
                {
                    Console.WriteLine($"from {item.firstCity} to {string.Join(" ", item.finalCity)} TicketPrice: {item.PRiCe}, DateTime: {item.DATE}, Duration: {item.DUration}");
                }

            }
        }