コード例 #1
0
        public ActionResult Create([Bind(Include = "Id,PassengerName,Contact,Address,UpCounter,DownCounter,BusNo,SeatNo,BusCategory,BusSchedule,ticketPrice")] tblTicketSell tblTicketSell)
        {
            if (ModelState.IsValid)
            {
                db.tblTicketSells.Add(tblTicketSell);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.BusNo       = new SelectList(db.tblBus, "Id", "BusNo", tblTicketSell.BusNo);
            ViewBag.BusCategory = new SelectList(db.tblBusCategories, "Id", "BusCategory", tblTicketSell.BusCategory);
            ViewBag.DownCounter = new SelectList(db.tblCounters, "Id", "CounterName", tblTicketSell.DownCounter);
            ViewBag.UpCounter   = new SelectList(db.tblCounters, "Id", "CounterName", tblTicketSell.UpCounter);
            return(View(tblTicketSell));
        }
コード例 #2
0
        public static void GenerateBusStations(BusTicketContext context)
        {
            var names = new string[] {
                "North Station",
                "West Station",
                "South Station",
                "East Station",
                "NorthEast Station",
                "NorthWest Station",
                "Central Station"
            };

            var townIds = context.Towns.Select(p => p.Id).ToArray();

            var validBusStations = new List <BusStation>();

            var rnd = new Random();

            for (int i = 0; i < names.Length; i++)
            {
                var townsIndex = rnd.Next(0, townIds.Length - 1);

                var busStation = new BusStation
                {
                    Name   = names[i],
                    TownId = townIds[townsIndex]
                };

                validBusStations.Add(busStation);
            }

            context.AddRange(validBusStations);
            context.SaveChanges();
        }
コード例 #3
0
        public void PayTicket(int id, decimal price)
        {
            var account = context.BankAccounts.Where(p => p.CustomerId == id).FirstOrDefault();

            account.Balance -= price;
            context.SaveChanges();
        }
コード例 #4
0
        public static string Execute(string[] data)
        {
            int    customerId     = int.Parse(data[1]);
            double grade          = double.Parse(data[2]);
            string busCompanyName = data[3];
            string content        = string.Join(" ", data.Skip(4).ToArray());

            string result = string.Empty;

            using (var db = new BusTicketContext())
            {
                var customer = db.Customers
                               .Include(x => x.Reviews)
                               .Where(x => x.CustomerId == customerId)
                               .FirstOrDefault();

                var busCompany = db.BusCompanies
                                 .Where(x => x.Name.ToLower() == busCompanyName.ToLower())
                                 .FirstOrDefault();

                if (customer == null)
                {
                    throw new InvalidOperationException("No such user!");
                }

                if (busCompany == null)
                {
                    throw new InvalidOperationException("No such company!");
                }

                if (grade < 1 || grade > 10)
                {
                    throw new InvalidOperationException("Grade must be between 1 and 10");
                }

                var review = new Review()
                {
                    BusCompanyId = busCompany.BusCompanyId,
                    Content      = content,
                    CustomerId   = customerId,
                    Grade        = grade
                };

                customer.Reviews.Add(review);
                db.SaveChanges();

                string fullName = customer.FirstName + " " + customer.LastName;

                StringBuilder sb = new StringBuilder();

                sb.AppendLine($"Customer {fullName} published review for company {busCompanyName}");

                result = sb.ToString().Trim();
            }

            return(result);
        }
コード例 #5
0
        //buy-ticket {customer ID} {Trip ID} {Price} {Seat}
        public static string Execute(string[] data)
        {
            int     customerId = int.Parse(data[1]);
            int     tripId     = int.Parse(data[2]);
            decimal price      = decimal.Parse(data[3]);
            int     seat       = int.Parse(data[4]);

            using (var db = new BusTicketContext())
            {
                var customer = db.Customers.Find(customerId);

                if (customer == null)
                {
                    throw new ArgumentException("No customer with given Id!");
                }

                var trip = db.Trips.Find(tripId);
                if (trip == null)
                {
                    throw new ArgumentException("No trip with given Id!");
                }

                bool isSeatFull = db.Tickets.Any(t => t.TripId == tripId && t.Seat == seat);
                if (isSeatFull)
                {
                    throw new ArgumentException("The seat is already sold! Pick another seat.");
                }

                var customerBankAccount = db.CustomersBankAccounts
                                          .Include(c => c.BankAccount)
                                          .SingleOrDefault(c => c.CustomerId == customerId);

                if (customerBankAccount == null)
                {
                    throw new ArgumentException("The customer has not got a bank account. Cannot buy ticket!");
                }

                if (customerBankAccount.BankAccount.Balance < price)
                {
                    throw new ArgumentException("Insufficient funds!");
                }
                customerBankAccount.BankAccount.Balance -= price;

                var ticket = new Ticket
                {
                    Price    = price,
                    Seat     = seat,
                    TripId   = tripId,
                    Customer = customer
                };
                db.Tickets.Add(ticket);
                db.SaveChanges();

                return("Successfully bought ticket.");
            }
        }
コード例 #6
0
        public static string Execute(string[] data)
        {
            int     customerId = int.Parse(data[1]);
            int     tripId     = int.Parse(data[2]);
            decimal price      = decimal.Parse(data[3]);
            string  seat       = data[4];

            string result = string.Empty;

            using (var db = new BusTicketContext())
            {
                var customer = db.Customers
                               .Include(x => x.BankAccount)
                               .Where(x => x.CustomerId == customerId)
                               .FirstOrDefault();

                if (customer == null)
                {
                    throw new InvalidOperationException($"No such user!");
                }

                if (price <= 0)
                {
                    throw new InvalidCastException("Price cannot be negative or zero.");
                }

                customer.BankAccount.Withdraw(price);

                Ticket ticket = new Ticket()
                {
                    CustomerId = customer.CustomerId,
                    Price      = price,
                    Seat       = seat,
                    TripId     = tripId,
                };

                if (db.Tickets.Any(x => x.CustomerId == customerId && x.TripId == tripId))
                {
                    string ticketUser = customer.FirstName + " " + customer.LastName;
                    throw new InvalidOperationException($"Customer {ticketUser} has ticket for this trip already!");
                }
                customer.Tickets.Add(ticket);
                db.SaveChanges();
                string fullName = customer.FirstName + " " + customer.LastName;

                StringBuilder sb = new StringBuilder();

                sb.AppendLine($"Customer {fullName} bought ticket for trip {tripId} for ${price} on seat {seat}");
                result = sb.ToString().Trim();
            }

            return(result);
        }
コード例 #7
0
        public string ChangeTripStatus(int id, string statusString)
        {
            var    trip      = context.Trips.SingleOrDefault(p => p.Id == id);
            var    oldStatus = trip.Status.ToString();
            Status status;
            string originStationName      = context.BusStations.SingleOrDefault(p => p.Id == trip.OriginStationId).Name;
            string destinationStationName =
                context.BusStations.SingleOrDefault(p => p.Id == trip.DestinationStationId).Name;

            try
            {
                status = Enum.Parse <Status>(statusString);
            }

            catch (Exception ex)
            {
                return("Invalid status");
            }


            trip.Status = status;
            context.SaveChanges();

            if (status == Status.Arrived)
            {
                var arrivedTrip = new ArrivedTrip()
                {
                    ArrivedTime          = DateTime.Now,
                    OriginStationId      = trip.OriginStationId,
                    DestinationStationId = trip.DestinationStationId,
                    PassengersCount      = context.Tickets.Count(p => p.TripId == trip.Id)
                };

                context.ArrivedTrips.Add(arrivedTrip);
                context.SaveChanges();
            }

            return($"Trip from {trip.OriginStation.Name} to {trip.DestinationStation.Name} on {trip.DepartureTime} Status changed from {oldStatus} to {status} ");
        }
コード例 #8
0
        internal string ChangeTripStatus(string[] args)
        {
            int    tripId    = int.Parse(args[0]);
            string newStatus = args[1];

            using (var context = new BusTicketContext())
            {
                var trip = context.Trips
                           .Include(t => t.OriginBusStation)
                           .ThenInclude(obs => obs.Town)
                           .Include(t => t.DestinationBusStation)
                           .ThenInclude(dbs => dbs.Town)
                           .Include(t => t.Tickets)
                           .SingleOrDefault(t => t.Id == tripId);

                if (trip == null)
                {
                    throw new CustomException($"Trip with Id '{tripId}' does not exist!");
                }
                if (trip.Status == Status.Arrived)
                {
                    throw new CustomException("This trip has already arrived!");
                }

                string result = $"Trip from {trip.OriginBusStation.Town.Name} to {trip.DestinationBusStation.Town.Name} " +
                                $"on {trip.DepartureTime}{Environment.NewLine}" +
                                $"Status changed from {trip.Status} to {newStatus}";

                trip.Status = Enum.Parse <Status>(newStatus);


                if (trip.Status == Status.Arrived)
                {
                    var arrivedTrip = new ArrivedTrip
                    {
                        ArrivalTime             = trip.ArrivalTime,
                        PassengersCount         = trip.Tickets.Count(),
                        OriginBusStation        = trip.OriginBusStation,
                        OriginBusStationId      = trip.OriginBusStationId,
                        DestinationBusStation   = trip.DestinationBusStation,
                        DestinationBusStationId = trip.DestinationBusStationId
                    };
                    context.ArrivedTrips.Add(arrivedTrip);
                    context.SaveChanges();

                    result += $"{Environment.NewLine}On {DateTime.Now} - {arrivedTrip.PassengersCount} passengers arrived at " +
                              $"{arrivedTrip.DestinationBusStation.Town.Name} from {arrivedTrip.DestinationBusStation.Town.Name}";
                }
                return(result);
            }
        }
コード例 #9
0
        public static void GenerateCustomers(BusTicketContext context)
        {
            var firstNames = new string[] {
                "Dale",
                "Laura",
                "Dominic",
                "Gordon",
                "Diane",
                "Donna",
                "Audrey",
                "Ben",
                "Marty"
            };

            var lastNames = new string[]
            {
                "Cole",
                "Cooper",
                "Hart",
                "West",
                "Bell",
                "McNulty",
                "Horne",
                "Palmer"
            };

            var validCustomers = new List <Customer>();
            var rnd            = new Random();


            for (int i = 0; i < firstNames.Length; i++)
            {
                var lastNameIndex = rnd.Next(0, lastNames.Length - 1);

                var daysToSubtract = rnd.Next(1000, 10000);

                var customer = new Customer
                {
                    FirstName = firstNames[i],
                    LastName  = lastNames[lastNameIndex],
                    BirthDate = DateTime.Now.AddDays(daysToSubtract * -1),
                    Gender    = (Gender)(i % 3)
                };


                validCustomers.Add(customer);
            }

            context.AddRange(validCustomers);
            context.SaveChanges();
        }
コード例 #10
0
        public void PublishReview(int customerId, double grade, int companyId, string content)
        {
            var review = new Review
            {
                CustomerId       = customerId,
                Grade            = grade,
                BusCompanyId     = companyId,
                Content          = content,
                DateOfPublishing = DateTime.Now
            };


            context.Reviews.Add(review);
            context.SaveChanges();
        }
コード例 #11
0
        //change-trip-status {Trip Id} {New Status}
        public static string Execute(string[] data)
        {
            int    tripId    = int.Parse(data[1]);
            string newStatus = data[2];
            string result    = "";

            using (var db = new BusTicketContext())
            {
                var trip = db.Trips
                           .Include(t => t.OriginBusStation)
                           .Include(t => t.DestinationBusStation)
                           .SingleOrDefault(t => t.TripId == tripId);

                if (trip == null)
                {
                    throw new ArgumentException("No such trip.");
                }

                bool isStatus = Enum.IsDefined(typeof(TripStatus), newStatus);
                if (!isStatus)
                {
                    throw new ArgumentException("Status must be departed,arrived,delayed or cancelled");
                }

                if (trip.Status.ToString() == newStatus)
                {
                    throw new ArgumentException("The current status of the trip is the given one.");
                }
                var oldStatus = trip.Status.ToString();
                trip.Status = Enum.Parse <TripStatus>(newStatus);
                result      = $"Trip from {trip.OriginBusStation.Name} to {trip.DestinationBusStation.Name} change {oldStatus} to {newStatus}";

                if (newStatus == TripStatus.arrived.ToString())
                {
                    var passangers  = db.Tickets.Where(t => t.TripId == tripId).Count();
                    var arrivalTrip = new ArrivedTrip
                    {
                        OriginBusStationName      = trip.OriginBusStation.Name,
                        DestinationBusStationName = trip.DestinationBusStation.Name,
                        PassengerCount            = passangers
                    };
                    db.ArrivalTrips.Add(arrivalTrip);
                    result = result + Environment.NewLine + $"On {DateTime.Now} - {passangers} passengers arrived  at {trip.DestinationBusStation.Name} from {trip.OriginBusStation.Name}";
                }
                db.SaveChanges();
                return(result);
            }
        }
コード例 #12
0
        internal string BuyTicket(string[] args)
        {
            int     customerId = int.Parse(args[0]);
            int     tripId     = int.Parse(args[1]);
            decimal price      = decimal.Parse(args[2]);
            int     seat       = int.Parse(string.Join("", args[3].Skip(1)));

            if (price < 0)
            {
                throw new CustomException("Invalid Price");
            }

            using (var context = new BusTicketContext())
            {
                var customer = context.Customers
                               .Include(c => c.BankAccount)
                               .SingleOrDefault(c => c.Id == customerId);

                if (customer.BankAccount.Balance < price)
                {
                    throw new CustomException($"Isufficient amount of money for customer " +
                                              $"{customer.FullName} with bank account number " +
                                              $"{customer.BankAccount.AccountNumber}");
                }

                var trip   = context.Trips.Find(tripId);
                var ticket = new Ticket
                {
                    Customer   = customer,
                    CustomerId = customer.Id,
                    Trip       = trip,
                    TripId     = trip.Id,
                    Price      = price,
                    Seat       = seat
                };

                context.Tickets.Add(ticket);
                customer.BankAccount.Balance -= price;
                customer.Tickets.ToList().Add(ticket);

                context.SaveChanges();

                return($"Customer {customer.FullName} bought ticket for trip {tripId} for {price:c} on seat {seat}");
            }
        }
コード例 #13
0
        public static void GenerateTowns(BusTicketContext context)
        {
            string[] townNames = new string[]
            {
                "Longdale",
                "StringDale",
                "SpringField",
                "Lodingdon",
                "AppleTown",
                "GeorgeTown",
                "Misty Lake"
            };


            string[] countries = new string[]
            {
                "Zemlemoria",
                "Middle Earch",
                "The green Kingdom",
                "Westeros"
            };


            var validTowns = new List <Town>();
            var rnd        = new Random();

            for (int i = 0; i < townNames.Length; i++)
            {
                int countryIndex = rnd.Next(0, countries.Length - 1);

                var town = new Town()
                {
                    Name    = townNames[i],
                    Country = countries[countryIndex]
                };

                validTowns.Add(town);
            }

            context.AddRange(validTowns);
            context.SaveChanges();
        }
コード例 #14
0
        public static void GenerateReview(BusTicketContext context)
        {
            var content = new string[] {
                "good",
                "bad",
                "very good",
                "very bad",
                "Excellent",
                "Appaling",
                "Shockingly bad"
            };


            var validReviews = new List <Review>();

            var companyIds  = context.BusCompanies.Select(p => p.Id).ToArray();
            var customerIds = context.Customers.Select(p => p.Id).ToArray();

            var rnd = new Random();

            for (int i = 0; i < 5; i++)
            {
                var reviewIndex   = rnd.Next(0, content.Length - 1);
                var companyIndex  = rnd.Next(0, companyIds.Length - 1);
                var customerIndex = rnd.Next(0, customerIds.Length - 1);
                var grade         = rnd.Next(0, 10) / rnd.Next(1, 5);

                var review = new Review
                {
                    Content          = content[reviewIndex],
                    Grade            = grade,
                    CustomerId       = customerIds[customerIndex],
                    DateOfPublishing = DateTime.Now,
                    BusCompanyId     = companyIds[companyIndex]
                };

                validReviews.Add(review);
            }

            context.AddRange(validReviews);
            context.SaveChanges();
        }
コード例 #15
0
        public static void GenerateTickets(BusTicketContext context)
        {
            var seats = new string[] {
                "A1",
                "A2",
                "A3",
                "A4",
                "A5",
                "B1",
                "B2",
                "B3",
                "B4",
                "B5"
            };

            var customerIds = context.Customers.Select(p => p.Id).ToArray();
            var tripIds     = context.Trips.Select(p => p.Id).ToArray();

            var rnd = new Random();

            var validTickets = new List <Ticket>();

            for (int i = 0; i < seats.Length; i++)
            {
                var     customerIndex = rnd.Next(0, customerIds.Length - 1);
                var     tripIndex     = rnd.Next(0, tripIds.Length - 1);
                decimal price         = rnd.Next(10, 20) / (decimal)rnd.Next(1, 5);

                var ticket = new Ticket
                {
                    Seat       = seats[i],
                    CustomerId = customerIds[customerIndex],
                    TripId     = tripIds[tripIndex],
                    Price      = price
                };

                validTickets.Add(ticket);
            }

            context.AddRange(validTickets);
            context.SaveChanges();
        }
コード例 #16
0
        public string BuyTicket(int customerId, int tripId, decimal price, string seat)
        {
            var trip     = context.Trips.SingleOrDefault(p => p.Id == tripId);
            var customer = context.Customers.SingleOrDefault(p => p.Id == customerId);



            var ticket = new Ticket
            {
                Trip     = trip,
                Customer = customer,
                Seat     = seat,
                Price    = price
            };

            context.Tickets.Add(ticket);
            context.SaveChanges();

            return($"Customer {customer.FirstName} {customer.LastName} bought ticket for trip {tripId} for {price} on seat {seat}");
        }
コード例 #17
0
        public static void GenerateCompaines(BusTicketContext context)
        {
            var busCompanyNames = new string[] {
                "UnionIvkoni",
                "UnionDimitrovi",
                "BussesAreUs",
                "DestinationFantasia"
            };


            var nationality = new string[] {
                "Bulgarian",
                "English",
                "French",
                "German",
                "Danish"
            };

            var rnd = new Random();

            var validCompanies = new List <BusCompany>();


            for (int i = 0; i < busCompanyNames.Length; i++)
            {
                var    nationalityIndex = rnd.Next(0, nationality.Length - 1);
                double rating           = rnd.Next(0, 10) / (double)rnd.Next(1, 5);

                var company = new BusCompany
                {
                    Name        = busCompanyNames[i],
                    Nationality = nationality[nationalityIndex],
                    Rating      = rating
                };

                validCompanies.Add(company);
            }

            context.AddRange(validCompanies);
            context.SaveChanges();
        }
コード例 #18
0
        public static void GenerateTrips(BusTicketContext context)
        {
            var rnd = new Random();

            var stationIds = context.BusStations.Select(p => p.Id).ToArray();

            var companyIds = context.BusCompanies.Select(p => p.Id).ToArray();

            var validTrips = new List <Trip>();


            for (int i = 0; i < 10; i++)
            {
                int indexStation     = rnd.Next(0, stationIds.Length - 1);
                int destinationIndex = rnd.Next(0, stationIds.Length - 1);
                while (indexStation == destinationIndex)
                {
                    destinationIndex = rnd.Next(0, stationIds.Length - 1);
                }

                int companyIndex = rnd.Next(0, companyIds.Length - 1);

                int days = rnd.Next(20, 100);

                var trip = new Trip
                {
                    DepartureTime        = DateTime.Now.AddDays(days * -1),
                    ArrivalTime          = DateTime.Now.AddDays(rnd.Next(1, 20)),
                    OriginStationId      = stationIds[indexStation],
                    DestinationStationId = stationIds[destinationIndex],
                    BusCompanyId         = companyIds[companyIndex],
                    Status = (Status)(i % 4)
                };

                validTrips.Add(trip);
            }

            context.AddRange(validTrips);
            context.SaveChanges();
        }
コード例 #19
0
        //publish-review {Customer ID} {Grade} {Bus Company Name} {Content}
        public static string Execute(string[] data)
        {
            int     customerId     = int.Parse(data[1]);
            decimal grade          = decimal.Parse(data[2]);
            string  busCompanyName = data[3];
            string  content        = data.Length == 5 ? data[4] : null;

            using (var db = new BusTicketContext())
            {
                var customer = db.Customers.Find(customerId);

                if (customer == null)
                {
                    throw new ArgumentException("No customer with given Id!");
                }

                var busCompany = db.BusCompanies.FirstOrDefault(b => b.Name == busCompanyName);
                if (busCompany == null)
                {
                    throw new ArgumentException("No such Bus Company");
                }

                if (grade < 1.0m || grade > 10.0m)
                {
                    throw new ArgumentException("Invalid grade value");
                }

                var review = new Review
                {
                    BusCompanyId = busCompany.BusCompanyId,
                    Content      = content,
                    CustomerId   = customerId,
                    Grade        = grade
                };
                db.Reviews.Add(review);
                db.SaveChanges();

                return("Successfully added review.");
            }
        }
コード例 #20
0
        public static void GenerateBankAccounts(BusTicketContext context)
        {
            string[] accountNumbers = new string[]
            {
                "123343473",
                "343255",
                "34354623423",
                "34354234",
                "3434232434",
                "342377684534"
            };

            var customerIds = context.Customers.Select(p => p.Id).ToArray();

            var validBankAccounts = new List <BankAccount>();

            var rnd = new Random();

            foreach (var id in customerIds)
            {
                var     index   = rnd.Next(0, accountNumbers.Length - 1);
                decimal balance = rnd.Next(100, 100000) / (decimal)rnd.Next(1, 20);

                var bankAccount = new BankAccount
                {
                    AccountNumber = accountNumbers[index],
                    CustomerId    = id,
                    Balance       = balance
                };

                validBankAccounts.Add(bankAccount);
            }

            context.AddRange(validBankAccounts);
            context.SaveChanges();
        }
コード例 #21
0
        internal string PublishReview(string[] args)
        {
            int    customerId     = int.Parse(args[0]);
            double grade          = double.Parse(args[1]);
            string busCompanyName = args[2];
            string content        = args[3];

            using (var context = new BusTicketContext())
            {
                var customer = context.Customers
                               .Include(c => c.Reviews)
                               .SingleOrDefault(c => c.Id == customerId);

                var company = context.Companies
                              .FirstOrDefault(c => c.Name == busCompanyName);

                if (company == null)
                {
                    throw new CustomException($"No such company '{busCompanyName}'");
                }

                var review = new Review
                {
                    Customer   = customer,
                    CustomerId = customer.Id,
                    Company    = company,
                    CompanyId  = company.Id,
                    Content    = content,
                    Grade      = grade
                };

                context.Reviews.Add(review);
                context.SaveChanges();
                return($"Customer {customer.FullName} published review for company {busCompanyName}");
            }
        }
コード例 #22
0
        private static void Seed()
        {
            using (var db = new BusTicketContext())
            {
                var busCompanies = new List <BusCompany>
                {
                    new BusCompany {
                        Name = "ETAP", Nationality = "BG"
                    },
                    new BusCompany {
                        Name = "BioMet"
                    },
                    new BusCompany {
                        Name = "UnionIvconi"
                    }
                };
                db.BusCompanies.AddRange(busCompanies);

                var towns = new List <Town>
                {
                    new Town {
                        Name = "Veliko Tyrnovo", Country = "Bulgaria"
                    },
                    new Town {
                        Name = "Sofia", Country = "Bulgaria"
                    },
                    new Town {
                        Name = "Varna", Country = "Bulgaria"
                    },
                    new Town {
                        Name = "Ruse", Country = "Bulgaria"
                    },
                    new Town {
                        Name = "Paris", Country = "France"
                    },
                    new Town {
                        Name = "London", Country = "England"
                    },
                    new Town {
                        Name = "Plovdiv", Country = "Bulgaria"
                    }
                };
                db.Towns.AddRange(towns);

                db.SaveChanges();

                var busStations = new List <BusStation>
                {
                    new BusStation {
                        Name = "Avtogara Iug", TownId = 1
                    },
                    new BusStation {
                        Name = "Avtogara Sever", TownId = 1
                    },
                    new BusStation {
                        Name = "Central Railway Station", TownId = 2
                    },
                    new BusStation {
                        Name = "Hotel Pliska", TownId = 2
                    },
                    new BusStation {
                        Name = "SeaViewStop", TownId = 3
                    },
                };
                db.BusStations.AddRange(busStations);

                db.SaveChanges();

                var trips = new List <Trip>
                {
                    new Trip {
                        DepartureTime           = DateTime.Parse("30-11-2017 07:30"),
                        Status                  = TripStatus.arrived,
                        OriginBusStationId      = 1,
                        DestinationBusStationId = 3,
                        BusCompanyId            = 1
                    },

                    new Trip {
                        DepartureTime           = DateTime.Parse("28-11-2017 12:30"),
                        Status                  = TripStatus.delayed,
                        OriginBusStationId      = 3,
                        DestinationBusStationId = 5,
                        BusCompanyId            = 3
                    },

                    new Trip {
                        DepartureTime           = DateTime.Parse("30-11-2017 15:00"),
                        Status                  = TripStatus.departed,
                        OriginBusStationId      = 4,
                        DestinationBusStationId = 3,
                        BusCompanyId            = 2
                    },

                    new Trip {
                        DepartureTime           = DateTime.Parse("12-12-2017 08:30"),
                        Status                  = TripStatus.cancelled,
                        OriginBusStationId      = 1,
                        DestinationBusStationId = 4,
                        BusCompanyId            = 3
                    },

                    new Trip {
                        DepartureTime           = DateTime.Parse("30-11-2017 15:30"),
                        Status                  = TripStatus.arrived,
                        OriginBusStationId      = 5,
                        DestinationBusStationId = 2,
                        BusCompanyId            = 1
                    }
                };
                db.Trips.AddRange(trips);

                var customers = new List <Customer>
                {
                    new Customer {
                        FirstName = "Pesho", LastName = "Petkov", Gender = Gender.male, HomeTownId = 1
                    },
                    new Customer {
                        FirstName = "Ivan", LastName = "Ivanov", HomeTownId = 2
                    },
                    new Customer {
                        FirstName = "Merry", LastName = "Santa", Gender = Gender.female, HomeTownId = 3
                    },
                    new Customer {
                        FirstName = "Katq", LastName = "Georgieva", Gender = Gender.notSpecified, HomeTownId = 4
                    },
                    new Customer {
                        FirstName = "Gosho", LastName = "Goshov", Gender = Gender.male, HomeTownId = 5
                    },
                };
                db.Customers.AddRange(customers);

                var bankAccs = new List <BankAccount>
                {
                    new BankAccount {
                        AccountNumber = "123BB32", Balance = 56.3m
                    },
                    new BankAccount {
                        AccountNumber = "9856TY0", Balance = 560m
                    },
                    new BankAccount {
                        AccountNumber = "2569KT6", Balance = 15000m
                    },
                    new BankAccount {
                        AccountNumber = "568KG65", Balance = 1500m
                    },
                    new BankAccount {
                        AccountNumber = "567GG32", Balance = 150m
                    }
                };
                db.BankAccounts.AddRange(bankAccs);

                db.SaveChanges();

                var tickets = new List <Ticket>
                {
                    new Ticket {
                        Price = 20m, Seat = 7, CustomerId = 1, TripId = 2
                    },
                    new Ticket {
                        Price = 16m, Seat = 42, CustomerId = 2, TripId = 2
                    },
                    new Ticket {
                        Price = 21m, Seat = 14, CustomerId = 5, TripId = 3
                    },
                    new Ticket {
                        Price = 15m, Seat = 4, CustomerId = 1, TripId = 4
                    }
                };
                db.Tickets.AddRange(tickets);

                var reviews = new List <Review>
                {
                    new Review {
                        Grade = 5.6m, BusCompanyId = 1, CustomerId = 1
                    },
                    new Review {
                        Grade = 10.0m, BusCompanyId = 1, CustomerId = 2
                    },
                    new Review {
                        Grade = 8.6m, BusCompanyId = 2, CustomerId = 3
                    },
                    new Review {
                        Grade = 9.0m, BusCompanyId = 3, CustomerId = 3
                    },
                    new Review {
                        Grade = 5.0m, BusCompanyId = 2, CustomerId = 4
                    },
                    new Review {
                        Grade = 8.9m, BusCompanyId = 1, CustomerId = 5
                    },
                };
                db.Reviews.AddRange(reviews);

                var customerBankAccs = new List <CustomerBankAcc>
                {
                    new CustomerBankAcc {
                        CustomerId = 1, BankAccountId = 1
                    },
                    new CustomerBankAcc {
                        CustomerId = 2, BankAccountId = 4
                    },
                    new CustomerBankAcc {
                        CustomerId = 3, BankAccountId = 2
                    },
                    new CustomerBankAcc {
                        CustomerId = 4, BankAccountId = 3
                    },
                    new CustomerBankAcc {
                        CustomerId = 5, BankAccountId = 5
                    },
                };
                db.CustomersBankAccounts.AddRange(customerBankAccs);

                db.SaveChanges();
            }
        }
コード例 #23
0
        private static void Seed(BusTicketContext context)
        {
            var towns = new List <Town>()
            {
                new Town()
                {
                    Name    = "Kyustendil",
                    Country = "Bulgaria",
                },
                new Town()
                {
                    Name    = "Sofia",
                    Country = "Bulgaria",
                },
                new Town()
                {
                    Name    = "Blagoevgrad",
                    Country = "Bulgaria",
                },
                new Town()
                {
                    Name    = "Burgas",
                    Country = "Bulgaria",
                },
                new Town()
                {
                    Name    = "Ruse",
                    Country = "Bulgaria",
                },
            };

            context.AddRange(towns);
            context.SaveChanges();

            var customers = new List <Customer>()
            {
                new Customer()
                {
                    FirstName  = "Cool1",
                    LastName   = "Guy1",
                    HomeTownId = 1,
                    Gender     = CustomerGender.Male
                },
                new Customer()
                {
                    FirstName  = "Cool1",
                    LastName   = "Girl1",
                    HomeTownId = 1,
                    Gender     = CustomerGender.Female
                },
                new Customer()
                {
                    FirstName  = "Cool2",
                    LastName   = "Guy2",
                    HomeTownId = 2,
                    Gender     = CustomerGender.Male
                },
                new Customer()
                {
                    FirstName  = "Cool2",
                    LastName   = "Girl2",
                    HomeTownId = 2,
                    Gender     = CustomerGender.Female
                },
                new Customer()
                {
                    FirstName  = "Cool3",
                    LastName   = "Unknown3",
                    HomeTownId = 3,
                    Gender     = CustomerGender.NotSpecified
                },
            };

            context.Customers.AddRange(customers);
            context.SaveChanges();

            var bankAccounts = new List <BankAccount>()
            {
                new BankAccount()
                {
                    AccountNumber = "Us1",
                    Balance       = 100,
                    CustomerId    = 1
                },
                new BankAccount()
                {
                    AccountNumber = "Us2",
                    Balance       = 200,
                    CustomerId    = 2
                },
                new BankAccount()
                {
                    AccountNumber = "Us3",
                    Balance       = 300,
                    CustomerId    = 3
                },
                new BankAccount()
                {
                    AccountNumber = "Us4",
                    Balance       = 400,
                    CustomerId    = 4
                },
                new BankAccount()
                {
                    AccountNumber = "Us5",
                    Balance       = 500,
                    CustomerId    = 5
                },
            };

            context.BankAccounts.AddRange(bankAccounts);
            context.SaveChanges();

            var reviews = new List <Review>()
            {
                new Review()
                {
                    BusCompanyId = 1,
                    Content      = "It was not bad trip",
                    CustomerId   = 1,
                    Grade        = 5.5
                },
                new Review()
                {
                    BusCompanyId = 1,
                    Content      = "It was a bad trip",
                    CustomerId   = 1,
                    Grade        = 2.0
                },
                new Review()
                {
                    BusCompanyId = 1,
                    Content      = "It was very bad trip",
                    CustomerId   = 2,
                    Grade        = 1.5
                },
                new Review()
                {
                    BusCompanyId = 2,
                    Content      = "It was awesome trip",
                    CustomerId   = 2,
                    Grade        = 8.5
                },
                new Review()
                {
                    BusCompanyId = 3,
                    Content      = "It was average trip",
                    CustomerId   = 3,
                    Grade        = 5.5
                },
            };

            context.Reviews.AddRange(reviews);

            var busCompanies = new List <BusCompany>()
            {
                new BusCompany()
                {
                    Name        = "Union",
                    Nationality = "BG",
                },
                new BusCompany()
                {
                    Name        = "Datsi",
                    Nationality = "BG"
                },
                new BusCompany()
                {
                    Name        = "RussianBuses",
                    Nationality = "RS"
                },
                new BusCompany()
                {
                    Name        = "Vodka",
                    Nationality = "RS"
                },
                new BusCompany()
                {
                    Name        = "UnitedLines",
                    Nationality = "US"
                },
            };

            context.BusCompanies.AddRange(busCompanies);
            context.SaveChanges();

            var busComsForRating = context.BusCompanies
                                   .Include(x => x.Reviews)
                                   .ToList();

            foreach (var company in busComsForRating)
            {
                double rating = 0.0d;
                if (company.Reviews.Count > 0)
                {
                    rating = company.Reviews.Average(x => x.Grade);

                    company.Rating = rating;
                }
                else
                {
                    company.Rating = 0.0d;
                }
            }
            context.SaveChanges();

            var busStations = new List <BusStation>()
            {
                new BusStation()
                {
                    Name   = "Kyustendil Centre",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Kyustendil 2",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Sofia Centre",
                    TownId = 2
                },
                new BusStation()
                {
                    Name   = "Sofia 2",
                    TownId = 2
                },
                new BusStation()
                {
                    Name   = "Blagoevgrad Centre",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Blagoevgrad 2",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Burgas Centre",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Burgas 2",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Ruse Centre",
                    TownId = 1
                },
                new BusStation()
                {
                    Name   = "Ruse 2",
                    TownId = 1
                },
            };

            context.BusStations.AddRange(busStations);
            context.SaveChanges();

            var trips = new List <Trip>()
            {
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 08:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 09:00"),
                    BusCompanyId            = 1,
                    OriginBusStationId      = 1,
                    DestinationBusStationId = 4,
                    Status = TripStatus.Arrived
                },
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 08:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 09:00"),
                    BusCompanyId            = 1,
                    OriginBusStationId      = 2,
                    DestinationBusStationId = 1,
                    Status = TripStatus.Arrived
                },
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 09:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 10:00"),
                    BusCompanyId            = 2,
                    OriginBusStationId      = 2,
                    DestinationBusStationId = 4,
                    Status = TripStatus.Delayed
                },
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 09:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 10:00"),
                    BusCompanyId            = 2,
                    OriginBusStationId      = 5,
                    DestinationBusStationId = 1,
                    Status = TripStatus.Delayed
                },
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 10:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 11:00"),
                    BusCompanyId            = 3,
                    OriginBusStationId      = 1,
                    DestinationBusStationId = 7,
                    Status = TripStatus.Cancelled
                },
                new Trip()
                {
                    DepartureTime           = DateTime.Parse("12-10-2017 10:00"),
                    ArrivalTime             = DateTime.Parse("12-10-2017 11:00"),
                    BusCompanyId            = 3,
                    OriginBusStationId      = 7,
                    DestinationBusStationId = 3,
                    Status = TripStatus.Cancelled
                },
            };

            context.Trips.AddRange(trips);
            context.SaveChanges();

            var tickets = new List <Ticket>()
            {
                new Ticket()
                {
                    CustomerId = 1,
                    Price      = 10.00m,
                    Seat       = "A1",
                    TripId     = 1
                },
                new Ticket()
                {
                    CustomerId = 2,
                    Price      = 10.00m,
                    Seat       = "A2",
                    TripId     = 1
                },
                new Ticket()
                {
                    CustomerId = 3,
                    Price      = 10.00m,
                    Seat       = "B1",
                    TripId     = 2
                },
                new Ticket()
                {
                    CustomerId = 4,
                    Price      = 10.00m,
                    Seat       = "B2",
                    TripId     = 2
                },
                new Ticket()
                {
                    CustomerId = 5,
                    Price      = 10.00m,
                    Seat       = "C5",
                    TripId     = 3
                },
            };

            context.Tickets.AddRange(tickets);
            context.SaveChanges();
        }
コード例 #24
0
        public static string Execute(string[] data)
        {
            int    tripId    = int.Parse(data[1]);
            string newStatus = data[2].ToLower();

            string result = string.Empty;

            using (var db = new BusTicketContext())
            {
                var trip = db.Trips
                           .Include(x => x.OriginBusStation)
                           .ThenInclude(x => x.Town)
                           .Include(x => x.DestinationBusStation)
                           .ThenInclude(x => x.Town)
                           .Include(x => x.Tickets)
                           .Where(x => x.TripId == tripId)
                           .FirstOrDefault();

                if (trip == null)
                {
                    throw new InvalidOperationException("No such trip.");
                }

                if (trip.Status.ToString().ToLower() == newStatus.ToLower())
                {
                    throw new InvalidOperationException("The new status must be different from the old one!");
                }

                string currentStatus = trip.Status.ToString();

                switch (newStatus)
                {
                case "departed":
                    trip.Status = TripStatus.Departed;
                    break;

                case "arrived":
                    trip.Status = TripStatus.Arrived;
                    break;

                case "delayed":
                    trip.Status = TripStatus.Delayed;
                    break;

                case "cancelled":
                    trip.Status = TripStatus.Cancelled;
                    break;

                default:
                    break;
                }
                db.SaveChanges();

                string outputStatus = trip.Status.ToString();

                if (newStatus == "arrived")
                {
                    var arrivedTrip = new ArrivedTrip()
                    {
                        ActualArriveTime = trip.ArrivalTime.AddMinutes(25),
                        ArriveDestinationBusStationId = trip.DestinationBusStationId,
                        ArriveOriginBusStationId      = trip.OriginBusStationId,
                        PassengersCount = trip.Tickets.Count()
                    };
                    db.ArrivedTrips.Add(arrivedTrip);
                    db.SaveChanges();

                    StringBuilder sb = new StringBuilder();

                    string actualTime = arrivedTrip.ActualArriveTime.ToString(@"yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);

                    sb.AppendLine($"Trip from {trip.OriginBusStation.Town.Name} to {trip.DestinationBusStation.Town.Name} on {trip.DepartureTime}");
                    sb.AppendLine($"Status changed from {currentStatus} to {outputStatus}");
                    sb.AppendLine($"On {actualTime} - {arrivedTrip.PassengersCount} passengers arrived at {trip.DestinationBusStation.Town.Name} from {trip.OriginBusStation.Town.Name}");
                    result = sb.ToString().Trim();
                }
                else
                {
                    StringBuilder sb = new StringBuilder();

                    sb.AppendLine($"Trip from {trip.OriginBusStation.Town.Name} to {trip.DestinationBusStation.Town.Name} on {trip.DepartureTime}");
                    sb.AppendLine($"Status changed from {currentStatus} to {outputStatus}");
                    result = sb.ToString().Trim();
                }
            }

            return(result);
        }