public string Execute(BusTicketsSystemContext db, string[] data)
        {
            int stationId = int.Parse(data[0]);

            var station = db.BusStations.Find(stationId);

            if (station == null)
            {
                return($"Station# {stationId} do not exist!");
            }

            var sb = new StringBuilder();

            sb.AppendLine($"Station: {station.Name}, {station.Town.Name}");
            sb.AppendLine($"Arrives:");
            foreach (var arrivinTrip in station.DestinationStationTrips)
            {
                sb.AppendLine($"From: {arrivinTrip.OriginStation.Town.Name} | Arrive at: {arrivinTrip.ArrivalTime} | Status: {arrivinTrip.Status.ToString()}");
            }
            sb.AppendLine("Departures:");
            foreach (var departTrip in station.OriginStationTrips)
            {
                sb.AppendLine($"To: {departTrip.DestinationStation.Town.Name} | Depert at: {departTrip.DepertureTime} |  Status: {departTrip.Status.ToString()}");
            }

            return(sb.ToString().TrimEnd());
        }
Example #2
0
 private static void RestartDatabase()
 {
     using (var db = new BusTicketsSystemContext())
     {
         db.Database.EnsureDeleted();
         db.Database.EnsureCreated();
     }
 }
Example #3
0
 private static void InitialDbSeed()
 {
     using (var db = new BusTicketsSystemContext())
     {
         var initialSeed = new InitialDbSeed();
         initialSeed.Seed(db);
     }
 }
Example #4
0
        public string Execute(BusTicketsSystemContext db, string[] data)
        {
            var customerId  = int.Parse(data[0]);
            var grade       = float.Parse(data[1]);
            var busCompName = data[2];
            var content     = data[3];

            var customer = db.Customers
                           .FirstOrDefault(x => x.Id == customerId);

            if (customer == null)
            {
                throw new ArgumentException($"Customer with Id: {customerId} was not found!");
            }

            var busCompany = db.BusCompanies
                             .FirstOrDefault(x => x.Name == busCompName);

            if (busCompany == null)
            {
                throw new ArgumentException($"Company: {busCompName} was not found!");
            }

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

            string lastName = customer.LastName;

            if (lastName.EndsWith("s"))
            {
                lastName += "'";
            }
            else
            {
                lastName += "'s";
            }

            return($"{customer.FirstName} {lastName} review was successfully published");
        }
        public string Execute(BusTicketsSystemContext db, string[] data)
        {
            int     customreId = int.Parse(data[0]);
            int     tripId     = int.Parse(data[1]);
            decimal price      = decimal.Parse(data[2]);
            int     seatNumber = int.Parse(data[3]);

            var customer = db.Customers.Find(customreId);

            if (customer == null)
            {
                throw new ArgumentException($"Customer with ID: {customreId} do not exist in database!");
            }

            var trip = db.Trips.Find(tripId);

            if (trip == null)
            {
                throw new ArgumentException($"Trip with ID: {tripId} was not found!");
            }

            if (price <= 0)
            {
                throw new ArgumentException("Invalid price");
            }

            var customerBalance = customer.BankAccount.Balance;

            if (customerBalance < price)
            {
                throw new InvalidOperationException($"Insufficient amount of money for customer {customer.FirstName} {customer.LastName} with bank account number {customer.BankAccount.AccountNumber}");
            }

            customer.BankAccount.Balance = -price;
            customer.Tickets.Add(new Ticket {
                Price = price, Seat = seatNumber, CustomerId = customreId, TripId = tripId
            });
            db.SaveChanges();

            return($"Customer {customer.FirstName} {customer.LastName} bought ticket for trip {tripId} for {price:F2} on seat {seatNumber}");
        }
Example #6
0
        public string DispatchCommand(string command, string[] data)
        {
            var commandToExecute = Assembly.GetExecutingAssembly()
                                   .GetTypes()
                                   .FirstOrDefault(x => x.Name.Equals(command + "Command", StringComparison.InvariantCultureIgnoreCase));

            if (commandToExecute == null)
            {
                throw new InvalidOperationException($"Command: {command} is not implemented yet!");
            }

            var    instance = (ICommand)Activator.CreateInstance(commandToExecute);
            string result   = null;

            using (var db = new BusTicketsSystemContext())
            {
                result = instance.Execute(db, data);
            }

            return(result);
        }
        public string Execute(BusTicketsSystemContext db, string[] data)
        {
            var companyId = int.Parse(data[0]);
            var company   = db.BusCompanies
                            .FirstOrDefault(x => x.Id == companyId);

            if (company == null)
            {
                throw new ArgumentException($"Company {companyId} was not found!");
            }

            var sb = new StringBuilder();

            foreach (var review in company.Reviews)
            {
                sb.AppendLine($"{review.Id} {review.Grade:F2} {review.PublishingDatetime}");
                sb.AppendLine($"{review.Customer.FirstName} {review.Customer.LastName}");
                sb.AppendLine($"{review.Content}");
            }

            return(sb.ToString().TrimEnd());
        }
 public BankAccountService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
Example #9
0
 public CustomerService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
 public BusStationService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
        public void Seed(BusTicketsSystemContext db)
        {
            var towns = new List <Town>()
            {
                new Town {
                    Name = "Sofia", Country = "Bulgaria"
                },
                new Town {
                    Name = "Varna", Country = "Bulgaria"
                },
                new Town {
                    Name = "Burgas", Country = "Bulgaria"
                },
                new Town {
                    Name = "Plovdiv", Country = "Bulgaria"
                },
                new Town {
                    Name = "Vraca", Country = "Bulgaria"
                },
                new Town {
                    Name = "Pernik", Country = "Bulgaria"
                },
                new Town {
                    Name = "Bansko", Country = "Bulgaria"
                }
            };

            foreach (var town in towns)
            {
                db.Towns.Add(town);
            }
            db.SaveChanges();

            var busStations = new List <BusStation>()
            {
                new BusStation {
                    Name = "Central Station", TownId = 1
                },
                new BusStation {
                    Name = "Only Station", TownId = 2
                },
                new BusStation {
                    Name = "North Station", TownId = 1
                },
                new BusStation {
                    Name = "No Station At All", TownId = 3
                },
                new BusStation {
                    Name = "South Station", TownId = 1
                }
            };

            foreach (var station in busStations)
            {
                db.BusStations.Add(station);
            }
            db.SaveChanges();

            var accounts = new List <BankAccount>()
            {
                new BankAccount {
                    AccountNumber = "3456456854", Balance = 5000
                },
                new BankAccount {
                    AccountNumber = "568926232", Balance = 200
                },
                new BankAccount {
                    AccountNumber = "4621122324"
                },
                new BankAccount {
                    AccountNumber = "659532413", Balance = 2000
                },
                new BankAccount {
                    AccountNumber = "235643232", Balance = 160
                }
            };

            foreach (var account in accounts)
            {
                db.BankAccounts.Add(account);
            }
            db.SaveChanges();

            var customers = new List <Customer>()
            {
                new Customer {
                    BankAccountId = 1, FirstName = "Ivan", LastName = "Ivanov", Gender = "Male", HomeTownId = 2
                },
                new Customer {
                    BankAccountId = 2, FirstName = "Petkan", LastName = "Petkov", Gender = "Male", HomeTownId = 2
                },
                new Customer {
                    BankAccountId = 3, FirstName = "Martin", LastName = "Martinov", Gender = "Male", HomeTownId = 1
                },
                new Customer {
                    BankAccountId = 4, FirstName = "Ivana", LastName = "Ivanova", Gender = "Female", HomeTownId = 3
                },
                new Customer {
                    BankAccountId = 5, FirstName = "Marina", LastName = "Marinova", Gender = "Female", HomeTownId = 4
                }
            };

            foreach (var customer in customers)
            {
                db.Customers.Add(customer);
            }
            db.SaveChanges();

            var companies = new List <BusCompany>()
            {
                new BusCompany {
                    Name = "FineTravels"
                },
                new BusCompany {
                    Name = "Don'tLookBack"
                },
                new BusCompany {
                    Name = "FlyAway"
                },
                new BusCompany {
                    Name = "FastAndFurious"
                },
                new BusCompany {
                    Name = "LateButSave"
                }
            };

            foreach (var company in companies)
            {
                db.BusCompanies.Add(company);
            }
            db.SaveChanges();

            var trips = new List <Trip>()
            {
                new Trip {
                    BusCompanyId = 5, DestinationStationId = 2, OriginStationId = 4, ArrivalTime = "13:56", DepertureTime = "22:16", Status = TripStatus.Arrived
                },
                new Trip {
                    BusCompanyId = 4, DestinationStationId = 5, OriginStationId = 2, ArrivalTime = "3:56", DepertureTime = "13:16", Status = TripStatus.Cancelled
                },
                new Trip {
                    BusCompanyId = 3, DestinationStationId = 2, OriginStationId = 5, ArrivalTime = "13:56", DepertureTime = "22:16", Status = TripStatus.Delayed
                },
                new Trip {
                    BusCompanyId = 2, DestinationStationId = 3, OriginStationId = 5, ArrivalTime = "11:56", DepertureTime = "15:06", Status = TripStatus.Departed
                },
                new Trip {
                    BusCompanyId = 1, DestinationStationId = 2, OriginStationId = 5, ArrivalTime = "12:56", DepertureTime = "20:16", Status = TripStatus.Cancelled
                },
                new Trip {
                    BusCompanyId = 3, DestinationStationId = 4, OriginStationId = 2, ArrivalTime = "6:56", DepertureTime = "23:16", Status = TripStatus.Delayed
                },
                new Trip {
                    BusCompanyId = 2, DestinationStationId = 2, OriginStationId = 1, ArrivalTime = "13:56", DepertureTime = "12:16", Status = TripStatus.Arrived
                },
                new Trip {
                    BusCompanyId = 4, DestinationStationId = 5, OriginStationId = 3, ArrivalTime = "15:06", DepertureTime = "2:16", Status = TripStatus.Departed
                }
            };

            foreach (var trip in trips)
            {
                db.Trips.Add(trip);
            }
            db.SaveChanges();

            var tickets = new List <Ticket>()
            {
                new Ticket {
                    TripId = 1, CustomerId = 2, Price = 98.76m, Seat = 2
                },
                new Ticket {
                    TripId = 2, CustomerId = 1, Price = 34, Seat = 1
                },
                new Ticket {
                    TripId = 3, CustomerId = 3, Price = 9.76m, Seat = 3
                },
                new Ticket {
                    TripId = 4, CustomerId = 2, Price = 89.76m, Seat = 1
                },
                new Ticket {
                    TripId = 1, CustomerId = 1, Price = 58.76m, Seat = 1
                },
                new Ticket {
                    TripId = 2, CustomerId = 5, Price = 9.76m, Seat = 1
                },
                new Ticket {
                    TripId = 1, CustomerId = 4, Price = 34.76m, Seat = 2
                },
                new Ticket {
                    TripId = 3, CustomerId = 2, Price = 23.76m, Seat = 4
                },
                new Ticket {
                    TripId = 4, CustomerId = 3, Price = 16.76m, Seat = 1
                }
            };

            foreach (var ticket in tickets)
            {
                db.Tickets.Add(ticket);
            }
            db.SaveChanges();

            var reviews = new List <Review>()
            {
                new Review {
                    CustomerId = 1, Grade = 2.3f, BusCompanyId = 2, Content = "Auful trip"
                },
                new Review {
                    CustomerId = 2, Grade = 6.3f, BusCompanyId = 2, Content = "Good trip"
                },
                new Review {
                    CustomerId = 1, Grade = 8.3f, BusCompanyId = 2, Content = "GREATTTTT trip"
                },
                new Review {
                    CustomerId = 3, Grade = 2.9f, BusCompanyId = 1, Content = "blah blah"
                },
                new Review {
                    CustomerId = 4, Grade = 9.3f, BusCompanyId = 1, Content = "Nice trip"
                },
                new Review {
                    CustomerId = 1, Grade = 8.3f, BusCompanyId = 2, Content = "Perfect trip"
                },
                new Review {
                    CustomerId = 5, Grade = 6.3f, BusCompanyId = 4, Content = "Last trip"
                },
                new Review {
                    CustomerId = 1, Grade = 9.3f, BusCompanyId = 2, Content = "I reccomend this trip"
                },
                new Review {
                    CustomerId = 2, Grade = 5.3f, BusCompanyId = 3, Content = "It was OK"
                }
            };

            foreach (var review in reviews)
            {
                db.Reviews.Add(review);
            }
            db.SaveChanges();
        }
Example #12
0
 public TownService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
 public ReviewService(BusTicketsSystemContext context, ICustomerService customerService, IBusCompanyService busCompanyService)
 {
     this.context           = context;
     this.customerService   = customerService;
     this.busCompanyService = busCompanyService;
 }
Example #14
0
 public DatabaseService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
Example #15
0
 public UnitOfWork()
 {
     this.context = new BusTicketsSystemContext();
 }
Example #16
0
 public Repository(BusTicketsSystemContext context)
 {
     this.context = context;
 }
 public DatabaseInitializerService(BusTicketsSystemContext context) => this.context = context;
 public BusCompanyService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
 public BusCompanyService(BusTicketsSystemContext context)
 {
     this.context = context;
 }
 public CustomerService(BusTicketsSystemContext context)
 {
     this.context = context;
 }
Example #21
0
 public TicketService(BusTicketsSystemContext context, ICustomerService customerService, ITripService tripService)
 {
     this.context         = context;
     this.customerService = customerService;
     this.tripService     = tripService;
 }
Example #22
0
 public ReviewService(BusTicketsSystemContext dbContext)
 {
     this.dbContext = dbContext;
 }
Example #23
0
 public TripService(BusTicketsSystemContext context)
 {
     this.context = context;
 }
Example #24
0
 public BusStationService(BusTicketsSystemContext context)
 {
     this.context = context;
 }