-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Walker
committed
May 14, 2020
1 parent
bea8b62
commit b1a049f
Showing
18 changed files
with
196 additions
and
247 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file modified
0
src/FlightRecorder.BusinessLogic/Extensions/FlightRecorderStringExtensions.cs
100755 → 100644
Empty file.
Empty file modified
0
src/FlightRecorder.Entities/Exceptions/InvalidRecordFormatException.cs
100755 → 100644
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/FlightRecorder.Migrations/FlightRecorder.Migrations.csproj
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.