Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR-31 Sightings Export #19

Merged
merged 2 commits into from
Oct 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public class FlightRecorderFactory
private readonly Lazy<IUserManager> _users = null;
private readonly Lazy<ICountryManager> _countries = null;
private readonly Lazy<IAirportManager> _airports = null;
private readonly Lazy<IJobStatusManager> _jobStatuses = null;
private readonly Lazy<IDateBasedReport<AirlineStatistics>> _airlineStatistics = null;
private readonly Lazy<IDateBasedReport<LocationStatistics>> _locationStatistics = null;
private readonly Lazy<IDateBasedReport<ManufacturerStatistics>> _manufacturerStatistics = null;
private readonly Lazy<IDateBasedReport<ModelStatistics>> _modelStatistics = null;
private readonly Lazy<IDateBasedReport<FlightsByMonth>> _flightsByMonth = null;

public FlightRecorderDbContext Context { get; private set; }

public IAirlineManager Airlines { get { return _airlines.Value; } }
public ILocationManager Locations { get { return _locations.Value; } }
public IManufacturerManager Manufacturers { get { return _manufacturers.Value; } }
Expand All @@ -35,6 +38,7 @@ public class FlightRecorderFactory
public IUserManager Users { get { return _users.Value; } }
public ICountryManager Countries { get { return _countries.Value; } }
public IAirportManager Airports { get { return _airports.Value; } }
public IJobStatusManager JobStatuses { get { return _jobStatuses.Value; } }

[ExcludeFromCodeCoverage]
public IDateBasedReport<AirlineStatistics> AirlineStatistics { get { return _airlineStatistics.Value; } }
Expand All @@ -51,10 +55,13 @@ public class FlightRecorderFactory
[ExcludeFromCodeCoverage]
public IDateBasedReport<FlightsByMonth> FlightsByMonth { get { return _flightsByMonth.Value; } }


public FlightRecorderFactory(FlightRecorderDbContext context)
{
// Store the database context
Context = context;

// Lazily instantiate the database managers : They'll only actually be created if called by
// the application
_airlines = new Lazy<IAirlineManager>(() => new AirlineManager(context));
_locations = new Lazy<ILocationManager>(() => new LocationManager(context));
_manufacturers = new Lazy<IManufacturerManager>(() => new ManufacturerManager(context));
Expand All @@ -65,6 +72,10 @@ public FlightRecorderFactory(FlightRecorderDbContext context)
_users = new Lazy<IUserManager>(() => new UserManager(context));
_countries = new Lazy<ICountryManager>(() => new CountryManager(context));
_airports = new Lazy<IAirportManager>(() => new AirportManager(this));
_jobStatuses = new Lazy<IJobStatusManager>(() => new JobStatusManager(context));

// Lazily instantiate the reporting managers : Once again, they'll only actually be created if called by
// the application
_airlineStatistics = new Lazy<IDateBasedReport<AirlineStatistics>>(() => new DateBasedReport<AirlineStatistics>(context));
_locationStatistics = new Lazy<IDateBasedReport<LocationStatistics>>(() => new DateBasedReport<LocationStatistics>(context));
_manufacturerStatistics = new Lazy<IDateBasedReport<ManufacturerStatistics>>(() => new DateBasedReport<ManufacturerStatistics>(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>FlightRecorder.BusinessLogic</PackageId>
<PackageVersion>1.1.4.0</PackageVersion>
<PackageVersion>1.2.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2020, 2021, 2022, 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,7 +16,7 @@
<PackageProjectUrl>https://github.com/davewalker5/FlightRecorderDb</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.1.4.0</ReleaseVersion>
<ReleaseVersion>1.2.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
98 changes: 98 additions & 0 deletions src/FlightRecorder.BusinessLogic/Logic/JobStatusManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using FlightRecorder.BusinessLogic.Extensions;
using FlightRecorder.Data;
using FlightRecorder.Entities.Db;
using FlightRecorder.Entities.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace FlightRecorder.BusinessLogic.Logic
{
internal class JobStatusManager : IJobStatusManager
{
private readonly FlightRecorderDbContext _context;

internal JobStatusManager(FlightRecorderDbContext context)
{
_context = context;
}

/// <summary>
/// Get the first job status matching the specified criteria
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<JobStatus> GetAsync(Expression<Func<JobStatus, bool>> predicate)
{
List<JobStatus> statuses = await ListAsync(predicate, 1, 1).ToListAsync();
return statuses.FirstOrDefault();
}

/// <summary>
/// Return all entities matching the specified criteria
/// </summary>
/// <param name="predicate"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public IAsyncEnumerable<JobStatus> ListAsync(Expression<Func<JobStatus, bool>> predicate, int pageNumber, int pageSize)
{
IAsyncEnumerable<JobStatus> results;
if (predicate == null)
{
results = _context.JobStatuses
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.AsAsyncEnumerable();
}
else
{
results = _context.JobStatuses.Where(predicate).AsAsyncEnumerable();
}

return results;
}

/// <summary>
/// Create a new job status
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public async Task<JobStatus> AddAsync(string name, string parameters)
{
JobStatus status = new JobStatus
{
Name = name.CleanString(),
Parameters = parameters.CleanString(),
Start = DateTime.Now
};

await _context.JobStatuses.AddAsync(status);
await _context.SaveChangesAsync();

return status;
}

/// <summary>
/// Update a job status, setting the end timestamp and error result
/// </summary>
/// <param name="id"></param>
/// <param name="error"></param>
/// <returns></returns>
public async Task<JobStatus> UpdateAsync(long id, string error)
{
JobStatus status = await GetAsync(x => x.Id == id);
if (status != null)
{
status.End = DateTime.Now;
status.Error = error.CleanString();
await _context.SaveChangesAsync();
}

return status;
}
}
}
16 changes: 0 additions & 16 deletions src/FlightRecorder.BusinessLogic/Sql/ManufacturerStatistis.sql

This file was deleted.

4 changes: 2 additions & 2 deletions src/FlightRecorder.Data/FlightRecorder.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>FlightRecorder.Data</PackageId>
<PackageVersion>1.1.4.0</PackageVersion>
<PackageVersion>1.2.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2020, 2021, 2022, 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,7 +16,7 @@
<PackageProjectUrl>https://github.com/davewalker5/FlightRecorderDb</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.1.4.0</ReleaseVersion>
<ReleaseVersion>1.2.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/FlightRecorder.Data/FlightRecorderDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class FlightRecorderDbContext : DbContext
public virtual DbSet<Manufacturer> Manufacturers { get; set; }
public virtual DbSet<Model> Models { get; set; }
public virtual DbSet<Sighting> Sightings { get; set; }
public virtual DbSet<JobStatus> JobStatuses { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<AirlineStatistics> AirlineStatistics { get; set; }
public virtual DbSet<LocationStatistics> LocationStatistics { get; set; }
Expand Down Expand Up @@ -203,6 +204,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.LocationId).HasColumnName("location_id");
});

modelBuilder.Entity<JobStatus>(entity =>
{
entity.ToTable("JOB_STATUS");

entity.Property(e => e.Id)
.HasColumnName("id")
.ValueGeneratedOnAdd();

entity.Property(e => e.Name).IsRequired().HasColumnName("name");
entity.Property(e => e.Parameters).HasColumnName("parameters");
entity.Property(e => e.Start).IsRequired().HasColumnName("start").HasColumnType("DATETIME");
entity.Property(e => e.End).HasColumnName("end").HasColumnType("DATETIME");
entity.Property(e => e.Error).HasColumnName("error");
});

modelBuilder.Entity<User>(entity =>
{
Expand Down
Loading
Loading