Skip to content

Commit 46b1bf3

Browse files
Merge pull request #13 from Nesteo/inspection-retrieval
Inspection and species retrieval implemented
2 parents 4b53ee7 + ff853a8 commit 46b1bf3

File tree

9 files changed

+169
-90
lines changed

9 files changed

+169
-90
lines changed

Nesteo.Server/Controllers/Api/InspectionsController.cs

Lines changed: 40 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,67 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Microsoft.AspNetCore.Mvc;
5+
using Nesteo.Server.Data.Entities;
56
using Nesteo.Server.Data.Enums;
67
using Nesteo.Server.Models;
8+
using Nesteo.Server.Services;
79

810
namespace Nesteo.Server.Controllers.Api
911
{
1012
[Route("api/v1/inspections")]
1113
public class InspectionsController : ApiControllerBase
1214
{
15+
private readonly IInspectionService _inspectionService;
16+
17+
public InspectionsController(IInspectionService inspectionService)
18+
{
19+
_inspectionService = inspectionService ?? throw new ArgumentNullException(nameof(inspectionService));
20+
}
21+
1322
/// <summary>
1423
/// Retrieve all inspections
1524
/// </summary>
16-
// TODO: Use IAsyncEnumerable<>
1725
[HttpGet]
18-
public Task<ActionResult<ICollection<Inspection>>> GetInspectionsAsync()
26+
public IAsyncEnumerable<Inspection> GetInspectionsAsync()
1927
{
20-
return Task.FromResult<ActionResult<ICollection<Inspection>>>(new List<Inspection> {
21-
new Inspection {
22-
Id = 0,
23-
NestingBox =
24-
new NestingBox {
25-
Id = "F000001",
26-
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
27-
OldId = null,
28-
ForeignId = "x234362",
29-
CoordinateLongitude = -97.142212,
30-
CoordinateLatitude = 30.081692,
31-
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
32-
HangUpUser = null,
33-
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
34-
Material = Material.TreatedWood,
35-
HoleSize = HoleSize.Large,
36-
ImageFileName = null,
37-
Comment = "This is a test",
38-
LastUpdated = DateTime.UtcNow
39-
},
40-
InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12),
41-
InspectedByUser = null,
42-
HasBeenCleaned = false,
43-
Condition = Condition.Good,
44-
JustRepaired = false,
45-
Occupied = true,
46-
ContainsEggs = true,
47-
EggCount = 0,
48-
ChickCount = 5,
49-
RingedChickCount = 4,
50-
AgeInDays = 6,
51-
FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged,
52-
MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged,
53-
Species = new Species { Id = 0, Name = "Dodo" },
54-
ImageFileName = null,
55-
Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!",
56-
LastUpdated = DateTime.UtcNow
57-
}
58-
});
28+
return _inspectionService.GetAllAsync();
5929
}
6030

6131
/// <summary>
6232
/// Retrieve an inspection by id
6333
/// </summary>
6434
[HttpGet("{id}")]
65-
public Task<ActionResult<Inspection>> GetInspectionByIdAsync(int id)
35+
public async Task<ActionResult<Inspection>> GetInspectionByIdAsync(int id)
36+
{
37+
// Retrieve inspection
38+
Inspection inspection = await _inspectionService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
39+
if (inspection == null)
40+
return NotFound();
41+
42+
return inspection;
43+
}
44+
45+
/// <summary>
46+
/// Preview all inspections with a reduced set of data
47+
/// </summary>
48+
[HttpGet("previews")]
49+
public IAsyncEnumerable<InspectionPreview> GetInspectionPreviewsAsync()
50+
{
51+
return _inspectionService.GetAllPreviewsAsync();
52+
}
53+
54+
/// <summary>
55+
/// Preview an inspection by id with a reduced set of data
56+
/// </summary>
57+
[HttpGet("previews/{id}")]
58+
public async Task<ActionResult<InspectionPreview>> GetInspectionPreviewByIdAsync(int id)
6659
{
67-
if (id != 0)
68-
return Task.FromResult<ActionResult<Inspection>>(NotFound());
60+
// Retrieve inspection preview
61+
InspectionPreview inspectionPreview = await _inspectionService.FindPreviewByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
62+
if (inspectionPreview == null)
63+
return NotFound();
6964

70-
return Task.FromResult<ActionResult<Inspection>>(new Inspection {
71-
Id = 0,
72-
NestingBox =
73-
new NestingBox {
74-
Id = "F000001",
75-
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
76-
OldId = null,
77-
ForeignId = "x234362",
78-
CoordinateLongitude = -97.142212,
79-
CoordinateLatitude = 30.081692,
80-
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
81-
HangUpUser = null,
82-
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
83-
Material = Material.TreatedWood,
84-
HoleSize = HoleSize.Large,
85-
ImageFileName = null,
86-
Comment = "This is a test",
87-
LastUpdated = DateTime.UtcNow
88-
},
89-
InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12),
90-
InspectedByUser = null,
91-
HasBeenCleaned = false,
92-
Condition = Condition.Good,
93-
JustRepaired = false,
94-
Occupied = true,
95-
ContainsEggs = true,
96-
EggCount = 0,
97-
ChickCount = 5,
98-
RingedChickCount = 4,
99-
AgeInDays = 6,
100-
FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged,
101-
MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged,
102-
Species = new Species { Id = 0, Name = "Dodo" },
103-
ImageFileName = null,
104-
Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!",
105-
LastUpdated = DateTime.UtcNow
106-
});
65+
return inspectionPreview;
10766
}
10867
}
10968
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.Mvc;
45
using Nesteo.Server.Models;
6+
using Nesteo.Server.Services;
57

68
namespace Nesteo.Server.Controllers.Api
79
{
810
[Route("api/v1/species")]
911
public class SpeciesController : ApiControllerBase
1012
{
13+
private readonly ISpeciesService _speciesService;
14+
15+
public SpeciesController(ISpeciesService speciesService)
16+
{
17+
_speciesService = speciesService ?? throw new ArgumentNullException(nameof(speciesService));
18+
}
19+
1120
/// <summary>
1221
/// Retrieve all species
1322
/// </summary>
14-
// TODO: Use IAsyncEnumerable<>
1523
[HttpGet]
16-
public Task<ActionResult<ICollection<Species>>> GetSpeciesAsync()
24+
public IAsyncEnumerable<Species> GetSpeciesAsync()
1725
{
18-
return Task.FromResult<ActionResult<ICollection<Species>>>(new List<Species> { new Species { Id = 0, Name = "Dodo" } });
26+
return _speciesService.GetAllAsync();
1927
}
2028

2129
/// <summary>
2230
/// Retrieve a species by id
2331
/// </summary>
2432
[HttpGet("{id}")]
25-
public Task<ActionResult<Species>> GetSpeciesByIdAsync(int id)
33+
public async Task<ActionResult<Species>> GetSpeciesByIdAsync(int id)
2634
{
27-
if (id != 0)
28-
return Task.FromResult<ActionResult<Species>>(NotFound());
35+
// Retrieve species
36+
Species species = await _speciesService.FindByIdAsync(id, HttpContext.RequestAborted).ConfigureAwait(false);
37+
if (species == null)
38+
return NotFound();
2939

30-
return Task.FromResult<ActionResult<Species>>(new Species { Id = 0, Name = "Dodo" });
40+
return species;
3141
}
3242
}
3343
}

Nesteo.Server/MappingProfiles/ModelMappingProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public ModelMappingProfile()
2626
.OrderByDescending(inspection => inspection.InspectionDate)
2727
.FirstOrDefault().InspectionDate));
2828
CreateMap<InspectionEntity, Inspection>();
29+
CreateMap<InspectionEntity, InspectionPreview>();
2930
}
3031
}
3132
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using Nesteo.Server.Data.Enums;
3+
4+
namespace Nesteo.Server.Models
5+
{
6+
public class InspectionPreview
7+
{
8+
/// <summary>
9+
/// Inspection-ID
10+
/// </summary>
11+
public int Id { get; set; }
12+
13+
/// <summary>
14+
/// The id of the inspected nesting box
15+
/// </summary>
16+
public string NestingBoxId { get; set; }
17+
18+
/// <summary>
19+
/// Date and time of the inspection
20+
/// </summary>
21+
public DateTime InspectionDate { get; set; }
22+
23+
/// <summary>
24+
/// The condition in which the nesting box has been found
25+
/// </summary>
26+
public Condition Condition { get; set; }
27+
28+
/// <summary>
29+
/// Number of ringed chicks
30+
/// </summary>
31+
public int RingedChickCount { get; set; }
32+
33+
/// <summary>
34+
/// Information about the presence of the female parent bird
35+
/// </summary>
36+
public ParentBirdDiscovery FemaleParentBirdDiscovery { get; set; }
37+
38+
/// <summary>
39+
/// Information about the presence of the male parent bird
40+
/// </summary>
41+
public ParentBirdDiscovery MaleParentBirdDiscovery { get; set; }
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Nesteo.Server.Models;
5+
6+
namespace Nesteo.Server.Services
7+
{
8+
public interface IInspectionService: ICrudService<Inspection, int>
9+
{
10+
IAsyncEnumerable<InspectionPreview> GetAllPreviewsAsync();
11+
12+
Task<InspectionPreview> FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default);
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Nesteo.Server.Models;
2+
3+
namespace Nesteo.Server.Services
4+
{
5+
public interface ISpeciesService : ICrudService<Species, int> { }
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using AutoMapper;
7+
using AutoMapper.QueryableExtensions;
8+
using Microsoft.EntityFrameworkCore;
9+
using Nesteo.Server.Data;
10+
using Nesteo.Server.Data.Entities;
11+
using Nesteo.Server.Models;
12+
13+
namespace Nesteo.Server.Services.Implementations
14+
{
15+
public class InspectionService : CrudServiceBase<InspectionEntity, Inspection, int>, IInspectionService
16+
{
17+
protected override DbSet<InspectionEntity> Entities => DbContext.Inspections;
18+
19+
public InspectionService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { }
20+
21+
public IAsyncEnumerable<InspectionPreview> GetAllPreviewsAsync()
22+
{
23+
return Entities.ProjectTo<InspectionPreview>(Mapper.ConfigurationProvider).AsAsyncEnumerable();
24+
}
25+
26+
public Task<InspectionPreview> FindPreviewByIdAsync(int id, CancellationToken cancellationToken = default)
27+
{
28+
return Entities.Where(entity => entity.Id.Equals(id)).ProjectTo<InspectionPreview>(Mapper.ConfigurationProvider).FirstOrDefaultAsync(cancellationToken);
29+
}
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AutoMapper;
2+
using Microsoft.EntityFrameworkCore;
3+
using Nesteo.Server.Data;
4+
using Nesteo.Server.Data.Entities;
5+
using Nesteo.Server.Models;
6+
7+
namespace Nesteo.Server.Services.Implementations
8+
{
9+
public class SpeciesService : CrudServiceBase<SpeciesEntity, Species, int>, ISpeciesService
10+
{
11+
protected override DbSet<SpeciesEntity> Entities => DbContext.Species;
12+
13+
public SpeciesService(NesteoDbContext dbContext, IMapper mapper) : base(dbContext, mapper) { }
14+
}
15+
}

Nesteo.Server/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public void ConfigureServices(IServiceCollection services)
115115
services.AddScoped<IUserService, UserService>();
116116
services.AddScoped<IRegionService, RegionService>();
117117
services.AddScoped<IOwnerService, OwnerService>();
118-
// TODO: services.AddScoped<ISpeciesService, SpeciesService>();
118+
services.AddScoped<ISpeciesService, SpeciesService>();
119119
services.AddScoped<INestingBoxService, NestingBoxService>();
120-
// TODO: services.AddScoped<IInspectionService, InspectionService>();
120+
services.AddScoped<IInspectionService, InspectionService>();
121121
}
122122

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

0 commit comments

Comments
 (0)