Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions DbUtil/SqlScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public SqlScripts()
new Script("Restructure Locations",RestructureLocations),
new Script("Add 3 columns to Phones", Add3ColumnsToPhones),
new Script("Create SIMs Table", CreateSIMsTable),
new Script("Replace ImportHistory Table", ReplaceImportHistoryTable)
];
}

Expand Down Expand Up @@ -48,16 +47,6 @@ PRIMARY KEY (PhoneNumber, BillingPeriod)
);
""";

private const string ReplaceImportHistoryTable = """
DROP TABLE ImportHistory;
CREATE TABLE ImportHistory (
Name TEXT NOT NULL,
Run TEXT NOT NULL,
ImportDate TEXT NOT NULL DEFAULT (CURRENT_TIMESTAMP),
PRIMARY KEY (Name, Run)
)
""";

private static string RestructureLocations
{
get
Expand Down
19 changes: 7 additions & 12 deletions PhoneAssistant.Cli/EECommand/EE.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System.CommandLine;
using System.IO.Compression;
using System.Numerics;
using System.Text;

using FluentResults;

using FluentResults;
using PhoneAssistant.Model;

using Serilog;
using System.CommandLine;
using System.IO.Compression;
using System.Text;

namespace PhoneAssistant.Cli.EECommand;

Expand Down Expand Up @@ -90,9 +86,9 @@ private static void Execute(DirectoryInfo? directory)
Log.Information("Billing period identified as: {BillingPeriod}", billingPeriod);

PhoneAssistantDbContext dbContext = ModelContext.Create();
ImportHistoryRepository importRepository = new(dbContext);
bool runExists = importRepository.RunExistsAsync(ImportType.BaseReport, billingPeriod).GetAwaiter().GetResult();
if (runExists)
SimRepository repository = new(dbContext);
string latestBillingPeriod = repository.GetLatestBillingPeriod().GetAwaiter().GetResult();
if (latestBillingPeriod == billingPeriod)
{
Log.Error("A run for this billing period already exists.");
return;
Expand Down Expand Up @@ -145,7 +141,6 @@ private static void Execute(DirectoryInfo? directory)
dbContext.Sims.Add(sim);
}

importRepository.CreateAsync(ImportType.BaseReport, billingPeriod).GetAwaiter().GetResult();
dbContext.SaveChanges();
Log.Information("Import completed. {LineCount} records processed.", lineCount);
}
Expand Down

This file was deleted.

37 changes: 37 additions & 0 deletions PhoneAssistant.Model.Tests/Repositories/SimRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace PhoneAssistant.Model.Tests;

public class SimRepositoryTests
{
readonly DbTestHelper _helper = new();
readonly SimRepository _repository;

public SimRepositoryTests()
{
_repository = new(_helper.DbContext);
}

[Test]
public async Task GetLatestBillingPeriod_should_return_Latest_when_Sims_exist()
{
Sim sim1 = new() { BillingPeriod = "202601", SIMNumber = "8944122605563572205", PhoneNumber = "07814209742", UserName = "John Doe", BroadbandData = 100 , TextMessages = 50, VoiceCalls = 20 };
Sim sim2 = new() { BillingPeriod = "202602", SIMNumber = "8944122605563572206", PhoneNumber = "07814209743", UserName = "Jane Smith", BroadbandData = 150, TextMessages = 75, VoiceCalls = 30 };
Sim sim3 = new() { BillingPeriod = "202501", SIMNumber = "8944122605563572207", PhoneNumber = "07814209744", UserName = "Bob Johnson", BroadbandData = 200, TextMessages = 100, VoiceCalls = 40 };

_helper.DbContext.Sims.Add(sim1);
_helper.DbContext.Sims.Add(sim2);
await _helper.DbContext.SaveChangesAsync();

var actual = await _repository.GetLatestBillingPeriod();

await Assert.That(actual).IsEqualTo("202602");
}

[Test]
public async Task GetLatestBillingPeriod_should_return_Unknown_when_no_Sims_exist()
{
var actual = await _repository.GetLatestBillingPeriod();

await Assert.That(actual).IsEqualTo("Unknown");
}

}
14 changes: 0 additions & 14 deletions PhoneAssistant.Model/Entities/ImportHistory.cs

This file was deleted.

10 changes: 0 additions & 10 deletions PhoneAssistant.Model/PhoneAssistantDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public PhoneAssistantDbContext() { }

public PhoneAssistantDbContext(DbContextOptions options) : base(options) { }

public DbSet<ImportHistory> Imports => Set<ImportHistory>();

public DbSet<Location> Locations => Set<Location>();

public DbSet<Phone> Phones => Set<Phone>();
Expand Down Expand Up @@ -42,14 +40,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ImportHistory>()
.ToTable("ImportHistory")
.HasKey(e => new { e.Name, e.Run });
modelBuilder.Entity<ImportHistory>()
.Property(e => e.Name).HasConversion(n => n.ToString(), n => Enum.Parse<ImportType>(n));
modelBuilder.Entity<ImportHistory>()
.Property(e => e.ImportDate).HasDefaultValueSql("CURRENT_TIMESTAMP");

modelBuilder.Entity<Location>(l =>
{
l.HasKey(e => e.Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
namespace PhoneAssistant.Model;

public interface IApplicationSettingsRepository
{
ApplicationSettings ApplicationSettings { get; init; }
void Save();
}
public sealed class ApplicationSettingsRepository : IApplicationSettingsRepository
{
private readonly string _appSettingsPath;
Expand Down

This file was deleted.

7 changes: 0 additions & 7 deletions PhoneAssistant.Model/Repositories/IImportHistoryRepository.cs

This file was deleted.

6 changes: 0 additions & 6 deletions PhoneAssistant.Model/Repositories/ILocationsRepository.cs

This file was deleted.

16 changes: 0 additions & 16 deletions PhoneAssistant.Model/Repositories/IPhonesRepository.cs

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions PhoneAssistant.Model/Repositories/ImportHistoryRepository.cs

This file was deleted.

5 changes: 5 additions & 0 deletions PhoneAssistant.Model/Repositories/LocationsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace PhoneAssistant.Model;

public interface ILocationsRepository
{
Task<IEnumerable<Location>> GetAllLocationsAsync();
}

public sealed class LocationsRepository : ILocationsRepository
{
private readonly PhoneAssistantDbContext _dbContext;
Expand Down
14 changes: 14 additions & 0 deletions PhoneAssistant.Model/Repositories/PhonesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

namespace PhoneAssistant.Model;

public interface IPhonesRepository
{
Task<bool> AssetTagUniqueAsync(string? assetTag);
Task<bool> ConcurrentChange(string imei, string lastUpdate);
Task CreateAsync(Phone phone);
Task<bool> ExistsAsync(string imei);
Task<IEnumerable<Phone>> GetActivePhonesAsync();
Task<IEnumerable<Phone>> GetAllPhonesAsync();
Task<Phone?> GetPhoneAsync(string imei);
Task<bool> UserHasProductionPhone(string user);
Task<bool> PhoneNumberExistsAsync(string phoneNumber);
Task<UpdateStatus> UpdateAsync(Phone phone);
Task UpdateStatusAsync(string imei, string status);
}
public sealed class PhonesRepository(PhoneAssistantDbContext dbContext) : IPhonesRepository
{
public async Task<bool> AssetTagUniqueAsync(string? assetTag)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace PhoneAssistant.Model;

public sealed class ServiceRequestsRepository : IServiceRequestsRepository
public interface IServiceRequestsRepository
{
private readonly PhoneAssistantDbContext _dbContext;
}

public ServiceRequestsRepository(PhoneAssistantDbContext dbContext) => _dbContext = dbContext;
public sealed class ServiceRequestsRepository(PhoneAssistantDbContext dbContext) : IServiceRequestsRepository
{
private readonly PhoneAssistantDbContext _dbContext = dbContext;
}
10 changes: 10 additions & 0 deletions PhoneAssistant.Model/Repositories/SimRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace PhoneAssistant.Model;
public interface ISimRepository
{
Task CreateAsync(Sim sim);
Task<string> GetLatestBillingPeriod();
Task<IEnumerable<Sim>> GetSim(string phoneNumber);
Task<string?> GetSimNumberAsync(string phoneNumber);
}
Expand All @@ -17,6 +18,15 @@ public async Task CreateAsync(Sim sim)
await dbContext.SaveChangesAsync();
}

public async Task<string> GetLatestBillingPeriod()
{
string? latestBillingPeriod = await dbContext.Sims
.OrderByDescending(s => s.BillingPeriod)
.Select(s => s.BillingPeriod)
.FirstOrDefaultAsync();
return latestBillingPeriod ?? "Unknown";
}

public async Task<IEnumerable<Sim>> GetSim(string phoneNumber)
{
IEnumerable<Sim> sims = await dbContext.Sims
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static IHostBuilder ConfigureApplicationServices(this IHostBuilder host)
new UpdateOptions() { AllowVersionDowngrade = true }));

// Repositories
services.AddSingleton<IImportHistoryRepository, ImportHistoryRepository>();
services.AddSingleton<ILocationsRepository, LocationsRepository>();
services.AddSingleton<IPhonesRepository, PhonesRepository>();
services.AddSingleton<ISimRepository, SimRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public interface IBaseReportMainViewModel : IViewModel
{
}

public partial class BaseReportMainViewModel(IImportHistoryRepository importHistory, ISimRepository repository) : ViewModelBase, IBaseReportMainViewModel
public partial class BaseReportMainViewModel(ISimRepository repository) : ViewModelBase, IBaseReportMainViewModel
{
private readonly IImportHistoryRepository _import = importHistory ?? throw new ArgumentNullException(nameof(importHistory));
private readonly ISimRepository _repository = repository ?? throw new ArgumentNullException(nameof(repository));

public ObservableCollection<BaseReportSim> BaseReportSims { get; } = [];
Expand Down Expand Up @@ -45,8 +44,7 @@ private async Task EnterKey()

public override async Task LoadAsync()
{
ImportHistory? importHistory = await _import.GetLatestImportAsync(ImportType.BaseReport);
LatestImport = importHistory is null ? $"Latest Import: None" : $"Latest Import: {importHistory.Run} ({importHistory.ImportDate})";
LatestImport = $"Latest Import: {await _repository.GetLatestBillingPeriod()}";
}
}

Expand Down
Loading