Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string version = typeof(Program).Assembly.GetName().Version.ToString();

            Console.WriteLine($"Drone Flight Log Database Management {version}");

            Operation op = new CommandParser().ParseCommandLine(args);

            if (op.Valid)
            {
                DroneFlightLogDbContext context = new DroneFlightLogDbContextFactory().CreateDbContext(null);
                DroneFlightLogFactory <DroneFlightLogDbContext> factory = new DroneFlightLogFactory <DroneFlightLogDbContext>(context);

                try
                {
                    switch (op.Type)
                    {
                    case OperationType.add:
                        factory.Users.AddUser(op.UserName, op.Password);
                        Console.WriteLine($"Added user {op.UserName}");
                        break;

                    case OperationType.setpassword:
                        factory.Users.SetPassword(op.UserName, op.Password);
                        Console.WriteLine($"Set password for user {op.UserName}");
                        break;

                    case OperationType.delete:
                        factory.Users.DeleteUser(op.UserName);
                        Console.WriteLine($"Deleted user {op.UserName}");
                        break;

                    case OperationType.update:
                        context.Database.Migrate();
                        Console.WriteLine($"Applied the latest database migrations");
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error : {ex.Message}");
                }
            }
            else
            {
                string executable = AppDomain.CurrentDomain.FriendlyName;
                Console.WriteLine("Usage:");
                Console.WriteLine($"[1] {executable} add username password");
                Console.WriteLine($"[2] {executable} setpassword username password");
                Console.WriteLine($"[3] {executable} delete username");
                Console.WriteLine($"[4] {executable} update");
            }
        }
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                // Read the application settings
                IConfiguration configuration = new ConfigurationBuilder()
                                               .AddJsonFile("appsettings.json")
                                               .Build();

                IConfigurationSection section  = configuration.GetSection("AppSettings");
                AppSettings           settings = section.Get <AppSettings>();

                // Create the factory for acessing the SQLite database
                DroneFlightLogDbContext context = new DroneFlightLogDbContextFactory().CreateDbContext(null);
                IDroneFlightLogFactory <DroneFlightLogDbContext> factory = new DroneFlightLogFactory <DroneFlightLogDbContext>(context);

                CsvReader <DroneFlightLogDbContext> reader = null;
                try
                {
                    //  Use the CSV reader to read the flights
                    reader = new CsvReader <DroneFlightLogDbContext>(settings, factory);
                    IList <Flight> flights = reader.Read(args[0]);

                    // Store the flights in the database
                    FlightWriter <DroneFlightLogDbContext> writer = new FlightWriter <DroneFlightLogDbContext>(factory);
                    writer.Save(flights);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                    if ((reader != null) && (reader.LastError != null))
                    {
                        Console.WriteLine($"Record {reader.LastError.Record} : {reader.LastError.Message}");
                    }
                }
            }
            else
            {
                string executable = AppDomain.CurrentDomain.FriendlyName;
                Console.WriteLine($"Usage : {executable} path_to_csv_file");
            }
        }