Skip to content

Commit

Permalink
Merge pull request #13 from Nesteo/inspection-retrieval
Browse files Browse the repository at this point in the history
Inspection and species retrieval implemented
  • Loading branch information
MarcusWichelmann authored Oct 16, 2019
2 parents 4b53ee7 + ff853a8 commit 46b1bf3
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 90 deletions.
121 changes: 40 additions & 81 deletions Nesteo.Server/Controllers/Api/InspectionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/// <summary>
/// Retrieve all inspections
/// </summary>
// TODO: Use IAsyncEnumerable<>
[HttpGet]
public Task<ActionResult<ICollection<Inspection>>> GetInspectionsAsync()
public IAsyncEnumerable<Inspection> GetInspectionsAsync()
{
return Task.FromResult<ActionResult<ICollection<Inspection>>>(new List<Inspection> {
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();
}

/// <summary>
/// Retrieve an inspection by id
/// </summary>
[HttpGet("{id}")]
public Task<ActionResult<Inspection>> GetInspectionByIdAsync(int id)
public async Task<ActionResult<Inspection>> GetInspectionByIdAsync(int id)
{
// Retrieve inspection
Inspection inspection = await _inspectionService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
if (inspection == null)
return NotFound();

return inspection;
}

/// <summary>
/// Preview all inspections with a reduced set of data
/// </summary>
[HttpGet("previews")]
public IAsyncEnumerable<InspectionPreview> GetInspectionPreviewsAsync()
{
return _inspectionService.GetAllPreviewsAsync();
}

/// <summary>
/// Preview an inspection by id with a reduced set of data
/// </summary>
[HttpGet("previews/{id}")]
public async Task<ActionResult<InspectionPreview>> GetInspectionPreviewByIdAsync(int id)
{
if (id != 0)
return Task.FromResult<ActionResult<Inspection>>(NotFound());
// Retrieve inspection preview
InspectionPreview inspectionPreview = await _inspectionService.FindPreviewByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
if (inspectionPreview == null)
return NotFound();

return Task.FromResult<ActionResult<Inspection>>(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;
}
}
}
24 changes: 17 additions & 7 deletions Nesteo.Server/Controllers/Api/SpeciesController.cs
Original file line number Diff line number Diff line change
@@ -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));
}

/// <summary>
/// Retrieve all species
/// </summary>
// TODO: Use IAsyncEnumerable<>
[HttpGet]
public Task<ActionResult<ICollection<Species>>> GetSpeciesAsync()
public IAsyncEnumerable<Species> GetSpeciesAsync()
{
return Task.FromResult<ActionResult<ICollection<Species>>>(new List<Species> { new Species { Id = 0, Name = "Dodo" } });
return _speciesService.GetAllAsync();
}

/// <summary>
/// Retrieve a species by id
/// </summary>
[HttpGet("{id}")]
public Task<ActionResult<Species>> GetSpeciesByIdAsync(int id)
public async Task<ActionResult<Species>> GetSpeciesByIdAsync(int id)
{
if (id != 0)
return Task.FromResult<ActionResult<Species>>(NotFound());
// Retrieve species
Species species = await _speciesService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
if (species == null)
return NotFound();

return Task.FromResult<ActionResult<Species>>(new Species { Id = 0, Name = "Dodo" });
return species;
}
}
}
1 change: 1 addition & 0 deletions Nesteo.Server/MappingProfiles/ModelMappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ModelMappingProfile()
.OrderByDescending(inspection => inspection.InspectionDate)
.FirstOrDefault().InspectionDate));
CreateMap<InspectionEntity, Inspection>();
CreateMap<InspectionEntity, InspectionPreview>();
}
}
}
43 changes: 43 additions & 0 deletions Nesteo.Server/Models/InspectionPreview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Nesteo.Server.Data.Enums;

namespace Nesteo.Server.Models
{
public class InspectionPreview
{
/// <summary>
/// Inspection-ID
/// </summary>
public int Id { get; set; }

/// <summary>
/// The id of the inspected nesting box
/// </summary>
public string NestingBoxId { get; set; }

/// <summary>
/// Date and time of the inspection
/// </summary>
public DateTime InspectionDate { get; set; }

/// <summary>
/// The condition in which the nesting box has been found
/// </summary>
public Condition Condition { get; set; }

/// <summary>
/// Number of ringed chicks
/// </summary>
public int RingedChickCount { get; set; }

/// <summary>
/// Information about the presence of the female parent bird
/// </summary>
public ParentBirdDiscovery FemaleParentBirdDiscovery { get; set; }

/// <summary>
/// Information about the presence of the male parent bird
/// </summary>
public ParentBirdDiscovery MaleParentBirdDiscovery { get; set; }
}
}
14 changes: 14 additions & 0 deletions Nesteo.Server/Services/IInspectionService.cs
Original file line number Diff line number Diff line change
@@ -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<Inspection, int>
{
IAsyncEnumerable<InspectionPreview> GetAllPreviewsAsync();

Task<InspectionPreview> FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default);
}
}
6 changes: 6 additions & 0 deletions Nesteo.Server/Services/ISpeciesService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Nesteo.Server.Models;

namespace Nesteo.Server.Services
{
public interface ISpeciesService : ICrudService<Species, int> { }
}
31 changes: 31 additions & 0 deletions Nesteo.Server/Services/Implementations/InspectionService.cs
Original file line number Diff line number Diff line change
@@ -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<InspectionEntity, Inspection, int>, IInspectionService
{
protected override DbSet<InspectionEntity> Entities => DbContext.Inspections;

public InspectionService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { }

public IAsyncEnumerable<InspectionPreview> GetAllPreviewsAsync()
{
return Entities.ProjectTo<InspectionPreview>(Mapper.ConfigurationProvider).AsAsyncEnumerable();
}

public Task<InspectionPreview> FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default)
{
return Entities.Where(entity => entity.Id.Equals(id)).ProjectTo<InspectionPreview>(Mapper.ConfigurationProvider).FirstOrDefaultAsync(cancellationToken);
}
}
}
15 changes: 15 additions & 0 deletions Nesteo.Server/Services/Implementations/SpeciesService.cs
Original file line number Diff line number Diff line change
@@ -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<SpeciesEntity, Species, int>, ISpeciesService
{
protected override DbSet<SpeciesEntity> Entities => DbContext.Species;

public SpeciesService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { }
}
}
4 changes: 2 additions & 2 deletions Nesteo.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<IUserService, UserService>();
services.AddScoped<IRegionService, RegionService>();
services.AddScoped<IOwnerService, OwnerService>();
// TODO: services.AddScoped<ISpeciesService, SpeciesService>();
services.AddScoped<ISpeciesService, SpeciesService>();
services.AddScoped<INestingBoxService, NestingBoxService>();
// TODO: services.AddScoped<IInspectionService, InspectionService>();
services.AddScoped<IInspectionService, InspectionService>();
}

// This method gets called by the runtime and configures the HTTP request pipeline.
Expand Down

0 comments on commit 46b1bf3

Please sign in to comment.