Exemplo n.º 1
0
        public IActionResult Index()
        {
            var travelers = _travelerRepository.GetAll().ToList();

            return(View(travelers));
        }
Exemplo n.º 2
0
        public static void Main()
        {
            Console.WriteLine("TraPaConsole is starting.");

            //todo connection string
            IDataAccessor dataAccessor = new DataAccessor(false, "Data Source=ConsoleRunDatabase.db");

            using (var dbContext = dataAccessor.GetNewDatabaseContext())
            {
                //dbContext.Database.EnsureDeleted();
                dbContext.Database.EnsureCreated();
                //dbContext.Database.Migrate();
            }
            IDatabaseRepositoryFactory databaseRepositoryFactory = new DatabaseRepositoryFactory(dataAccessor);

            ITravelerRepository   travelerRepository   = databaseRepositoryFactory.GetNewTravelerRepository();
            ITravelDateRepository travelDateRepository = databaseRepositoryFactory.GetNewTravelDateRepository();
            ITravelerTravelDateReferenceRepository travelerTravelDateReferenceRepository =
                databaseRepositoryFactory.GetNewTravelerTravelDateReferenceRepository();

            Console.WriteLine("Provide the action you would like to execute \n" +
                              "add : add a new traveler to the database \n" +
                              "list : list all travelers \n" +
                              "delete : remove traveler with a given index \n" +
                              "addtravel : add a new travel to the database \n" +
                              "listtravel : list all travels \n" +
                              "deletetravel : remove travel with a given index \n" +
                              "addreference : add a new travel travel date reference to the database \n" +
                              "listreference : list all travel travel date references \n" +
                              "deletereference : remove travel travel date references with a given index \n" +
                              "q : exit");

            var flag = true;

            do
            {
                var input       = Console.ReadLine();
                var punctuation = input.Where(Char.IsPunctuation).Distinct().ToArray();
                var words       = input.Split().Select(x => x.Trim(punctuation)).ToList();

                switch (words[0])
                {
                case "add":
                    try
                    {
                        var traveler = CreateTraveler(words);
                        traveler = travelerRepository.Add(traveler.FirstName, traveler.LastName, traveler.PhoneNumber, traveler.DirectionFrom, traveler.DirectionTo, traveler.DoesPay, traveler.Price);
                        Console.WriteLine($"Traveler was added. Id: '{traveler.Id}'");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not add the traveler to the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "list":
                    var travelers = travelerRepository.GetAll();
                    WriteTravelers(travelers);
                    break;

                case "delete":
                    try
                    {
                        var idToDelete = Int32.Parse(words[1]);
                        travelerRepository.Remove(idToDelete);
                        Console.WriteLine($"Traveler was removed. Id: {idToDelete}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not remove the traveler from the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "addtravel":
                    try
                    {
                        var travel = CreateTravel(words);
                        travelDateRepository.Add(travel.TravelName, travel.DateOfTravel);
                        Console.WriteLine("Travel was added.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not add the travel to the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "listtravel":
                    var travelDates = travelDateRepository.GetAll();
                    WriteTravels(travelDates);
                    break;

                case "deletetravel":
                    try
                    {
                        var idToDelete = Int32.Parse(words[1]);
                        travelDateRepository.Remove(idToDelete);
                        Console.WriteLine($"Travel was removed. Id: {idToDelete}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not remove the travel from the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "addreference":
                    try
                    {
                        var travelTravelDateReference = CreateTravelTravelDateReference(words);
                        travelerTravelDateReferenceRepository.Add(travelTravelDateReference.TravelerId, travelTravelDateReference.TravelDateId);
                        Console.WriteLine("Traveler - travel date reference was added.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not add the traveler - travel date reference to the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "listreference":
                    var travelerTravelDateReferences = travelerTravelDateReferenceRepository.GetAll();
                    WriteTravelTravelDateReferences(travelerTravelDateReferences);
                    break;

                case "deletereference":
                    try
                    {
                        var travelerId   = Int32.Parse(words[1]);
                        var travelDateId = Int32.Parse(words[2]);
                        travelerTravelDateReferenceRepository.Remove(travelerId, travelDateId);
                        Console.WriteLine("Traveler - travel date reference was removed.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Could not remove the traveler - travel date reference from the database. Reason:");
                        Console.WriteLine(e);
                    }
                    break;

                case "q":
                    flag = false;
                    Console.WriteLine("Stopping the application.");
                    break;

                default:
                    Console.WriteLine(@"Invalid key: {0}, try again.", words[0]);
                    break;
                }
            } while (flag);
        }