Example #1
0
        public List <VoyageViewModel> SearchVoyages(SearchVoyageViewModel searchVoyage)
        {
            var result = new List <VoyageViewModel>();
            var d      = DateTime.Parse(searchVoyage.Date);
            var d1     = d.AddDays(1);

            using (var DB = new BusTicketsContext())
            {
                result = DB.Voyages.Where(x => x.DepartureStopId == searchVoyage.DepartureId &&
                                          x.ArivalStopId == searchVoyage.ArriveId &&
                                          x.DepartureDateTime > d && x.DepartureDateTime < d1)
                         .Select(x => new VoyageViewModel
                {
                    Id            = x.Id,
                    Name          = x.VoyageName,
                    Number        = x.VoyageNumber,
                    TravelTime    = x.TravelTime.ToString(),
                    Departure     = x.DepartureDateTime.ToString(),
                    Arrival       = x.ArrivalDateTime.ToString(),
                    NumberSeats   = x.NumberOfSeets,
                    OneTicketCost = x.TicketCost
                }).ToList();
            }

            return(result);
        }
Example #2
0
        public List <StopInfoViewModel> GetStopInfo(int id)
        {
            var stopInfo = new List <StopInfoViewModel>();

            using (var DB = new BusTicketsContext())
            {
                var stopArrival = DB.Voyages.Where(x => x.ArivalStopId == id)
                                  .Select(x => new StopInfoViewModel
                {
                    Name    = x.VoyageName,
                    Number  = x.VoyageNumber,
                    Arrival = x.ArrivalDateTime.ToString()
                }).ToList();

                var stopDeparture = DB.Voyages.Where(x => x.DepartureStopId == id)
                                    .Select(x => new StopInfoViewModel
                {
                    Name      = x.VoyageName,
                    Number    = x.VoyageNumber,
                    Departure = x.DepartureDateTime.ToString()
                }).ToList();

                stopInfo.AddRange(stopArrival);
                stopInfo.AddRange(stopDeparture);
            }

            return(stopInfo);
        }
        // print-reviews {Bus Company ID}
        public override string Execute(BusTicketsContext context)
        {
            if (this.CmdArgs.Length != 1)
            {
                throw new ArgumentException(InvalidCommandArgsExceptionMessage);
            }

            var companyId = int.Parse(this.CmdArgs[0]);
            var company   = context.BusCompanies
                            .Select(c => new
            {
                c.Id,
                Reviews = c.Reviews
                          .Select(r => string.Concat($"{r.Id} {r.Grade} {r.PublishDate.ToString("hh:mm dd-MM-yyyy")}{Environment.NewLine}",
                                                     $"{r.Customer.FirstName} {r.Customer.LastName}{Environment.NewLine}",
                                                     r.Content))
            })
                            .SingleOrDefault(c => c.Id == companyId);

            if (company == null)
            {
                throw new ArgumentException(string.Format(CompanyNotFoundExceptionMessage, companyId));
            }

            return(company.Reviews == null
                ? "There is not reviews for this company"
                : "   - " + string.Join($"{Environment.NewLine}   - ", company.Reviews));
        }
        //{Bus Company ID}
        public string Execute(IList <string> data)
        {
            var companyId = int.Parse(data[0]);

            using (var db = new BusTicketsContext())
            {
                if (!db.Companies.Any(c => c.Id == companyId))
                {
                    throw new ArgumentException("No such company");
                }

                var reviews = db.Reviews
                              .AsNoTracking()
                              .Include(r => r.Customer)
                              .Where(r => r.CompanyId == companyId);

                var sb = new StringBuilder();
                foreach (var r in reviews)
                {
                    sb.AppendLine(
                        $"{r.Id} {r.Grade} {r.PublishedOn.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture)} " +
                        $"{r.Customer.FirstName} {r.Customer.LastName} {r.Content}");
                }

                return(sb.ToString().Trim());
            }
        }
        public static void Seed(BusTicketsContext context)
        {
            Town[] towns = TownGenerator.GenerateTowns();
            context.Towns.AddRange(towns);

            BusCompany[] companies = BusCompanyGenerator.GenerateBusCompanies();
            context.BusCompanies.AddRange(companies);

            BusStation[] stations = BusStationGenerator.GenerateBusStations(towns);
            context.BusStations.AddRange(stations);

            Customer[] customers = CustomerGenerator.GenerateCustomers(towns);
            context.Customers.AddRange(customers);

            BankAccount[] accounts = BankAccountGenerator.GenerateBankAccounts(customers);
            context.BankAccounts.AddRange(accounts);

            //BusCompany[] companiesFromDb = context.BusCompanies.ToArray();
            Review[] reviews = ReviewGenerator.GenerateReviews(companies, customers);
            context.Reviews.AddRange(reviews);

            Trip[] trips = TripGenerator.GenerateTrips(stations, companies);
            context.Trips.AddRange(trips);

            Ticket[] tickets = TicketGenerator.GenerateTicket(customers, trips);
            context.Tickets.AddRange(tickets);

            context.SaveChanges();

            Console.WriteLine("Sample data inserted successfully.");
        }
Example #6
0
 public void UpdateOrder(int id, string status)
 {
     using (var DB = new BusTicketsContext())
     {
         var order = DB.Orders.Find(id);
         order.Status = status;
         DB.SaveChanges();
     }
 }
Example #7
0
 public static void Main()
 {
     using (var context = new BusTicketsContext())
     {
         var commandDispatcher = new CommandDispatcher <ICommand>();
         var engine            = new Engine(context, commandDispatcher);
         engine.Run();
     }
 }
Example #8
0
 public void AddStop(StopViewModel stop)
 {
     using (var DB = new BusTicketsContext())
     {
         var addStop = DB.Stops.Add(new Stop {
             Name = stop.Name, Description = stop.Description, Status = stop.Status
         });
         DB.SaveChanges();
     }
 }
Example #9
0
 public void Buy(int id)
 {
     using (var DB = new BusTicketsContext())
     {
         var order = DB.Orders.Find(id);
         order.Status = "bought Out";
         var ticket = DB.Tickets.First(x => x.OrderId == id);
         ticket.Status = "bought Out";
         DB.SaveChanges();
     }
 }
Example #10
0
        public static void Main()
        {
            using (var db = new BusTicketsContext())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
                SeedDb(db);
            }

            var engine = new Engine();

            engine.Run();
        }
        public static void Main(string[] args)
        {
            IServiceProvider serviceProvider = ConfigureServices();

            IDatabaseService   databaseService   = serviceProvider.GetService <IDatabaseService>();
            ICommandDispatcher commandDispatcher = serviceProvider.GetService <ICommandDispatcher>();
            Engine             engine            = new Engine(databaseService, commandDispatcher);

            using (var context = new BusTicketsContext())
            {
                DbInitializer.Seed(context);
            }
            engine.Run();
        }
Example #12
0
        public List <StopViewModel> GetStops(string name)
        {
            var stops = new List <StopViewModel>();

            using (var DB = new BusTicketsContext())
            {
                stops = DB.Stops.Where(x => x.Name.Contains(name)).Select(x => new StopViewModel
                {
                    Id          = x.Id,
                    Name        = x.Name,
                    Description = x.Description,
                    Status      = x.Status
                }).ToList();
            }
            return(stops);
        }
Example #13
0
        public int NewOrder(int voyageId, string userName)
        {
            int id;

            using (var DB = new BusTicketsContext())
            {
                Order order = new Order {
                    VoyageId = voyageId, Status = "open", UserName = userName
                };
                var newOrder = DB.Orders.Add(order);
                DB.SaveChanges();
                id = newOrder.Id;
            }

            return(id);
        }
Example #14
0
        public List <OrderViewModel> GetOrder(string userName)
        {
            var orders = new List <OrderViewModel>();

            using (var DB = new BusTicketsContext())
            {
                orders = DB.Orders.Where(x => x.UserName == userName).Select(x => new OrderViewModel
                {
                    Id        = x.Id,
                    Name      = x.Voyage.VoyageName,
                    Departure = x.Voyage.DepartureDateTime.ToString(),
                    Status    = x.Status
                }).ToList();
            }
            return(orders);
        }
Example #15
0
        // {Customer ID} {Grade} {Bus Company Name} {Content}
        public string Execute(IList <string> data)
        {
            var customerId     = int.Parse(data[0]);
            var grade          = double.Parse(data[1]);
            var busCompanyName = data[2];
            var content        = data[3];

            using (var db = new BusTicketsContext())
            {
                var customer = db.Customers
                               .Select(c => new
                {
                    Id   = c.Id,
                    Name = c.FirstName + " " + c.LastName
                })
                               .FirstOrDefault(c => c.Id == customerId);

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

                var company = db.Companies
                              .Select(c => new
                {
                    Id   = c.Id,
                    Name = c.Name
                })
                              .FirstOrDefault(c => c.Name == busCompanyName);

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

                db.Reviews.Add(new Review
                {
                    CompanyId  = company.Id,
                    CustomerId = customer.Id,
                    Content    = content,
                    Grade      = grade
                });
                db.SaveChanges();

                return($"Customer {customer.Name} published review for company {company.Name}");
            }
        }
        private Review CreateReview(BusTicketsContext context, int customerId, double grade, string companyName, string content)
        {
            var customer = context.Customers.SingleOrDefault(c => c.Id == customerId);

            if (customer == null)
            {
                throw new ArgumentException(string.Format(EntityNotFoundExceptionMessage, customerId));
            }

            var company = context.BusCompanies.SingleOrDefault(c => c.Name.Equals(companyName, StringComparison.OrdinalIgnoreCase));

            if (company == null)
            {
                throw new ArgumentException(string.Format(CompanyNotFoundExceptionMessage, companyName));
            }

            return(new Review(content, grade, DateTime.Now, company, customer));
        }
        // publish-review {Customer ID} {Grade} {Bus Company Name} {Content}
        public override string Execute(BusTicketsContext context)
        {
            if (this.CmdArgs.Length < 4)
            {
                throw new ArgumentException(InvalidCommandArgsExceptionMessage);
            }

            var customerId  = int.Parse(this.CmdArgs[0]);
            var grade       = double.Parse(this.CmdArgs[1]);
            var companyName = this.CmdArgs[2];
            var content     = string.Join(" ", this.CmdArgs.Skip(3));

            var review = this.CreateReview(context, customerId, grade, companyName, content);

            var customerFullName = $"{review.Customer.FirstName} {review.Customer.LastName}";

            return($"Customer {customerFullName} published review for company {review.BusCompany.Name}");
        }
Example #18
0
        private Ticket CreateTicket(BusTicketsContext context, int customerId, int tripId, decimal price, string seat)
        {
            var customer = context.Customers.SingleOrDefault(c => c.Id == customerId);

            if (customer == null)
            {
                throw new ArgumentException(string.Format(EntityNotFoundExceptionMessage, nameof(Customer), customerId));
            }

            var trip = context.Trips.SingleOrDefault(t => t.Id == tripId);

            if (trip == null)
            {
                throw new ArgumentException(string.Format(EntityNotFoundExceptionMessage, nameof(Trip), tripId));
            }

            return(new Ticket(price, seat, customer, trip));
        }
Example #19
0
        // buy-ticket {customer ID} {Trip ID} {Price} {Seat}
        public override string Execute(BusTicketsContext context)
        {
            if (this.CmdArgs.Length != 4)
            {
                throw new ArgumentException(InvalidCommandArgsExceptionMessage);
            }

            var customerId = int.Parse(this.CmdArgs[0]);
            var tripId     = int.Parse(this.CmdArgs[1]);
            var price      = decimal.Parse(this.CmdArgs[2]);
            var seat       = this.CmdArgs[3];

            var ticket = this.CreateTicket(context, customerId, tripId, price, seat);

            var customerFullName = $"{ticket.Customer.FirstName} {ticket.Customer.LastName}";

            return($"Customer {customerFullName} bought ticket for trip {ticket.Trip.Id} for {ticket.Price} on seat {ticket.Seat}");
        }
Example #20
0
        public int NewTicket(TicketViewModel ticket)
        {
            int id;

            using (var DB = new BusTicketsContext())
            {
                var newTicket = DB.Tickets.Add(new Ticket
                {
                    OrderId = ticket.OrderId,
                    PassengerDocumentNumber = ticket.DocumentNumber,
                    PassengerName           = ticket.PassengerName,
                    SeetNumber = ticket.NumberSeet,
                    Status     = "reserved"
                });
                DB.SaveChanges();
                id = newTicket.Id;
            }
            return(id);
        }
Example #21
0
        private void SeedDb(BusTicketsContext context)
        {
            var generator = new DataGenerator();

            var countries    = GetCountries(generator, SeedCount);
            var busCompanies = GetBusCompanies(generator, countries, SeedCount);
            var towns        = GetTowns(generator, countries, SeedCount);
            var busStations  = GetStations(generator, towns, SeedCount);
            var customers    = GetCustomers(generator, towns, SeedCount);
            var bankAccounts = GetBankAccounts(generator, customers, SeedCount);
            var trips        = GetTrips(generator, busStations, busCompanies, SeedCount);
            var tickets      = GetTickets(generator, customers, trips, SeedCount);
            var reviews      = GetReviews(generator, busCompanies, customers, SeedCount / 2);

            context.Trips.AddRange(trips);
            context.Tickets.AddRange(tickets);
            context.Reviews.AddRange(reviews);
            context.BankAccounts.AddRange(bankAccounts);
            context.SaveChanges();
        }
Example #22
0
        public void InitDB()
        {
            using (var DB = new BusTicketsContext())
            {
                var    stop1 = DB.Stops.First(x => x.Id == 1);
                var    stop2 = DB.Stops.First(x => x.Id == 2);
                var    stop3 = DB.Stops.First(x => x.Id == 10);
                Voyage v1    = new Voyage
                {
                    VoyageName        = "Minsk - Gomel",
                    VoyageNumber      = "15",
                    DepartureDateTime = new DateTime(2016, 11, 20, 10, 30, 00),
                    ArrivalDateTime   = new DateTime(2016, 11, 20, 15, 30, 00),
                    DepartureStop     = stop1,
                    ArriveStop        = stop3,
                    DepartureStopId   = stop1.Id,
                    ArivalStopId      = stop3.Id,
                    TravelTime        = new TimeSpan(5, 0, 0),
                    NumberOfSeets     = 100,
                    TicketCost        = 200
                };
                Voyage v2 = new Voyage
                {
                    VoyageName        = "Brest - Minsk",
                    VoyageNumber      = "21",
                    DepartureDateTime = new DateTime(2016, 11, 20, 14, 00, 00),
                    ArrivalDateTime   = new DateTime(2016, 11, 20, 18, 30, 00),
                    DepartureStop     = stop2,
                    ArriveStop        = stop1,
                    DepartureStopId   = stop2.Id,
                    ArivalStopId      = stop1.Id,
                    TravelTime        = new TimeSpan(4, 30, 0),
                    NumberOfSeets     = 100,
                    TicketCost        = 150
                };

                DB.Voyages.Add(v1);
                DB.Voyages.Add(v2);
                DB.SaveChanges();
            }
        }
Example #23
0
        public VoyageViewModel GetVoyage(int id)
        {
            VoyageViewModel voyage;

            using (var DB = new BusTicketsContext())
            {
                var result = DB.Voyages.First(x => x.Id == id);
                voyage = new VoyageViewModel {
                    Id            = result.Id,
                    Name          = result.VoyageName,
                    Number        = result.VoyageNumber,
                    TravelTime    = result.TravelTime.ToString(),
                    Departure     = result.DepartureDateTime.ToString(),
                    Arrival       = result.ArrivalDateTime.ToString(),
                    NumberSeats   = result.NumberOfSeets,
                    OneTicketCost = result.TicketCost
                };
            }

            return(voyage);
        }
Example #24
0
        //{customer ID} {Trip ID} {Price} {Seat}
        public string Execute(IList <string> data)
        {
            var customerId = int.Parse(data[0]);
            var tripId     = int.Parse(data[1]);
            var price      = decimal.Parse(data[2]);
            var seat       = data[3];

            using (var db = new BusTicketsContext())
            {
                var customer = db.Customers
                               .AsNoTracking()
                               .FirstOrDefault(c => c.Id == customerId);

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

                if (!db.Trips.Any(t => t.Id == tripId))
                {
                    throw new ArgumentException("No such trip");
                }

                db.Tickets.Add(new Ticket
                {
                    CustomerId = customerId,
                    TripId     = tripId,
                    Price      = price,
                    Seat       = seat
                });
                db.SaveChanges();

                return
                    ($"Customer {customer.FirstName} {customer.LastName} bought ticket " +
                     $"for trip {tripId} for {price} on seat {seat}");
            }
        }
Example #25
0
        // print-info {Bus Station ID}
        public override string Execute(BusTicketsContext context)
        {
            if (this.CmdArgs.Length != 1)
            {
                throw new ArgumentException(InvalidCommandArgsExceptionMessage);
            }

            var busStationId = int.Parse(this.CmdArgs[0]);
            var stationInfo  = context.BusStations
                               .Select(bs => new
            {
                bs.Id,
                StationName = bs.Name,
                TownName    = bs.Town.Name,
                Arrivals    = bs.ArrivingTrips
                              .Select(at => $"From {at.OriginBusStation.Name} | Arrive at: {at.ArrivalTime.ToString("hh:mm dd.MM.yyy")} | Status: {at.Status}"),
                Departures = bs.StartingTrips
                             .Select(st => $"To {st.DestinationBusStation.Name} | Depart at: {st.DepartureTime.ToString("hh:mm dd.MM.yyy")} | Status {st.Status}")
            })
                               .SingleOrDefault(s => s.Id == busStationId);

            if (stationInfo == null)
            {
                throw new ArgumentException(StationNotFoundExceptionMessage + busStationId);
            }

            var sb = new StringBuilder();

            sb.AppendLine($"{stationInfo.StationName}, {stationInfo.TownName}")
            .AppendLine("Arrivals:")
            .AppendLine(string.Join(Environment.NewLine, stationInfo.Arrivals))
            .AppendLine("Departures:")
            .AppendLine(string.Join(Environment.NewLine, stationInfo?.Departures));

            return(sb.ToString());
        }
Example #26
0
        public TicketInfoViewModel TicketInfo(int id)
        {
            TicketInfoViewModel ticketInfo;

            using (var DB = new BusTicketsContext())
            {
                ticketInfo = DB.Tickets.Where(x => x.Id == id).Select(x => new TicketInfoViewModel
                {
                    Id             = x.Id,
                    OrderId        = x.OrderId,
                    DocumentNumber = x.PassengerDocumentNumber,
                    PassengerName  = x.PassengerName,
                    NumberSeet     = x.SeetNumber,
                    Status         = x.Status
                }).Single();
                var voyageId = DB.Orders.Find(ticketInfo.OrderId).VoyageId;
                var voyage   = DB.Voyages.Find(voyageId);
                ticketInfo.VoyageName    = voyage.VoyageName;
                ticketInfo.VoyageNumeber = voyage.VoyageNumber;
                ticketInfo.Departure     = voyage.DepartureDateTime;
            }

            return(ticketInfo);
        }
Example #27
0
 public abstract string Execute(BusTicketsContext context);
 public BusCompanyService(BusTicketsContext context)
 {
     this.context = context;
 }
Example #29
0
 public TripService(BusTicketsContext context)
 {
     this.context = context;
 }
Example #30
0
        private static void SeedDb(BusTicketsContext db)
        {
            db.Towns.AddRange(new List <Town>
            {
                new Town {
                    Country = "Bulgaria", Name = "Sofia"
                },
                new Town {
                    Country = "Bulgaria", Name = "Varna"
                },
                new Town {
                    Country = "Bulgaria", Name = "Plovdiv"
                },
                new Town {
                    Country = "Bulgaria", Name = "Ruse"
                }
            });
            db.SaveChanges();

            db.BusStations.AddRange(new List <BusStation>
            {
                new BusStation {
                    Name = "Central Station", TownId = 1
                },
                new BusStation {
                    Name = "Union Station", TownId = 2
                },
                new BusStation {
                    Name = "Northern Station", TownId = 1
                },
                new BusStation {
                    Name = "Eastern Station", TownId = 3
                },
            });
            db.SaveChanges();

            db.Companies.AddRange(new List <Company>
            {
                new Company {
                    Name = "SomeCompany", Nationality = "bg", Rating = 6
                },
                new Company {
                    Name = "SomeCompany2", Nationality = "bg", Rating = 7
                },
                new Company {
                    Name = "SomeCompany3", Nationality = "bg", Rating = 4
                }
            });
            db.SaveChanges();

            db.Trips.AddRange(new List <Trip>
            {
                new Trip
                {
                    CompanyId               = 1,
                    ArrivalTime             = DateTime.Now,
                    DepartureTime           = DateTime.Now,
                    OriginBusStationId      = 1,
                    DestinationBusStationId = 2,
                    Status = TripStatus.Arrived
                },
                new Trip
                {
                    CompanyId               = 2,
                    ArrivalTime             = DateTime.Now,
                    DepartureTime           = DateTime.Now,
                    OriginBusStationId      = 1,
                    DestinationBusStationId = 3,
                    Status = TripStatus.Cancelled
                },
                new Trip
                {
                    CompanyId               = 1,
                    ArrivalTime             = DateTime.Now,
                    DepartureTime           = DateTime.Now,
                    OriginBusStationId      = 4,
                    DestinationBusStationId = 1,
                    Status = TripStatus.Departed
                }
            });
            db.SaveChanges();

            db.Customers.AddRange(new List <Customer>
            {
                new Customer
                {
                    FirstName   = "Mickey",
                    LastName    = "Mouse",
                    HomeTownId  = 1,
                    DateOfBirth = DateTime.MinValue,
                    Gender      = Gender.Male
                },
                new Customer
                {
                    FirstName   = "Minnie",
                    LastName    = "Mouse",
                    HomeTownId  = 2,
                    DateOfBirth = DateTime.MinValue,
                    Gender      = Gender.Female
                },
                new Customer
                {
                    FirstName   = "Donald",
                    LastName    = "Duck",
                    HomeTownId  = 3,
                    DateOfBirth = DateTime.MinValue,
                    Gender      = Gender.Male
                }
            });
            db.SaveChanges();

            db.Tickets.AddRange(new List <Ticket>
            {
                new Ticket {
                    Price = 20, Seat = "32B", CustomerId = 1, TripId = 2
                },
                new Ticket {
                    Price = 50, Seat = "16C", CustomerId = 2, TripId = 1
                },
                new Ticket {
                    Price = 32.99m, Seat = "1A", CustomerId = 1, TripId = 3
                }
            });
            db.SaveChanges();

            db.Reviews.AddRange(new List <Review>
            {
                new Review {
                    CustomerId = 1, CompanyId = 2, Grade = 8, Content = "Satisfactory", PublishedOn = DateTime.Now
                },
                new Review {
                    CustomerId = 2, CompanyId = 1, Grade = 1, Content = "Disgusting", PublishedOn = DateTime.Now
                },
                new Review {
                    CustomerId = 1, CompanyId = 3, Grade = 9, Content = "Awesome!", PublishedOn = DateTime.Now
                },
            });
            db.SaveChanges();

            Console.WriteLine("Database Initialized!");
        }