|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Threading.Tasks; |
4 | 4 | using Microsoft.AspNetCore.Mvc; |
| 5 | +using Nesteo.Server.Data.Entities; |
5 | 6 | using Nesteo.Server.Data.Enums; |
6 | 7 | using Nesteo.Server.Models; |
| 8 | +using Nesteo.Server.Services; |
7 | 9 |
|
8 | 10 | namespace Nesteo.Server.Controllers.Api |
9 | 11 | { |
10 | 12 | [Route("api/v1/inspections")] |
11 | 13 | public class InspectionsController : ApiControllerBase |
12 | 14 | { |
| 15 | + private readonly IInspectionService _inspectionService; |
| 16 | + |
| 17 | + public InspectionsController(IInspectionService inspectionService) |
| 18 | + { |
| 19 | + _inspectionService = inspectionService ?? throw new ArgumentNullException(nameof(inspectionService)); |
| 20 | + } |
| 21 | + |
13 | 22 | /// <summary> |
14 | 23 | /// Retrieve all inspections |
15 | 24 | /// </summary> |
16 | | - // TODO: Use IAsyncEnumerable<> |
17 | 25 | [HttpGet] |
18 | | - public Task<ActionResult<ICollection<Inspection>>> GetInspectionsAsync() |
| 26 | + public IAsyncEnumerable<Inspection> GetInspectionsAsync() |
19 | 27 | { |
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(); |
59 | 29 | } |
60 | 30 |
|
61 | 31 | /// <summary> |
62 | 32 | /// Retrieve an inspection by id |
63 | 33 | /// </summary> |
64 | 34 | [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) |
66 | 59 | { |
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(); |
69 | 64 |
|
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; |
107 | 66 | } |
108 | 67 | } |
109 | 68 | } |
0 commit comments