Skip to content

Commit

Permalink
Added database management app
Browse files Browse the repository at this point in the history
  • Loading branch information
David Walker committed May 14, 2020
1 parent 35b03dc commit b7639a3
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/DroneFlightLog.Manager/DroneFlightLog.Manager.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DroneFlightLog.Data\DroneFlightLog.Data.csproj" />
<ProjectReference Include="..\DroneFlightLog.Data.Sqlite\DroneFlightLog.Data.Sqlite.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions src/DroneFlightLog.Manager/Entities/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DroneFlightLog.Manager.Entities
{
public class Operation
{
public bool Valid { get; set; }
public OperationType Type { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string FileName { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/DroneFlightLog.Manager/Entities/OperationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DroneFlightLog.Manager.Entities
{
public enum OperationType
{
add = 0,
setpassword = 1,
delete = 2,
export = 3,
import = 4,
update = 5
}
}
76 changes: 76 additions & 0 deletions src/DroneFlightLog.Manager/Logic/CommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using DroneFlightLog.Manager.Entities;

namespace DroneFlightLog.Manager.Logic
{
public class CommandParser
{
// The index into this array is one of the values from the OperationType
// enumeration, mapping the operation to the required argument count
private readonly int[] _requiredArgumentCount = { 3, 3, 2, 2, 2, 1 };

/// <summary>
/// Parse the command line, extracting the operation to be performed
/// and its parameters
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public Operation ParseCommandLine(string[] args)
{
Operation op = new Operation();

if (args.Length > 0)
{
// Attempt to parse out the operation type from the first argument
if (Enum.TryParse<OperationType>(args[0], out OperationType type))
{
// Check there are sufficient arguments for this operation
op.Type = type;
int requiredArgumentCount = _requiredArgumentCount[(int)type];

// All is OK at this point if the argument count is correct
op.Valid = (args.Length == requiredArgumentCount);
if (op.Valid)
{
// Extract the arguments
AssignOperationParameters(op, args);
}
}
}

return op;
}

/// <summary>
/// For those operations that require it, assign the operation parameters
/// from the command line, based on the operation type
/// </summary>
/// <param name="op"></param>
/// <param name="args"></param>
private void AssignOperationParameters(Operation op, string[] args)
{
switch (op.Type)
{
case OperationType.add:
op.UserName = args[1];
op.Password = args[2];
break;
case OperationType.delete:
op.UserName = args[1];
break;
case OperationType.setpassword:
op.UserName = args[1];
op.Password = args[2];
break;
case OperationType.import:
op.FileName = args[1];
break;
case OperationType.export:
op.FileName = args[1];
break;
default:
break;
}
}
}
}
63 changes: 63 additions & 0 deletions src/DroneFlightLog.Manager/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using DroneFlightLog.Data.Factory;
using DroneFlightLog.Data.Sqlite;
using DroneFlightLog.Manager.Entities;
using DroneFlightLog.Manager.Logic;
using Microsoft.EntityFrameworkCore;

namespace DroneFlightLog.Manager
{
class Program
{
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");
}
}
}
}
5 changes: 5 additions & 0 deletions src/DroneFlightLog.Manager/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"DroneLogDb": "Data Source=droneflightlog.db"
}
}
12 changes: 6 additions & 6 deletions src/DroneFlightLogDb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DroneFlightLog.Data.InMemor
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DroneFlightLog.Data.Sqlite", "DroneFlightLog.Data.Sqlite\DroneFlightLog.Data.Sqlite.csproj", "{3CC17699-2B58-4FAA-8A97-3DA91982DFBD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DroneFlightLog.Data.Migrations", "DroneFlightLog.Data.Migrations\DroneFlightLog.Data.Migrations.csproj", "{D08E58F6-E94A-4FD8-AECB-E5FE58792DDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DroneFlightLog.Data.Tests", "DroneFlightLog.Data.Tests\DroneFlightLog.Data.Tests.csproj", "{A8953DA2-FB16-4011-BD84-AFE02582AD5C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DroneFlightLog.Manager", "DroneFlightLog.Manager\DroneFlightLog.Manager.csproj", "{1771E4AF-1AC9-4A41-AC0B-DA462EFBC086}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,13 +29,13 @@ Global
{3CC17699-2B58-4FAA-8A97-3DA91982DFBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CC17699-2B58-4FAA-8A97-3DA91982DFBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CC17699-2B58-4FAA-8A97-3DA91982DFBD}.Release|Any CPU.Build.0 = Release|Any CPU
{D08E58F6-E94A-4FD8-AECB-E5FE58792DDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D08E58F6-E94A-4FD8-AECB-E5FE58792DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D08E58F6-E94A-4FD8-AECB-E5FE58792DDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D08E58F6-E94A-4FD8-AECB-E5FE58792DDE}.Release|Any CPU.Build.0 = Release|Any CPU
{A8953DA2-FB16-4011-BD84-AFE02582AD5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8953DA2-FB16-4011-BD84-AFE02582AD5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8953DA2-FB16-4011-BD84-AFE02582AD5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8953DA2-FB16-4011-BD84-AFE02582AD5C}.Release|Any CPU.Build.0 = Release|Any CPU
{1771E4AF-1AC9-4A41-AC0B-DA462EFBC086}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1771E4AF-1AC9-4A41-AC0B-DA462EFBC086}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1771E4AF-1AC9-4A41-AC0B-DA462EFBC086}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1771E4AF-1AC9-4A41-AC0B-DA462EFBC086}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit b7639a3

Please sign in to comment.