From e110861232b36046c19233b4cd1d6825f8db9183 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:12:58 +0100 Subject: [PATCH 1/7] Added paging to aircraft retrieval --- .../FlightRecorder.BusinessLogic.csproj | 2 +- .../Logic/AircraftManager.cs | 30 ++++++++++++------- .../FlightRecorder.Entities.csproj | 2 +- .../Interfaces/IAircraftManager.cs | 12 ++++---- .../AircraftManagerTest.cs | 24 +++++++-------- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/FlightRecorder.BusinessLogic.csproj b/src/FlightRecorder.BusinessLogic/FlightRecorder.BusinessLogic.csproj index ddf6b21..504bbc4 100644 --- a/src/FlightRecorder.BusinessLogic/FlightRecorder.BusinessLogic.csproj +++ b/src/FlightRecorder.BusinessLogic/FlightRecorder.BusinessLogic.csproj @@ -3,7 +3,7 @@ netstandard2.1 FlightRecorder.BusinessLogic - 1.0.0.2 + 1.0.0.3 Dave Walker Copyright (c) Dave Walker 2020 Dave Walker diff --git a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs index 0066bca..1a11e5c 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs @@ -26,7 +26,7 @@ internal AircraftManager(FlightRecorderFactory factory) /// /// public Aircraft Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Get the first aircraft matching the specified criteria along with the associated model @@ -45,8 +45,10 @@ public async Task GetAsync(Expression> predicate) /// Get the aircraft matching the specified criteria along with the associated models /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable aircraft; @@ -71,8 +73,10 @@ public IEnumerable List(Expression> predicate = n /// Get the aircraft matching the specified criteria along with the associated models /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable aircraft; @@ -81,6 +85,8 @@ public IAsyncEnumerable ListAsync(Expression> pre aircraft = _factory.Context.Aircraft .Include(a => a.Model) .ThenInclude(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } else @@ -89,6 +95,8 @@ public IAsyncEnumerable ListAsync(Expression> pre .Include(a => a.Model) .ThenInclude(m => m.Manufacturer) .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } @@ -100,7 +108,7 @@ public IAsyncEnumerable ListAsync(Expression> pre /// /// /// - public IEnumerable ListByModel(string modelName) + public IEnumerable ListByModel(string modelName, int pageNumber, int pageSize) { IEnumerable matches = null; @@ -108,7 +116,7 @@ public IEnumerable ListByModel(string modelName) Model model = _factory.Models.Get(m => m.Name == modelName); if (model != null) { - matches = List(m => m.ModelId == model.Id); + matches = List(m => m.ModelId == model.Id, pageNumber, pageSize); } return matches; @@ -119,7 +127,7 @@ public IEnumerable ListByModel(string modelName) /// /// /// - public async Task> ListByModelAsync(string modelName) + public async Task> ListByModelAsync(string modelName, int pageNumber, int pageSize) { IAsyncEnumerable matches = null; @@ -127,7 +135,7 @@ public async Task> ListByModelAsync(string modelName) Model model = await _factory.Models.GetAsync(m => m.Name == modelName); if (model != null) { - matches = ListAsync(m => m.ModelId == model.Id); + matches = ListAsync(m => m.ModelId == model.Id, pageNumber, pageSize); } return matches; @@ -138,7 +146,7 @@ public async Task> ListByModelAsync(string modelName) /// /// /// - public IEnumerable ListByManufacturer(string manufacturerName) + public IEnumerable ListByManufacturer(string manufacturerName, int pageNumber, int pageSize) { IEnumerable matches = null; @@ -152,7 +160,7 @@ public IEnumerable ListByManufacturer(string manufacturerName) .Select(m => m.Id); if (modelIds.Any()) { - matches = List(a => modelIds.Contains(a.ModelId)); + matches = List(a => modelIds.Contains(a.ModelId), pageNumber, pageSize); } } @@ -164,7 +172,7 @@ public IEnumerable ListByManufacturer(string manufacturerName) /// /// /// - public async Task> ListByManufacturerAsync(string manufacturerName) + public async Task> ListByManufacturerAsync(string manufacturerName, int pageNumber, int pageSize) { IAsyncEnumerable matches = null; @@ -179,7 +187,7 @@ public async Task> ListByManufacturerAsync(string man .ToListAsync(); if (modelIds.Any()) { - matches = ListAsync(a => modelIds.Contains(a.ModelId)); + matches = ListAsync(a => modelIds.Contains(a.ModelId), pageNumber, pageSize); } } diff --git a/src/FlightRecorder.Entities/FlightRecorder.Entities.csproj b/src/FlightRecorder.Entities/FlightRecorder.Entities.csproj index b4d18fd..66bde13 100644 --- a/src/FlightRecorder.Entities/FlightRecorder.Entities.csproj +++ b/src/FlightRecorder.Entities/FlightRecorder.Entities.csproj @@ -3,7 +3,7 @@ netstandard2.1 FlightRecorder.Entities - 1.0.0.3 + 1.0.0.4 Dave Walker Copyright (c) Dave Walker 2020 Dave Walker diff --git a/src/FlightRecorder.Entities/Interfaces/IAircraftManager.cs b/src/FlightRecorder.Entities/Interfaces/IAircraftManager.cs index 1389597..6f97da4 100644 --- a/src/FlightRecorder.Entities/Interfaces/IAircraftManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/IAircraftManager.cs @@ -12,11 +12,11 @@ public interface IAircraftManager Task AddAsync(string registration, string serialNumber, long yearOfManufacture, string modelName, string manufacturerName); Aircraft Get(Expression> predicate = null); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); - IEnumerable ListByModel(string modelName); - Task> ListByModelAsync(string modelName); - IEnumerable ListByManufacturer(string manufacturerName); - Task> ListByManufacturerAsync(string manufacturerName); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); + IEnumerable ListByModel(string modelName, int pageNumber, int pageSize); + Task> ListByModelAsync(string modelName, int pageNumber, int pageSize); + IEnumerable ListByManufacturer(string manufacturerName, int pageNumber, int pageSize); + Task> ListByManufacturerAsync(string manufacturerName, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/AircraftManagerTest.cs b/src/FlightRecorder.Tests/AircraftManagerTest.cs index 73519b5..46fa4a8 100644 --- a/src/FlightRecorder.Tests/AircraftManagerTest.cs +++ b/src/FlightRecorder.Tests/AircraftManagerTest.cs @@ -81,7 +81,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable aircraft = _factory.Aircraft.List(); + IEnumerable aircraft = _factory.Aircraft.List(null, 1, 100); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); Assert.AreEqual(ModelName, aircraft.First().Model.Name); @@ -92,7 +92,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List aircraft = await _factory.Aircraft - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); @@ -103,7 +103,7 @@ public async Task ListAllAsyncTest() [TestMethod] public void FilteredListTest() { - IEnumerable aircraft = _factory.Aircraft.List(e => e.Registration == Registration); + IEnumerable aircraft = _factory.Aircraft.List(e => e.Registration == Registration, 1, 100); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); Assert.AreEqual(ModelName, aircraft.First().Model.Name); @@ -114,7 +114,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List aircraft = await _factory.Aircraft - .ListAsync(e => e.Registration == Registration) + .ListAsync(e => e.Registration == Registration, 1, 100) .ToListAsync(); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); @@ -125,7 +125,7 @@ public async Task FilteredListAsyncTest() [TestMethod] public void FilterByModelTest() { - IEnumerable aircraft = _factory.Aircraft.ListByModel(ModelName); + IEnumerable aircraft = _factory.Aircraft.ListByModel(ModelName, 1, 100); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); Assert.AreEqual(ModelName, aircraft.First().Model.Name); @@ -136,7 +136,7 @@ public void FilterByModelTest() public async Task FilterByModelAsyncTest() { IAsyncEnumerable matches = await _factory.Aircraft - .ListByModelAsync(ModelName); + .ListByModelAsync(ModelName, 1, 100); List aircraft = await matches.ToListAsync(); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); @@ -147,7 +147,7 @@ public async Task FilterByModelAsyncTest() [TestMethod] public void FilterByManufacturerTest() { - IEnumerable aircraft = _factory.Aircraft.ListByManufacturer(ManufacturerName); + IEnumerable aircraft = _factory.Aircraft.ListByManufacturer(ManufacturerName, 1, 100); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); Assert.AreEqual(ModelName, aircraft.First().Model.Name); @@ -158,7 +158,7 @@ public void FilterByManufacturerTest() public async Task FilterByManufacturerAsyncTest() { IAsyncEnumerable matches = await _factory.Aircraft - .ListByManufacturerAsync(ManufacturerName); + .ListByManufacturerAsync(ManufacturerName, 1, 100); List aircraft = await matches.ToListAsync(); Assert.AreEqual(1, aircraft.Count()); Assert.AreEqual(Registration, aircraft.First().Registration); @@ -169,21 +169,21 @@ public async Task FilterByManufacturerAsyncTest() [TestMethod] public void ListMissingTest() { - IEnumerable aircraft = _factory.Aircraft.List(e => e.Registration == "Missing"); + IEnumerable aircraft = _factory.Aircraft.List(e => e.Registration == "Missing", 1, 100); Assert.AreEqual(0, aircraft.Count()); } [TestMethod] public void ListByMissingModelTest() { - IEnumerable aircraft = _factory.Aircraft.ListByModel("Missing"); + IEnumerable aircraft = _factory.Aircraft.ListByModel("Missing", 1, 100); Assert.IsNull(aircraft); } [TestMethod] public void ListByMissingManufacturerTest() { - IEnumerable aircraft = _factory.Aircraft.ListByManufacturer("Missing"); + IEnumerable aircraft = _factory.Aircraft.ListByManufacturer("Missing", 1, 100); Assert.IsNull(aircraft); } @@ -191,7 +191,7 @@ public void ListByMissingManufacturerTest() public void ListByManufacturerWithNoModelsTest() { _factory.Manufacturers.Add("Boeing"); - IEnumerable aircraft = _factory.Aircraft.ListByManufacturer("Boeing"); + IEnumerable aircraft = _factory.Aircraft.ListByManufacturer("Boeing", 1, 100); Assert.IsNull(aircraft); } } From b012484aefc23f9c85588b64e0b20074f2805624 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:19:58 +0100 Subject: [PATCH 2/7] Added paging to airline retrieval --- .../Logic/AircraftManager.cs | 8 +++++++ .../Logic/AirlineManager.cs | 22 ++++++++++++++----- .../Interfaces/IAirlineManager.cs | 4 ++-- .../AirlineManagerTest.cs | 12 +++++----- src/FlightRecorder.Tests/FlightManagerTest.cs | 2 +- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs index 1a11e5c..283e366 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs @@ -107,6 +107,8 @@ public IAsyncEnumerable ListAsync(Expression> pre /// Get the aircraft of a specified model /// /// + /// + /// /// public IEnumerable ListByModel(string modelName, int pageNumber, int pageSize) { @@ -126,6 +128,8 @@ public IEnumerable ListByModel(string modelName, int pageNumber, int p /// Get the aircraft of a specified model /// /// + /// + /// /// public async Task> ListByModelAsync(string modelName, int pageNumber, int pageSize) { @@ -145,6 +149,8 @@ public async Task> ListByModelAsync(string modelName, /// Get the aircraft manufactured by a given manufacturer /// /// + /// + /// /// public IEnumerable ListByManufacturer(string manufacturerName, int pageNumber, int pageSize) { @@ -171,6 +177,8 @@ public IEnumerable ListByManufacturer(string manufacturerName, int pag /// Get the aircraft manufactured by a given manufacturer /// /// + /// + /// /// public async Task> ListByManufacturerAsync(string manufacturerName, int pageNumber, int pageSize) { diff --git a/src/FlightRecorder.BusinessLogic/Logic/AirlineManager.cs b/src/FlightRecorder.BusinessLogic/Logic/AirlineManager.cs index 09f63e6..4d9588c 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/AirlineManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/AirlineManager.cs @@ -26,7 +26,7 @@ internal AirlineManager(FlightRecorderDbContext context) /// /// public Airline Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Get the first airline matching the specified criteria @@ -45,17 +45,24 @@ public async Task GetAsync(Expression> predicate) /// Return all entities matching the specified criteria /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable results; if (predicate == null) { results = _context.Airlines; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { - results = _context.Airlines.Where(predicate); + results = _context.Airlines + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); ; } return results; @@ -65,13 +72,18 @@ public IEnumerable List(Expression> predicate = nul /// Return all entities matching the specified criteria /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable results; if (predicate == null) { - results = _context.Airlines.AsAsyncEnumerable(); + results = _context.Airlines; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .AsAsyncEnumerable(); } else { diff --git a/src/FlightRecorder.Entities/Interfaces/IAirlineManager.cs b/src/FlightRecorder.Entities/Interfaces/IAirlineManager.cs index 030be02..ab3a5f5 100644 --- a/src/FlightRecorder.Entities/Interfaces/IAirlineManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/IAirlineManager.cs @@ -12,7 +12,7 @@ public interface IAirlineManager Task AddAsync(string name); Airline Get(Expression> predicate); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/AirlineManagerTest.cs b/src/FlightRecorder.Tests/AirlineManagerTest.cs index 1fd78bd..cc95840 100644 --- a/src/FlightRecorder.Tests/AirlineManagerTest.cs +++ b/src/FlightRecorder.Tests/AirlineManagerTest.cs @@ -28,7 +28,7 @@ public void TestInitialize() public void AddDuplicateTest() { _factory.Airlines.Add(EntityName); - Assert.AreEqual(1, _factory.Airlines.List().Count()); + Assert.AreEqual(1, _factory.Airlines.List(null, 1, 100).Count()); } [TestMethod] @@ -61,7 +61,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable entities = _factory.Airlines.List(); + IEnumerable entities = _factory.Airlines.List(null, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -70,7 +70,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List entities = await _factory.Airlines - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -79,7 +79,7 @@ public async Task ListAllAsyncTest() [TestMethod] public void FilteredListTest() { - IEnumerable entities = _factory.Airlines.List(e => e.Name == EntityName); + IEnumerable entities = _factory.Airlines.List(e => e.Name == EntityName, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -88,7 +88,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List entities = await _factory.Airlines - .ListAsync(e => e.Name == EntityName) + .ListAsync(e => e.Name == EntityName, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -97,7 +97,7 @@ public async Task FilteredListAsyncTest() [TestMethod] public void ListMissingTest() { - IEnumerable entities = _factory.Airlines.List(e => e.Name == "Missing"); + IEnumerable entities = _factory.Airlines.List(e => e.Name == "Missing", 1, 100); Assert.AreEqual(0, entities.Count()); } } diff --git a/src/FlightRecorder.Tests/FlightManagerTest.cs b/src/FlightRecorder.Tests/FlightManagerTest.cs index 55859ff..cd0b4f2 100644 --- a/src/FlightRecorder.Tests/FlightManagerTest.cs +++ b/src/FlightRecorder.Tests/FlightManagerTest.cs @@ -34,7 +34,7 @@ public void AddDuplicateTest() { _factory.Flights.Add(FlightNumber, Embarkation, Destination, AirlineName); Assert.AreEqual(1, _factory.Flights.List().Count()); - Assert.AreEqual(1, _factory.Airlines.List().Count()); + Assert.AreEqual(1, _factory.Airlines.List(null, 1, 100).Count()); } [TestMethod] From 20553095d7aa15b163ec90599ff96df0c57e512c Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:27:20 +0100 Subject: [PATCH 3/7] Added paging to flight retrieval --- .../Logic/FlightManager.cs | 34 ++++++++++++++----- .../Logic/SightingManager.cs | 12 ++++--- .../Interfaces/IFlightManager.cs | 8 ++--- src/FlightRecorder.Tests/FlightManagerTest.cs | 18 +++++----- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/FlightManager.cs b/src/FlightRecorder.BusinessLogic/Logic/FlightManager.cs index eece902..501b73e 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/FlightManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/FlightManager.cs @@ -26,7 +26,7 @@ internal FlightManager(FlightRecorderFactory factory) /// /// public Flight Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Get the first flight matching the specified criteria along with the associated airline @@ -45,21 +45,27 @@ public async Task GetAsync(Expression> predicate) /// Get the flights matching the specified criteria along with the associated airlines /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable flights; if (predicate == null) { flights = _factory.Context.Flights - .Include(m => m.Airline); + .Include(m => m.Airline) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { flights = _factory.Context.Flights .Include(m => m.Airline) - .Where(predicate); + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } return flights; @@ -69,8 +75,10 @@ public IEnumerable List(Expression> predicate = null) /// Get the flights matching the specified criteria along with the associated airlines /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable flights; @@ -78,6 +86,8 @@ public IAsyncEnumerable ListAsync(Expression> predica { flights = _factory.Context.Flights .Include(m => m.Airline) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } else @@ -85,6 +95,8 @@ public IAsyncEnumerable ListAsync(Expression> predica flights = _factory.Context.Flights .Include(m => m.Airline) .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } @@ -95,8 +107,10 @@ public IAsyncEnumerable ListAsync(Expression> predica /// Get the flights for a named airline /// /// + /// + /// /// - public IEnumerable ListByAirline(string airlineName) + public IEnumerable ListByAirline(string airlineName, int pageNumber, int pageSize) { IEnumerable matches = null; @@ -104,7 +118,7 @@ public IEnumerable ListByAirline(string airlineName) Airline airline = _factory.Airlines.Get(m => m.Name == airlineName); if (airline != null) { - matches = List(m => m.AirlineId == airline.Id); + matches = List(m => m.AirlineId == airline.Id, pageNumber, pageSize); } return matches; @@ -114,8 +128,10 @@ public IEnumerable ListByAirline(string airlineName) /// Get the flights for a named airline /// /// + /// + /// /// - public async Task> ListByAirlineAsync(string airlineName) + public async Task> ListByAirlineAsync(string airlineName, int pageNumber, int pageSize) { IAsyncEnumerable matches = null; @@ -124,7 +140,7 @@ public async Task> ListByAirlineAsync(string airlineNam .GetAsync(m => m.Name == airlineName); if (airline != null) { - matches = ListAsync(m => m.AirlineId == airline.Id); + matches = ListAsync(m => m.AirlineId == airline.Id, pageNumber, pageSize); } return matches; diff --git a/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs b/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs index 211e907..403442d 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs @@ -14,6 +14,8 @@ namespace FlightRecorder.BusinessLogic.Logic { internal class SightingManager : ISightingManager { + private const int AllFlightsPageSize = 1000000; + private FlightRecorderFactory _factory; internal SightingManager(FlightRecorderFactory factory) @@ -255,9 +257,11 @@ public IEnumerable ListByRoute(string embarkation, string destination) { IEnumerable sightings = null; + // Note that the "List" method uses an arbitrary maximum number of flights. This is + // more than enough to cover a hobbyist flight log! embarkation = embarkation.CleanString().ToUpper(); destination = destination.CleanString().ToUpper(); - IEnumerable flights = _factory.Flights.List(f => (f.Embarkation == embarkation) && (f.Destination == destination)); + IEnumerable flights = _factory.Flights.List(f => (f.Embarkation == embarkation) && (f.Destination == destination), 1, AllFlightsPageSize); if ((flights != null) && flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); @@ -281,7 +285,7 @@ public async Task> ListByRouteAsync(string embarkatio destination = destination.CleanString().ToUpper(); List flights = await _factory.Flights .ListAsync(f => (f.Embarkation == embarkation) && - (f.Destination == destination)) + (f.Destination == destination), 1, AllFlightsPageSize) .ToListAsync(); if (flights.Any()) { @@ -302,7 +306,7 @@ public IEnumerable ListByAirline(string airlineName) IEnumerable sightings = null; airlineName = airlineName.CleanString(); - IEnumerable flights = _factory.Flights.ListByAirline(airlineName); + IEnumerable flights = _factory.Flights.ListByAirline(airlineName, 1, AllFlightsPageSize); if ((flights != null) && flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); @@ -322,7 +326,7 @@ public async Task> ListByAirlineAsync(string airlineN IAsyncEnumerable sightings = null; airlineName = airlineName.CleanString(); - IAsyncEnumerable matches = await _factory.Flights.ListByAirlineAsync(airlineName); + IAsyncEnumerable matches = await _factory.Flights.ListByAirlineAsync(airlineName, 1, AllFlightsPageSize); if (matches != null) { List flights = await matches.ToListAsync(); diff --git a/src/FlightRecorder.Entities/Interfaces/IFlightManager.cs b/src/FlightRecorder.Entities/Interfaces/IFlightManager.cs index 5476f10..61a6da0 100644 --- a/src/FlightRecorder.Entities/Interfaces/IFlightManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/IFlightManager.cs @@ -12,9 +12,9 @@ public interface IFlightManager Task AddAsync(string number, string embarkation, string destination, string airlineName); Flight Get(Expression> predicate = null); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); - IEnumerable ListByAirline(string airlineName); - Task> ListByAirlineAsync(string airlineName); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); + IEnumerable ListByAirline(string airlineName, int pageNumber, int pageSize); + Task> ListByAirlineAsync(string airlineName, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/FlightManagerTest.cs b/src/FlightRecorder.Tests/FlightManagerTest.cs index cd0b4f2..1f504f5 100644 --- a/src/FlightRecorder.Tests/FlightManagerTest.cs +++ b/src/FlightRecorder.Tests/FlightManagerTest.cs @@ -33,7 +33,7 @@ public void TestInitialize() public void AddDuplicateTest() { _factory.Flights.Add(FlightNumber, Embarkation, Destination, AirlineName); - Assert.AreEqual(1, _factory.Flights.List().Count()); + Assert.AreEqual(1, _factory.Flights.List(null, 1, 100).Count()); Assert.AreEqual(1, _factory.Airlines.List(null, 1, 100).Count()); } @@ -80,7 +80,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable flights = _factory.Flights.List(); + IEnumerable flights = _factory.Flights.List(null, 1, 100); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); Assert.AreEqual(AirlineName, flights.First().Airline.Name); @@ -90,7 +90,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List flights = await _factory.Flights - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); @@ -100,7 +100,7 @@ public async Task ListAllAsyncTest() [TestMethod] public void FilteredListTest() { - IEnumerable flights = _factory.Flights.List(e => e.Number == FlightNumber); + IEnumerable flights = _factory.Flights.List(e => e.Number == FlightNumber, 1, 100); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); Assert.AreEqual(AirlineName, flights.First().Airline.Name); @@ -110,7 +110,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List flights = await _factory.Flights - .ListAsync(e => e.Number == FlightNumber) + .ListAsync(e => e.Number == FlightNumber, 1, 100) .ToListAsync(); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); @@ -120,14 +120,14 @@ public async Task FilteredListAsyncTest() [TestMethod] public void ListMissingTest() { - IEnumerable flights = _factory.Flights.List(e => e.Number == "Missing"); + IEnumerable flights = _factory.Flights.List(e => e.Number == "Missing", 1, 100); Assert.AreEqual(0, flights.Count()); } [TestMethod] public void ListByAirlineTest() { - IEnumerable flights = _factory.Flights.ListByAirline(AirlineName); + IEnumerable flights = _factory.Flights.ListByAirline(AirlineName, 1, 100); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); Assert.AreEqual(AirlineName, flights.First().Airline.Name); @@ -137,7 +137,7 @@ public void ListByAirlineTest() public async Task ListByAirlineAsyncTest() { IAsyncEnumerable matches = await _factory.Flights - .ListByAirlineAsync(AirlineName); + .ListByAirlineAsync(AirlineName, 1, 100); List flights = await matches.ToListAsync(); Assert.AreEqual(1, flights.Count()); Assert.AreEqual(FlightNumber, flights.First().Number); @@ -147,7 +147,7 @@ public async Task ListByAirlineAsyncTest() [TestMethod] public void ListByMissingAirlineTest() { - IEnumerable flights = _factory.Flights.ListByAirline("Missing"); + IEnumerable flights = _factory.Flights.ListByAirline("Missing", 1, 100); Assert.IsNull(flights); } } From 399b8da25ac51594d871e561748e2ebade83baa9 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:34:43 +0100 Subject: [PATCH 4/7] Added paging to location retrieval --- .../Logic/AircraftManager.cs | 8 ++++-- .../Logic/LocationManager.cs | 28 +++++++++++++++---- .../Interfaces/ILocationManager.cs | 4 +-- .../LocationManagerTest.cs | 12 ++++---- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs index 283e366..4d0f232 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs @@ -56,14 +56,18 @@ public IEnumerable List(Expression> predicate, in { aircraft = _factory.Context.Aircraft .Include(a => a.Model) - .ThenInclude(m => m.Manufacturer); + .ThenInclude(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { aircraft = _factory.Context.Aircraft .Include(a => a.Model) .ThenInclude(m => m.Manufacturer) - .Where(predicate); + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } return aircraft; diff --git a/src/FlightRecorder.BusinessLogic/Logic/LocationManager.cs b/src/FlightRecorder.BusinessLogic/Logic/LocationManager.cs index e2f2820..d4a7a1e 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/LocationManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/LocationManager.cs @@ -26,7 +26,7 @@ internal LocationManager(FlightRecorderDbContext context) /// /// public Location Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Return the first entity matching the specified criteria @@ -45,17 +45,24 @@ public async Task GetAsync(Expression> predicate) /// Return all entities matching the specified criteria /// /// + /// + /// /// - public virtual IEnumerable List(Expression> predicate = null) + public virtual IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable results; if (predicate == null) { results = _context.Locations; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { - results = _context.Locations.Where(predicate); + results = _context.Locations + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } return results; @@ -65,17 +72,26 @@ public virtual IEnumerable List(Expression> predi /// Return all entities matching the specified criteria /// /// + /// + /// /// - public virtual IAsyncEnumerable ListAsync(Expression> predicate = null) + public virtual IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable results; if (predicate == null) { - results = _context.Locations.AsAsyncEnumerable(); + results = _context.Locations; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .AsAsyncEnumerable(); } else { - results = _context.Locations.Where(predicate).AsAsyncEnumerable(); + results = _context.Locations + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .AsAsyncEnumerable(); } return results; diff --git a/src/FlightRecorder.Entities/Interfaces/ILocationManager.cs b/src/FlightRecorder.Entities/Interfaces/ILocationManager.cs index 0aa5e7b..fb48c0a 100644 --- a/src/FlightRecorder.Entities/Interfaces/ILocationManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/ILocationManager.cs @@ -12,7 +12,7 @@ public interface ILocationManager Task AddAsync(string name); Location Get(Expression> predicate); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/LocationManagerTest.cs b/src/FlightRecorder.Tests/LocationManagerTest.cs index 93c0160..ab0bbe4 100644 --- a/src/FlightRecorder.Tests/LocationManagerTest.cs +++ b/src/FlightRecorder.Tests/LocationManagerTest.cs @@ -28,7 +28,7 @@ public void TestInitialize() public void AddDuplicateTest() { _factory.Locations.Add(EntityName); - Assert.AreEqual(1, _factory.Locations.List().Count()); + Assert.AreEqual(1, _factory.Locations.List(null, 1, 100).Count()); } [TestMethod] @@ -60,7 +60,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable entities = _factory.Locations.List(); + IEnumerable entities = _factory.Locations.List(null, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -69,7 +69,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List entities = await _factory.Locations - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -78,7 +78,7 @@ public async Task ListAllAsyncTest() [TestMethod] public void FilteredListTest() { - IEnumerable entities = _factory.Locations.List(e => e.Name == EntityName); + IEnumerable entities = _factory.Locations.List(e => e.Name == EntityName, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -87,7 +87,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List entities = await _factory.Locations - .ListAsync(e => e.Name == EntityName) + .ListAsync(e => e.Name == EntityName, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -96,7 +96,7 @@ public async Task FilteredListAsyncTest() [TestMethod] public void ListMissingTest() { - IEnumerable entities = _factory.Locations.List(e => e.Name == "Missing"); + IEnumerable entities = _factory.Locations.List(e => e.Name == "Missing", 1, 100); Assert.AreEqual(0, entities.Count()); } } From 6d52dade2f76cdb52080abc07e3e428d79b28421 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:39:16 +0100 Subject: [PATCH 5/7] Added paging to manufacturer retrieval --- .../Logic/ManufacturerManager.cs | 26 +++++++++++++++---- .../Interfaces/IManufacturerManager.cs | 4 +-- src/FlightRecorder.Tests/ManufacturerTest.cs | 12 ++++----- src/FlightRecorder.Tests/ModelManagerTest.cs | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/ManufacturerManager.cs b/src/FlightRecorder.BusinessLogic/Logic/ManufacturerManager.cs index 20616ca..8680a57 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/ManufacturerManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/ManufacturerManager.cs @@ -45,17 +45,24 @@ public async Task GetAsync(Expression> pr /// Return all entities matching the specified criteria /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable results; if (predicate == null) { results = _context.Manufacturers; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { - results = _context.Manufacturers.Where(predicate); + results = _context.Manufacturers + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } return results; @@ -65,17 +72,26 @@ public IEnumerable List(Expression> predi /// Return all entities matching the specified criteria /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable results; if (predicate == null) { - results = _context.Manufacturers.AsAsyncEnumerable(); + results = _context.Manufacturers; + results = results.Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .AsAsyncEnumerable(); } else { - results = _context.Manufacturers.Where(predicate).AsAsyncEnumerable(); + results = _context.Manufacturers + .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .AsAsyncEnumerable(); } return results; diff --git a/src/FlightRecorder.Entities/Interfaces/IManufacturerManager.cs b/src/FlightRecorder.Entities/Interfaces/IManufacturerManager.cs index c0dcc66..d2ba3cf 100644 --- a/src/FlightRecorder.Entities/Interfaces/IManufacturerManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/IManufacturerManager.cs @@ -12,7 +12,7 @@ public interface IManufacturerManager Task AddAsync(string name); Manufacturer Get(Expression> predicate); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/ManufacturerTest.cs b/src/FlightRecorder.Tests/ManufacturerTest.cs index bac4e69..939ae95 100644 --- a/src/FlightRecorder.Tests/ManufacturerTest.cs +++ b/src/FlightRecorder.Tests/ManufacturerTest.cs @@ -28,7 +28,7 @@ public void TestInitialize() public void AddDuplicateTest() { _factory.Manufacturers.Add(EntityName); - Assert.AreEqual(1, _factory.Manufacturers.List().Count()); + Assert.AreEqual(1, _factory.Manufacturers.List(null, 1, 100).Count()); } [TestMethod] @@ -64,7 +64,7 @@ public void GetMissingTest() public void ListAllTest() { IEnumerable entities = _factory.Manufacturers - .List(); + .List(null, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -73,7 +73,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List entities = await _factory.Manufacturers - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -83,7 +83,7 @@ public async Task ListAllAsyncTest() public void FilteredListTest() { IEnumerable entities = _factory.Manufacturers - .List(e => e.Name == EntityName); + .List(e => e.Name == EntityName, 1, 100); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); } @@ -92,7 +92,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List entities = await _factory.Manufacturers - .ListAsync(e => e.Name == EntityName) + .ListAsync(e => e.Name == EntityName, 1, 100) .ToListAsync(); Assert.AreEqual(1, entities.Count()); Assert.AreEqual(EntityName, entities.First().Name); @@ -102,7 +102,7 @@ public async Task FilteredListAsyncTest() public void ListMissingTest() { IEnumerable entities = _factory.Manufacturers - .List(e => e.Name == "Missing"); + .List(e => e.Name == "Missing", 1, 100); Assert.AreEqual(0, entities.Count()); } } diff --git a/src/FlightRecorder.Tests/ModelManagerTest.cs b/src/FlightRecorder.Tests/ModelManagerTest.cs index 55ee35d..03ff59b 100644 --- a/src/FlightRecorder.Tests/ModelManagerTest.cs +++ b/src/FlightRecorder.Tests/ModelManagerTest.cs @@ -30,7 +30,7 @@ public void AddDuplicateTest() { _factory.Models.Add(ModelName, ManufacturerName); Assert.AreEqual(1, _factory.Models.List().Count()); - Assert.AreEqual(1, _factory.Manufacturers.List().Count()); + Assert.AreEqual(1, _factory.Manufacturers.List(null, 1, 100).Count()); } [TestMethod] From 8d476747c497246e666820969e6f6a498d714e63 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:45:14 +0100 Subject: [PATCH 6/7] Added paging to model retrieval --- .../Logic/AircraftManager.cs | 8 +++-- .../Logic/ModelManager.cs | 34 +++++++++++++++---- .../Interfaces/IModelManager.cs | 8 ++--- src/FlightRecorder.Tests/ModelManagerTest.cs | 18 +++++----- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs index 4d0f232..6573bc0 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/AircraftManager.cs @@ -13,6 +13,8 @@ namespace FlightRecorder.BusinessLogic.Logic { internal class AircraftManager : IAircraftManager { + private const int AllModelsPageSize = 1000000; + private FlightRecorderFactory _factory; internal AircraftManager(FlightRecorderFactory factory) @@ -165,8 +167,9 @@ public IEnumerable ListByManufacturer(string manufacturerName, int pag .Get(m => m.Name == manufacturerName); if (manufacturer != null) { + // Model retrieval uses an arbitrarily large page size to retrieve all models IEnumerable modelIds = _factory.Models - .List(m => m.ManufacturerId == manufacturer.Id) + .List(m => m.ManufacturerId == manufacturer.Id, 1, AllModelsPageSize) .Select(m => m.Id); if (modelIds.Any()) { @@ -193,8 +196,9 @@ public async Task> ListByManufacturerAsync(string man .GetAsync(m => m.Name == manufacturerName); if (manufacturer != null) { + // Model retrieval uses an arbitrarily large page size to retrieve all models List modelIds = await _factory.Models - .ListAsync(m => m.ManufacturerId == manufacturer.Id) + .ListAsync(m => m.ManufacturerId == manufacturer.Id, 1, AllModelsPageSize) .Select(m => m.Id) .ToListAsync(); if (modelIds.Any()) diff --git a/src/FlightRecorder.BusinessLogic/Logic/ModelManager.cs b/src/FlightRecorder.BusinessLogic/Logic/ModelManager.cs index d599c3b..c18ced6 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/ModelManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/ModelManager.cs @@ -26,7 +26,7 @@ internal ModelManager(FlightRecorderFactory factory) /// /// public Model Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Get the first model matching the specified criteria along with the associated manufacturer @@ -45,20 +45,26 @@ public async Task GetAsync(Expression> predicate) /// Get the models matching the specified criteria along with the associated manufacturers /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable models; if (predicate == null) { models = _factory.Context.Models - .Include(m => m.Manufacturer); + .Include(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { models = _factory.Context.Models .Include(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .Where(predicate); } @@ -69,8 +75,10 @@ public IEnumerable List(Expression> predicate = null) /// Get the models matching the specified criteria along with the associated manufacturers /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable models; @@ -78,6 +86,8 @@ public IAsyncEnumerable ListAsync(Expression> predicate { models = _factory.Context.Models .Include(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } else @@ -85,6 +95,8 @@ public IAsyncEnumerable ListAsync(Expression> predicate models = _factory.Context.Models .Include(m => m.Manufacturer) .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } @@ -95,13 +107,17 @@ public IAsyncEnumerable ListAsync(Expression> predicate /// Get the models for a named manufacturer /// /// + /// + /// /// - public IEnumerable ListByManufacturer(string manufacturerName) + public IEnumerable ListByManufacturer(string manufacturerName, int pageNumber, int pageSize) { manufacturerName = manufacturerName.CleanString(); IEnumerable models = _factory.Context.Models .Include(m => m.Manufacturer) - .Where(m => m.Manufacturer.Name == manufacturerName); + .Where(m => m.Manufacturer.Name == manufacturerName) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); return models; } @@ -109,13 +125,17 @@ public IEnumerable ListByManufacturer(string manufacturerName) /// Get the models for a named manufacturer /// /// + /// + /// /// - public IAsyncEnumerable ListByManufacturerAsync(string manufacturerName) + public IAsyncEnumerable ListByManufacturerAsync(string manufacturerName, int pageNumber, int pageSize) { manufacturerName = manufacturerName.CleanString(); IAsyncEnumerable models = _factory.Context.Models .Include(m => m.Manufacturer) .Where(m => m.Manufacturer.Name == manufacturerName) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); return models; } diff --git a/src/FlightRecorder.Entities/Interfaces/IModelManager.cs b/src/FlightRecorder.Entities/Interfaces/IModelManager.cs index a08eaa0..5bc99d6 100644 --- a/src/FlightRecorder.Entities/Interfaces/IModelManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/IModelManager.cs @@ -12,9 +12,9 @@ public interface IModelManager Task AddAsync(string name, string manufacturerName); Model Get(Expression> predicate); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); - IEnumerable ListByManufacturer(string manufacturerName); - IAsyncEnumerable ListByManufacturerAsync(string manufacturerName); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); + IEnumerable ListByManufacturer(string manufacturerName, int pageNumber, int pageSize); + IAsyncEnumerable ListByManufacturerAsync(string manufacturerName, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/ModelManagerTest.cs b/src/FlightRecorder.Tests/ModelManagerTest.cs index 03ff59b..38c4f52 100644 --- a/src/FlightRecorder.Tests/ModelManagerTest.cs +++ b/src/FlightRecorder.Tests/ModelManagerTest.cs @@ -29,7 +29,7 @@ public void TestInitialize() public void AddDuplicateTest() { _factory.Models.Add(ModelName, ManufacturerName); - Assert.AreEqual(1, _factory.Models.List().Count()); + Assert.AreEqual(1, _factory.Models.List(null, 1, 100).Count()); Assert.AreEqual(1, _factory.Manufacturers.List(null, 1, 100).Count()); } @@ -72,7 +72,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable models = _factory.Models.List(); + IEnumerable models = _factory.Models.List(null, 1, 100); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); Assert.AreEqual(ManufacturerName, models.First().Manufacturer.Name); @@ -82,7 +82,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List models = await _factory.Models - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); @@ -93,7 +93,7 @@ public async Task ListAllAsyncTest() public void FilteredListTest() { IEnumerable models = _factory.Models - .List(e => e.Name == ModelName); + .List(e => e.Name == ModelName, 1, 100); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); Assert.AreEqual(ManufacturerName, models.First().Manufacturer.Name); @@ -103,7 +103,7 @@ public void FilteredListTest() public async Task FilteredListAsyncTest() { List models = await _factory.Models - .ListAsync(e => e.Name == ModelName) + .ListAsync(e => e.Name == ModelName, 1, 100) .ToListAsync(); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); @@ -113,14 +113,14 @@ public async Task FilteredListAsyncTest() [TestMethod] public void ListMissingTest() { - IEnumerable models = _factory.Models.List(e => e.Name == "Missing"); + IEnumerable models = _factory.Models.List(e => e.Name == "Missing", 1, 100); Assert.AreEqual(0, models.Count()); } [TestMethod] public void ListByManufacturerTest() { - IEnumerable models = _factory.Models.ListByManufacturer(ManufacturerName); + IEnumerable models = _factory.Models.ListByManufacturer(ManufacturerName, 1, 100); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); Assert.AreEqual(ManufacturerName, models.First().Manufacturer.Name); @@ -130,7 +130,7 @@ public void ListByManufacturerTest() public async Task ListByManufacturerAsyncTest() { List models = await _factory.Models - .ListByManufacturerAsync(ManufacturerName) + .ListByManufacturerAsync(ManufacturerName, 1, 100) .ToListAsync(); Assert.AreEqual(1, models.Count()); Assert.AreEqual(ModelName, models.First().Name); @@ -140,7 +140,7 @@ public async Task ListByManufacturerAsyncTest() [TestMethod] public void ListByMissingManufacturerTest() { - IEnumerable models = _factory.Models.ListByManufacturer("Missing"); + IEnumerable models = _factory.Models.ListByManufacturer("Missing", 1, 100); Assert.IsFalse(models.Any()); } } From 7f366c6356962ffded14715a73477245984e2fa9 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 14 Apr 2020 14:50:46 +0100 Subject: [PATCH 7/7] Added paging to sighting retrieval --- .../Logic/SightingManager.cs | 68 +++++++++++++------ .../Interfaces/ISightingManager.cs | 20 +++--- src/FlightRecorder.Tests/DataExchangeTests.cs | 2 +- .../SightingManagerTest.cs | 36 +++++----- 4 files changed, 77 insertions(+), 49 deletions(-) diff --git a/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs b/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs index 403442d..504ee2d 100644 --- a/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs +++ b/src/FlightRecorder.BusinessLogic/Logic/SightingManager.cs @@ -29,7 +29,7 @@ internal SightingManager(FlightRecorderFactory factory) /// /// public Sighting Get(Expression> predicate) - => List(predicate).FirstOrDefault(); + => List(predicate, 1, 1).FirstOrDefault(); /// /// Get the first sighting matching the specified criteria along with the associated entities @@ -48,8 +48,10 @@ public async Task GetAsync(Expression> predicate) /// Get the sightings matching the specified criteria along with the associated entities /// /// + /// + /// /// - public IEnumerable List(Expression> predicate = null) + public IEnumerable List(Expression> predicate, int pageNumber, int pageSize) { IEnumerable sightings; @@ -61,7 +63,9 @@ public IEnumerable List(Expression> predicate = n .ThenInclude(f => f.Airline) .Include(s => s.Aircraft) .ThenInclude(a => a.Model) - .ThenInclude(m => m.Manufacturer); + .ThenInclude(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize); } else { @@ -72,6 +76,8 @@ public IEnumerable List(Expression> predicate = n .Include(s => s.Aircraft) .ThenInclude(a => a.Model) .ThenInclude(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .Where(predicate); } @@ -82,8 +88,10 @@ public IEnumerable List(Expression> predicate = n /// Get the sightings matching the specified criteria along with the associated entities /// /// + /// + /// /// - public IAsyncEnumerable ListAsync(Expression> predicate = null) + public IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize) { IAsyncEnumerable sightings; @@ -96,6 +104,8 @@ public IAsyncEnumerable ListAsync(Expression> pre .Include(s => s.Aircraft) .ThenInclude(a => a.Model) .ThenInclude(m => m.Manufacturer) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } else @@ -108,6 +118,8 @@ public IAsyncEnumerable ListAsync(Expression> pre .ThenInclude(a => a.Model) .ThenInclude(m => m.Manufacturer) .Where(predicate) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) .AsAsyncEnumerable(); } @@ -213,8 +225,10 @@ public async Task AddAsync(FlattenedSighting flattened) /// Return a list of sightings of a specified aircraft /// /// + /// + /// /// - public IEnumerable ListByAircraft(string registration) + public IEnumerable ListByAircraft(string registration, int pageNumber, int pageSize) { IEnumerable sightings = null; @@ -222,7 +236,7 @@ public IEnumerable ListByAircraft(string registration) Aircraft aircraft = _factory.Aircraft.Get(a => a.Registration == registration); if (aircraft != null) { - sightings = List(s => s.AircraftId == aircraft.Id); + sightings = List(s => s.AircraftId == aircraft.Id, pageNumber, pageSize); } return sightings; @@ -232,8 +246,10 @@ public IEnumerable ListByAircraft(string registration) /// Return a list of sightings of a specified aircraft /// /// + /// + /// /// - public async Task> ListByAircraftAsync(string registration) + public async Task> ListByAircraftAsync(string registration, int pageNumber, int pageSize) { IAsyncEnumerable sightings = null; @@ -241,7 +257,7 @@ public async Task> ListByAircraftAsync(string registr Aircraft aircraft = await _factory.Aircraft.GetAsync(a => a.Registration == registration); if (aircraft != null) { - sightings = ListAsync(s => s.AircraftId == aircraft.Id); + sightings = ListAsync(s => s.AircraftId == aircraft.Id, pageNumber, pageSize); } return sightings; @@ -252,8 +268,10 @@ public async Task> ListByAircraftAsync(string registr /// /// /// + /// + /// /// - public IEnumerable ListByRoute(string embarkation, string destination) + public IEnumerable ListByRoute(string embarkation, string destination, int pageNumber, int pageSize) { IEnumerable sightings = null; @@ -265,7 +283,7 @@ public IEnumerable ListByRoute(string embarkation, string destination) if ((flights != null) && flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); - sightings = List(s => flightIds.Contains(s.FlightId)); + sightings = List(s => flightIds.Contains(s.FlightId), pageNumber, pageSize); } return sightings; @@ -276,8 +294,10 @@ public IEnumerable ListByRoute(string embarkation, string destination) /// /// /// + /// + /// /// - public async Task> ListByRouteAsync(string embarkation, string destination) + public async Task> ListByRouteAsync(string embarkation, string destination, int pageNumber, int pageSize) { IAsyncEnumerable sightings = null; @@ -290,7 +310,7 @@ public async Task> ListByRouteAsync(string embarkatio if (flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); - sightings = ListAsync(s => flightIds.Contains(s.FlightId)); + sightings = ListAsync(s => flightIds.Contains(s.FlightId), pageNumber, pageSize); } return sightings; @@ -300,8 +320,10 @@ public async Task> ListByRouteAsync(string embarkatio /// Return a list of sightings for a specified airline /// /// + /// + /// /// - public IEnumerable ListByAirline(string airlineName) + public IEnumerable ListByAirline(string airlineName, int pageNumber, int pageSize) { IEnumerable sightings = null; @@ -310,7 +332,7 @@ public IEnumerable ListByAirline(string airlineName) if ((flights != null) && flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); - sightings = List(s => flightIds.Contains(s.FlightId)); + sightings = List(s => flightIds.Contains(s.FlightId), pageNumber, pageSize); } return sightings; @@ -320,8 +342,10 @@ public IEnumerable ListByAirline(string airlineName) /// Return a list of sightings for a specified airline /// /// + /// + /// /// - public async Task> ListByAirlineAsync(string airlineName) + public async Task> ListByAirlineAsync(string airlineName, int pageNumber, int pageSize) { IAsyncEnumerable sightings = null; @@ -333,7 +357,7 @@ public async Task> ListByAirlineAsync(string airlineN if (flights.Any()) { IEnumerable flightIds = flights.Select(f => f.Id); - sightings = ListAsync(s => flightIds.Contains(s.FlightId)); + sightings = ListAsync(s => flightIds.Contains(s.FlightId), pageNumber, pageSize); } } @@ -344,8 +368,10 @@ public async Task> ListByAirlineAsync(string airlineN /// Return a list of sightings at a specified location /// /// + /// + /// /// - public IEnumerable ListByLocation(string locationName) + public IEnumerable ListByLocation(string locationName, int pageNumber, int pageSize) { IEnumerable sightings = null; @@ -353,7 +379,7 @@ public IEnumerable ListByLocation(string locationName) Location location = _factory.Locations.Get(a => a.Name == locationName); if (location != null) { - sightings = List(s => s.LocationId == location.Id); + sightings = List(s => s.LocationId == location.Id, pageNumber, pageSize); } return sightings; @@ -363,8 +389,10 @@ public IEnumerable ListByLocation(string locationName) /// Return a list of sightings at a specified location /// /// + /// + /// /// - public async Task> ListByLocationAsync(string locationName) + public async Task> ListByLocationAsync(string locationName, int pageNumber, int pageSize) { IAsyncEnumerable sightings = null; @@ -372,7 +400,7 @@ public async Task> ListByLocationAsync(string locatio Location location = await _factory.Locations.GetAsync(a => a.Name == locationName); if (location != null) { - sightings = ListAsync(s => s.LocationId == location.Id); + sightings = ListAsync(s => s.LocationId == location.Id, pageNumber, pageSize); } return sightings; diff --git a/src/FlightRecorder.Entities/Interfaces/ISightingManager.cs b/src/FlightRecorder.Entities/Interfaces/ISightingManager.cs index ca3d858..a0c998e 100644 --- a/src/FlightRecorder.Entities/Interfaces/ISightingManager.cs +++ b/src/FlightRecorder.Entities/Interfaces/ISightingManager.cs @@ -15,15 +15,15 @@ public interface ISightingManager Task AddAsync(FlattenedSighting flattened); Sighting Get(Expression> predicate = null); Task GetAsync(Expression> predicate); - IEnumerable List(Expression> predicate = null); - IAsyncEnumerable ListAsync(Expression> predicate = null); - IEnumerable ListByAircraft(string registration); - Task> ListByAircraftAsync(string registration); - IEnumerable ListByRoute(string embarkation, string destination); - Task> ListByRouteAsync(string embarkation, string destination); - IEnumerable ListByAirline(string airlineName); - Task> ListByAirlineAsync(string airlineName); - IEnumerable ListByLocation(string locationName); - Task> ListByLocationAsync(string locationName); + IEnumerable List(Expression> predicate, int pageNumber, int pageSize); + IAsyncEnumerable ListAsync(Expression> predicate, int pageNumber, int pageSize); + IEnumerable ListByAircraft(string registration, int pageNumber, int pageSize); + Task> ListByAircraftAsync(string registration, int pageNumber, int pageSize); + IEnumerable ListByRoute(string embarkation, string destination, int pageNumber, int pageSize); + Task> ListByRouteAsync(string embarkation, string destination, int pageNumber, int pageSize); + IEnumerable ListByAirline(string airlineName, int pageNumber, int pageSize); + Task> ListByAirlineAsync(string airlineName, int pageNumber, int pageSize); + IEnumerable ListByLocation(string locationName, int pageNumber, int pageSize); + Task> ListByLocationAsync(string locationName, int pageNumber, int pageSize); } } \ No newline at end of file diff --git a/src/FlightRecorder.Tests/DataExchangeTests.cs b/src/FlightRecorder.Tests/DataExchangeTests.cs index 29b6aa5..5ec7f98 100644 --- a/src/FlightRecorder.Tests/DataExchangeTests.cs +++ b/src/FlightRecorder.Tests/DataExchangeTests.cs @@ -89,7 +89,7 @@ public void AddAndGetTest() [TestMethod] public void FlattenCollectionTest() { - IEnumerable flattened = _factory.Sightings.List().Flatten(); + IEnumerable flattened = _factory.Sightings.List(null, 1, 100).Flatten(); Assert.AreEqual(1, flattened.Count()); Assert.AreEqual(FlightNumber, flattened.First().FlightNumber); Assert.AreEqual(AirlineName, flattened.First().Airline); diff --git a/src/FlightRecorder.Tests/SightingManagerTest.cs b/src/FlightRecorder.Tests/SightingManagerTest.cs index 9ac6c41..b0737ae 100644 --- a/src/FlightRecorder.Tests/SightingManagerTest.cs +++ b/src/FlightRecorder.Tests/SightingManagerTest.cs @@ -130,7 +130,7 @@ public void GetMissingTest() [TestMethod] public void ListAllTest() { - IEnumerable sightings = _factory.Sightings.List(); + IEnumerable sightings = _factory.Sightings.List(null, 1, 100); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); } @@ -139,7 +139,7 @@ public void ListAllTest() public async Task ListAllAsyncTest() { List sightings = await _factory.Sightings - .ListAsync() + .ListAsync(null, 1, 100) .ToListAsync(); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); @@ -148,7 +148,7 @@ public async Task ListAllAsyncTest() [TestMethod] public void ListByAircraftTest() { - IEnumerable sightings = _factory.Sightings.ListByAircraft(Registration); + IEnumerable sightings = _factory.Sightings.ListByAircraft(Registration, 1, 100); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); } @@ -157,7 +157,7 @@ public void ListByAircraftTest() public async Task ListByAircraftAsyncTest() { IAsyncEnumerable matches = await _factory.Sightings - .ListByAircraftAsync(Registration); + .ListByAircraftAsync(Registration, 1, 100); List sightings = await matches.ToListAsync(); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); @@ -167,21 +167,21 @@ public async Task ListByAircraftAsyncTest() public void ListByAircraftWithNoSightingsTest() { _factory.Aircraft.Add("G-EZEH", "2184", 2004, ModelName, ManufacturerName); - IEnumerable sightings = _factory.Sightings.ListByAircraft("G-EZEH"); + IEnumerable sightings = _factory.Sightings.ListByAircraft("G-EZEH", 1, 100); Assert.AreEqual(0, sightings.Count()); } [TestMethod] public void ListByMissingAircraftTest() { - IEnumerable sightings = _factory.Sightings.ListByAircraft("Missing"); + IEnumerable sightings = _factory.Sightings.ListByAircraft("Missing", 1, 100); Assert.IsNull(sightings); } [TestMethod] public void ListByRouteTest() { - IEnumerable sightings = _factory.Sightings.ListByRoute(Embarkation, Destination); + IEnumerable sightings = _factory.Sightings.ListByRoute(Embarkation, Destination, 1, 100); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); } @@ -190,7 +190,7 @@ public void ListByRouteTest() public async Task ListByRouteAsyncTest() { IAsyncEnumerable matches = await _factory.Sightings - .ListByRouteAsync(Embarkation, Destination); + .ListByRouteAsync(Embarkation, Destination, 1, 100); List sightings = await matches.ToListAsync(); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); @@ -200,21 +200,21 @@ public async Task ListByRouteAsyncTest() public void ListByRouteWithNoSightingsTest() { _factory.Flights.Add("BA92", "YYZ", "LHR", "British Airways"); - IEnumerable sightings = _factory.Sightings.ListByRoute("YYZ", "LHR"); + IEnumerable sightings = _factory.Sightings.ListByRoute("YYZ", "LHR", 1, 100); Assert.AreEqual(0, sightings.Count()); } [TestMethod] public void ListByMissingRoute() { - IEnumerable sightings = _factory.Sightings.ListByRoute("RMU", "MAN"); + IEnumerable sightings = _factory.Sightings.ListByRoute("RMU", "MAN", 1, 100); Assert.IsNull(sightings); } [TestMethod] public void ListByAirlineTest() { - IEnumerable sightings = _factory.Sightings.ListByAirline(AirlineName); + IEnumerable sightings = _factory.Sightings.ListByAirline(AirlineName, 1, 100); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); } @@ -223,7 +223,7 @@ public void ListByAirlineTest() public async Task ListByAirlineAsyncTest() { IAsyncEnumerable matches = await _factory.Sightings - .ListByAirlineAsync(AirlineName); + .ListByAirlineAsync(AirlineName, 1, 100); List sightings = await matches.ToListAsync(); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); @@ -233,21 +233,21 @@ public async Task ListByAirlineAsyncTest() public void ListByAirlineWithNoSightingsTest() { _factory.Airlines.Add("British Airways"); - IEnumerable sightings = _factory.Sightings.ListByAirline("British Airways"); + IEnumerable sightings = _factory.Sightings.ListByAirline("British Airways", 1, 100); Assert.IsNull(sightings); } [TestMethod] public void ListByMissingAirline() { - IEnumerable sightings = _factory.Sightings.ListByAirline("Missing"); + IEnumerable sightings = _factory.Sightings.ListByAirline("Missing", 1, 100); Assert.IsNull(sightings); } [TestMethod] public void ListByLocationTest() { - IEnumerable sightings = _factory.Sightings.ListByLocation(LocationName); + IEnumerable sightings = _factory.Sightings.ListByLocation(LocationName, 1, 100); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); } @@ -256,7 +256,7 @@ public void ListByLocationTest() public async Task ListByLocationAsyncTest() { IAsyncEnumerable matches = await _factory.Sightings - .ListByLocationAsync(LocationName); + .ListByLocationAsync(LocationName, 1, 100); List sightings = await matches.ToListAsync(); Assert.AreEqual(1, sightings.Count()); Assert.AreEqual(_sightingId, sightings.First().Id); @@ -266,14 +266,14 @@ public async Task ListByLocationAsyncTest() public void ListByLocationWithNoSightingsTest() { _factory.Airlines.Add("Gatwick Airport"); - IEnumerable sightings = _factory.Sightings.ListByLocation("Gatwick Airport"); + IEnumerable sightings = _factory.Sightings.ListByLocation("Gatwick Airport", 1, 100); Assert.IsNull(sightings); } [TestMethod] public void ListByMissingLocation() { - IEnumerable sightings = _factory.Sightings.ListByLocation("Missing"); + IEnumerable sightings = _factory.Sightings.ListByLocation("Missing", 1, 100); Assert.IsNull(sightings); } }