public static List<Schedule> ListSchedule() { using (var db = new TrainContext()) { return db.Schedules.ToList(); } }
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"); } } }
static void Main(string[] args) { using (var db = new TrainContext()) { UserWise.BuyTicket(134, 41); } }
public static List<City> GetAllCities() { List<City> cities = new List<City>(); using (var dbcontext = new TrainContext()) { cities = dbcontext.Cities.ToList(); } return cities; }
public static void AddUser(User user) { using (var db = new TrainContext()) { var us = db.Set<User>(); us.Add(user); db.SaveChanges(); } }
public static void AddCity(City city) { using (var context = new TrainContext()) { var citi = context.Set<City>(); citi.Add(city); context.SaveChanges(); } }
public static List<Train> ListAllTrains() { List<Train> trains = new List<Train>(); using (var db = new TrainContext()) { trains = db.Trains.ToList(); } return trains; }
public static void DeleteTrain(Train train) { using (var db = new TrainContext()) { db.Trains.Attach(train); db.Trains.Remove(train); db.SaveChanges(); } }
public static void AddTrain(Train train) { using (var db = new TrainContext()) { var tr = db.Set<Train>(); tr.Add(train); db.SaveChanges(); } }
public static void DeleteCity(City city) { using (var dbContext = new TrainContext()) { dbContext.Cities.Attach(city); dbContext.Cities.Remove(city); dbContext.SaveChanges(); } }
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(); } } }
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(); } } }
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; }
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; }
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(); } }
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(); } } }
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; }
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; }
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(); } } }
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; }
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}"); } } }