From ff853a89c6f9a6aeeccff680860a5d4bf07f6efa Mon Sep 17 00:00:00 2001 From: Marcus Wichelmann Date: Wed, 16 Oct 2019 12:17:01 +0200 Subject: [PATCH] Inspection and species retrieval implemented --- .../Controllers/Api/InspectionsController.cs | 121 ++++++------------ .../Controllers/Api/SpeciesController.cs | 24 +++- .../MappingProfiles/ModelMappingProfile.cs | 1 + Nesteo.Server/Models/InspectionPreview.cs | 43 +++++++ Nesteo.Server/Services/IInspectionService.cs | 14 ++ Nesteo.Server/Services/ISpeciesService.cs | 6 + .../Implementations/InspectionService.cs | 31 +++++ .../Implementations/SpeciesService.cs | 15 +++ Nesteo.Server/Startup.cs | 4 +- 9 files changed, 169 insertions(+), 90 deletions(-) create mode 100644 Nesteo.Server/Models/InspectionPreview.cs create mode 100644 Nesteo.Server/Services/IInspectionService.cs create mode 100644 Nesteo.Server/Services/ISpeciesService.cs create mode 100644 Nesteo.Server/Services/Implementations/InspectionService.cs create mode 100644 Nesteo.Server/Services/Implementations/SpeciesService.cs diff --git a/Nesteo.Server/Controllers/Api/InspectionsController.cs b/Nesteo.Server/Controllers/Api/InspectionsController.cs index cfaaea2..b70e6ae 100644 --- a/Nesteo.Server/Controllers/Api/InspectionsController.cs +++ b/Nesteo.Server/Controllers/Api/InspectionsController.cs @@ -2,108 +2,67 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Nesteo.Server.Data.Entities; using Nesteo.Server.Data.Enums; using Nesteo.Server.Models; +using Nesteo.Server.Services; namespace Nesteo.Server.Controllers.Api { [Route("api/v1/inspections")] public class InspectionsController : ApiControllerBase { + private readonly IInspectionService _inspectionService; + + public InspectionsController(IInspectionService inspectionService) + { + _inspectionService = inspectionService ?? throw new ArgumentNullException(nameof(inspectionService)); + } + /// /// Retrieve all inspections /// - // TODO: Use IAsyncEnumerable<> [HttpGet] - public Task>> GetInspectionsAsync() + public IAsyncEnumerable GetInspectionsAsync() { - return Task.FromResult>>(new List { - new Inspection { - Id = 0, - NestingBox = - new NestingBox { - Id = "F000001", - Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" }, - OldId = null, - ForeignId = "x234362", - CoordinateLongitude = -97.142212, - CoordinateLatitude = 30.081692, - HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12), - HangUpUser = null, - Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" }, - Material = Material.TreatedWood, - HoleSize = HoleSize.Large, - ImageFileName = null, - Comment = "This is a test", - LastUpdated = DateTime.UtcNow - }, - InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12), - InspectedByUser = null, - HasBeenCleaned = false, - Condition = Condition.Good, - JustRepaired = false, - Occupied = true, - ContainsEggs = true, - EggCount = 0, - ChickCount = 5, - RingedChickCount = 4, - AgeInDays = 6, - FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged, - MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged, - Species = new Species { Id = 0, Name = "Dodo" }, - ImageFileName = null, - Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!", - LastUpdated = DateTime.UtcNow - } - }); + return _inspectionService.GetAllAsync(); } /// /// Retrieve an inspection by id /// [HttpGet("{id}")] - public Task> GetInspectionByIdAsync(int id) + public async Task> GetInspectionByIdAsync(int id) + { + // Retrieve inspection + Inspection inspection = await _inspectionService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false); + if (inspection == null) + return NotFound(); + + return inspection; + } + + /// + /// Preview all inspections with a reduced set of data + /// + [HttpGet("previews")] + public IAsyncEnumerable GetInspectionPreviewsAsync() + { + return _inspectionService.GetAllPreviewsAsync(); + } + + /// + /// Preview an inspection by id with a reduced set of data + /// + [HttpGet("previews/{id}")] + public async Task> GetInspectionPreviewByIdAsync(int id) { - if (id != 0) - return Task.FromResult>(NotFound()); + // Retrieve inspection preview + InspectionPreview inspectionPreview = await _inspectionService.FindPreviewByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false); + if (inspectionPreview == null) + return NotFound(); - return Task.FromResult>(new Inspection { - Id = 0, - NestingBox = - new NestingBox { - Id = "F000001", - Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" }, - OldId = null, - ForeignId = "x234362", - CoordinateLongitude = -97.142212, - CoordinateLatitude = 30.081692, - HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12), - HangUpUser = null, - Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" }, - Material = Material.TreatedWood, - HoleSize = HoleSize.Large, - ImageFileName = null, - Comment = "This is a test", - LastUpdated = DateTime.UtcNow - }, - InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12), - InspectedByUser = null, - HasBeenCleaned = false, - Condition = Condition.Good, - JustRepaired = false, - Occupied = true, - ContainsEggs = true, - EggCount = 0, - ChickCount = 5, - RingedChickCount = 4, - AgeInDays = 6, - FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged, - MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged, - Species = new Species { Id = 0, Name = "Dodo" }, - ImageFileName = null, - Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!", - LastUpdated = DateTime.UtcNow - }); + return inspectionPreview; } } } diff --git a/Nesteo.Server/Controllers/Api/SpeciesController.cs b/Nesteo.Server/Controllers/Api/SpeciesController.cs index 3fadbb6..0284f86 100644 --- a/Nesteo.Server/Controllers/Api/SpeciesController.cs +++ b/Nesteo.Server/Controllers/Api/SpeciesController.cs @@ -1,33 +1,43 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Nesteo.Server.Models; +using Nesteo.Server.Services; namespace Nesteo.Server.Controllers.Api { [Route("api/v1/species")] public class SpeciesController : ApiControllerBase { + private readonly ISpeciesService _speciesService; + + public SpeciesController(ISpeciesService speciesService) + { + _speciesService = speciesService ?? throw new ArgumentNullException(nameof(speciesService)); + } + /// /// Retrieve all species /// - // TODO: Use IAsyncEnumerable<> [HttpGet] - public Task>> GetSpeciesAsync() + public IAsyncEnumerable GetSpeciesAsync() { - return Task.FromResult>>(new List { new Species { Id = 0, Name = "Dodo" } }); + return _speciesService.GetAllAsync(); } /// /// Retrieve a species by id /// [HttpGet("{id}")] - public Task> GetSpeciesByIdAsync(int id) + public async Task> GetSpeciesByIdAsync(int id) { - if (id != 0) - return Task.FromResult>(NotFound()); + // Retrieve species + Species species = await _speciesService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false); + if (species == null) + return NotFound(); - return Task.FromResult>(new Species { Id = 0, Name = "Dodo" }); + return species; } } } diff --git a/Nesteo.Server/MappingProfiles/ModelMappingProfile.cs b/Nesteo.Server/MappingProfiles/ModelMappingProfile.cs index efb1539..08fca39 100644 --- a/Nesteo.Server/MappingProfiles/ModelMappingProfile.cs +++ b/Nesteo.Server/MappingProfiles/ModelMappingProfile.cs @@ -26,6 +26,7 @@ public ModelMappingProfile() .OrderByDescending(inspection => inspection.InspectionDate) .FirstOrDefault().InspectionDate)); CreateMap(); + CreateMap(); } } } diff --git a/Nesteo.Server/Models/InspectionPreview.cs b/Nesteo.Server/Models/InspectionPreview.cs new file mode 100644 index 0000000..ea5d1ca --- /dev/null +++ b/Nesteo.Server/Models/InspectionPreview.cs @@ -0,0 +1,43 @@ +using System; +using Nesteo.Server.Data.Enums; + +namespace Nesteo.Server.Models +{ + public class InspectionPreview + { + /// + /// Inspection-ID + /// + public int Id { get; set; } + + /// + /// The id of the inspected nesting box + /// + public string NestingBoxId { get; set; } + + /// + /// Date and time of the inspection + /// + public DateTime InspectionDate { get; set; } + + /// + /// The condition in which the nesting box has been found + /// + public Condition Condition { get; set; } + + /// + /// Number of ringed chicks + /// + public int RingedChickCount { get; set; } + + /// + /// Information about the presence of the female parent bird + /// + public ParentBirdDiscovery FemaleParentBirdDiscovery { get; set; } + + /// + /// Information about the presence of the male parent bird + /// + public ParentBirdDiscovery MaleParentBirdDiscovery { get; set; } + } +} diff --git a/Nesteo.Server/Services/IInspectionService.cs b/Nesteo.Server/Services/IInspectionService.cs new file mode 100644 index 0000000..4f6d10a --- /dev/null +++ b/Nesteo.Server/Services/IInspectionService.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Nesteo.Server.Models; + +namespace Nesteo.Server.Services +{ + public interface IInspectionService: ICrudService + { + IAsyncEnumerable GetAllPreviewsAsync(); + + Task FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default); + } +} diff --git a/Nesteo.Server/Services/ISpeciesService.cs b/Nesteo.Server/Services/ISpeciesService.cs new file mode 100644 index 0000000..512e69d --- /dev/null +++ b/Nesteo.Server/Services/ISpeciesService.cs @@ -0,0 +1,6 @@ +using Nesteo.Server.Models; + +namespace Nesteo.Server.Services +{ + public interface ISpeciesService : ICrudService { } +} diff --git a/Nesteo.Server/Services/Implementations/InspectionService.cs b/Nesteo.Server/Services/Implementations/InspectionService.cs new file mode 100644 index 0000000..416067b --- /dev/null +++ b/Nesteo.Server/Services/Implementations/InspectionService.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoMapper; +using AutoMapper.QueryableExtensions; +using Microsoft.EntityFrameworkCore; +using Nesteo.Server.Data; +using Nesteo.Server.Data.Entities; +using Nesteo.Server.Models; + +namespace Nesteo.Server.Services.Implementations +{ + public class InspectionService : CrudServiceBase, IInspectionService + { + protected override DbSet Entities => DbContext.Inspections; + + public InspectionService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { } + + public IAsyncEnumerable GetAllPreviewsAsync() + { + return Entities.ProjectTo(Mapper.ConfigurationProvider).AsAsyncEnumerable(); + } + + public Task FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default) + { + return Entities.Where(entity => entity.Id.Equals(id)).ProjectTo(Mapper.ConfigurationProvider).FirstOrDefaultAsync(cancellationToken); + } + } +} diff --git a/Nesteo.Server/Services/Implementations/SpeciesService.cs b/Nesteo.Server/Services/Implementations/SpeciesService.cs new file mode 100644 index 0000000..3074f5d --- /dev/null +++ b/Nesteo.Server/Services/Implementations/SpeciesService.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using Microsoft.EntityFrameworkCore; +using Nesteo.Server.Data; +using Nesteo.Server.Data.Entities; +using Nesteo.Server.Models; + +namespace Nesteo.Server.Services.Implementations +{ + public class SpeciesService : CrudServiceBase, ISpeciesService + { + protected override DbSet Entities => DbContext.Species; + + public SpeciesService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { } + } +} diff --git a/Nesteo.Server/Startup.cs b/Nesteo.Server/Startup.cs index 4e81b3e..d62815c 100644 --- a/Nesteo.Server/Startup.cs +++ b/Nesteo.Server/Startup.cs @@ -115,9 +115,9 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); - // TODO: services.AddScoped(); + services.AddScoped(); services.AddScoped(); - // TODO: services.AddScoped(); + services.AddScoped(); } // This method gets called by the runtime and configures the HTTP request pipeline.