Skip to content

Commit

Permalink
Added database management tool
Browse files Browse the repository at this point in the history
  • Loading branch information
David Walker committed May 14, 2020
1 parent bea8b62 commit b1a049f
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 247 deletions.
55 changes: 0 additions & 55 deletions src/FlightRecorder.BusinessLogic/Base/ManagerBase.cs

This file was deleted.

Empty file.
Empty file.
13 changes: 0 additions & 13 deletions src/FlightRecorder.Entities/Interfaces/IManagerBase.cs

This file was deleted.

11 changes: 11 additions & 0 deletions src/FlightRecorder.Manager/Entities/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace FlightRecorder.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/FlightRecorder.Manager/Entities/OperationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace FlightRecorder.Manager.Entities
{
public enum OperationType
{
add = 0,
setpassword = 1,
delete = 2,
export = 3,
import = 4,
update = 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FlightRecorder.BusinessLogic\FlightRecorder.BusinessLogic.csproj" />
<ProjectReference Include="..\FlightRecorder.Entities\FlightRecorder.Entities.csproj" />
<Folder Include="Entities\" />
<Folder Include="Logic\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FlightRecorder.Data\FlightRecorder.Data.csproj" />
<ProjectReference Include="..\FlightRecorder.BusinessLogic\FlightRecorder.BusinessLogic.csproj" />
<ProjectReference Include="..\FlightRecorder.DataExchange\FlightRecorder.DataExchange.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
Expand Down
76 changes: 76 additions & 0 deletions src/FlightRecorder.Manager/Logic/CommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using FlightRecorder.Manager.Entities;

namespace FlightRecorder.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;
}
}
}
}
81 changes: 81 additions & 0 deletions src/FlightRecorder.Manager/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using FlightRecorder.BusinessLogic.Factory;
using FlightRecorder.Data;
using FlightRecorder.Entities.Db;
using FlightRecorder.Manager.Entities;
using FlightRecorder.Manager.Logic;
using FlightRecorder.DataExchange;

namespace FlightRecorder.Manager
{
class Program
{
static void Main(string[] args)
{
string version = typeof(Program).Assembly.GetName().Version.ToString();
Console.WriteLine($"Flight Recorder Database Management {version}");

Operation op = new CommandParser().ParseCommandLine(args);
if (op.Valid)
{
FlightRecorderDbContext context = new FlightRecorderDbContextFactory().CreateDbContext(null);
FlightRecorderFactory factory = new FlightRecorderFactory(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.import:
CsvImporter importer = new CsvImporter();
importer.Import(op.FileName, factory);
Console.WriteLine($"Imported data from {op.FileName}");
break;
case OperationType.export:
// The third parameter is an arbitrary large number intended to capture all
// sightings
IEnumerable<Sighting> sightings = factory.Sightings.List(null, 1, 99999999);
CsvExporter exporter = new CsvExporter();
exporter.Export(sightings, op.FileName);
Console.WriteLine($"Exported the database to {op.FileName}");
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} import csv_file_path");
Console.WriteLine($"[5] {executable} export csv_file_path");
Console.WriteLine($"[6] {executable} update");
}
}
}
}
5 changes: 5 additions & 0 deletions src/FlightRecorder.Manager/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"FlightRecorderDB": "Data Source=flightrecorder.db"
}
}
28 changes: 0 additions & 28 deletions src/FlightRecorder.Migrations/FlightRecorder.Migrations.csproj

This file was deleted.

15 changes: 0 additions & 15 deletions src/FlightRecorder.Migrations/Program.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/FlightRecorder.Migrations/appsettings.json

This file was deleted.

51 changes: 0 additions & 51 deletions src/FlightRecorder.Users/CommandParser.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/FlightRecorder.Users/OperationType.cs

This file was deleted.

Loading

0 comments on commit b1a049f

Please sign in to comment.