Skip to content

Commit 86f1f49

Browse files
Implemented API endpoints for data retrieval (fixes #4) that deliver some dummy data (fixes #5)
1 parent c49c6d5 commit 86f1f49

22 files changed

+593
-4
lines changed

Nesteo.Server/Configuration/JsonConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.Json.Serialization;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.Extensions.Hosting;
@@ -19,6 +20,9 @@ public void Configure(JsonOptions options)
1920
{
2021
// Indent JSON responses in development mode
2122
options.JsonSerializerOptions.WriteIndented = _webHostEnvironment.IsDevelopment();
23+
24+
// Serialize enum values as strings (and the other way around)
25+
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(null, false));
2226
}
2327
}
2428
}

Nesteo.Server/Controllers/Api/AuthController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nesteo.Server.Controllers.Api
1111
{
1212
[Route("api/v1/auth")]
13-
[Produces("application/json")]
1413
public class AuthController : ApiControllerBase
1514
{
1615
private readonly IUserService _userService;
@@ -24,7 +23,6 @@ public AuthController(IUserService userService)
2423
/// Retrieves information about the currently authenticated user.
2524
/// </summary>
2625
[HttpGet]
27-
[ProducesResponseType(StatusCodes.Status200OK)]
2826
public async Task<ActionResult<User>> GetAuthenticatedUserAsync()
2927
{
3028
// Get the of the currently authenticated user
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Nesteo.Server.Data.Enums;
7+
using Nesteo.Server.Models;
8+
9+
namespace Nesteo.Server.Controllers.Api
10+
{
11+
[Route("api/v1/inspections")]
12+
public class InspectionsController : ApiControllerBase
13+
{
14+
/// <summary>
15+
/// Retrieves all inspections
16+
/// </summary>
17+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
18+
[HttpGet]
19+
public Task<ActionResult<ICollection<Inspection>>> GetInspectionsAsync()
20+
{
21+
return Task.FromResult<ActionResult<ICollection<Inspection>>>(new List<Inspection> {
22+
new Inspection {
23+
Id = 0,
24+
NestingBox =
25+
new NestingBox {
26+
Id = "F000001",
27+
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
28+
OldId = null,
29+
ForeignId = "x234362",
30+
CoordinateLongitude = -97.142212,
31+
CoordinateLatitude = 30.081692,
32+
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
33+
HangUpUser = null,
34+
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
35+
Material = Material.TreatedWood,
36+
HoleSize = HoleSize.Large,
37+
ImageFileName = null,
38+
Comment = "This is a test",
39+
LastUpdated = DateTime.UtcNow
40+
},
41+
InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12),
42+
InspectedByUser = null,
43+
HasBeenCleaned = false,
44+
Condition = Condition.Good,
45+
JustRepaired = false,
46+
Occupied = true,
47+
ContainsEggs = true,
48+
EggCount = 0,
49+
ChickCount = 5,
50+
RingedChickCount = 4,
51+
AgeInDays = 6,
52+
FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged,
53+
MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged,
54+
Species = new Species { Id = 0, Name = "Dodo" },
55+
ImageFileName = null,
56+
Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!",
57+
LastUpdated = DateTime.UtcNow
58+
}
59+
});
60+
}
61+
62+
/// <summary>
63+
/// Retrieves an inspection by id
64+
/// </summary>
65+
[HttpGet("{id}")]
66+
public Task<ActionResult<Inspection>> GetInspectionByIdAsync(int id)
67+
{
68+
if (id != 0)
69+
return Task.FromResult<ActionResult<Inspection>>(NotFound());
70+
71+
return Task.FromResult<ActionResult<Inspection>>(new Inspection {
72+
Id = 0,
73+
NestingBox =
74+
new NestingBox {
75+
Id = "F000001",
76+
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
77+
OldId = null,
78+
ForeignId = "x234362",
79+
CoordinateLongitude = -97.142212,
80+
CoordinateLatitude = 30.081692,
81+
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
82+
HangUpUser = null,
83+
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
84+
Material = Material.TreatedWood,
85+
HoleSize = HoleSize.Large,
86+
ImageFileName = null,
87+
Comment = "This is a test",
88+
LastUpdated = DateTime.UtcNow
89+
},
90+
InspectionDate = new DateTime(2013, 12, 12, 12, 12, 12),
91+
InspectedByUser = null,
92+
HasBeenCleaned = false,
93+
Condition = Condition.Good,
94+
JustRepaired = false,
95+
Occupied = true,
96+
ContainsEggs = true,
97+
EggCount = 0,
98+
ChickCount = 5,
99+
RingedChickCount = 4,
100+
AgeInDays = 6,
101+
FemaleParentBirdDiscovery = ParentBirdDiscovery.AlreadyRinged,
102+
MaleParentBirdDiscovery = ParentBirdDiscovery.NewlyRinged,
103+
Species = new Species { Id = 0, Name = "Dodo" },
104+
ImageFileName = null,
105+
Comment = "It has been a great inspection! It's true! Trust me! It has been the greatest inspection ever! It's true!",
106+
LastUpdated = DateTime.UtcNow
107+
});
108+
}
109+
}
110+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Nesteo.Server.Data.Enums;
7+
using Nesteo.Server.Models;
8+
9+
namespace Nesteo.Server.Controllers.Api
10+
{
11+
[Route("api/v1/nesting-boxes")]
12+
public class NestingBoxesController : ApiControllerBase
13+
{
14+
/// <summary>
15+
/// Retrieves all nesting boxes
16+
/// </summary>
17+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
18+
[HttpGet]
19+
public Task<ActionResult<ICollection<NestingBox>>> GetNestingBoxesAsync()
20+
{
21+
return Task.FromResult<ActionResult<ICollection<NestingBox>>>(new List<NestingBox> {
22+
new NestingBox {
23+
Id = "F000001",
24+
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
25+
OldId = null,
26+
ForeignId = "x234362",
27+
CoordinateLongitude = -97.142212,
28+
CoordinateLatitude = 30.081692,
29+
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
30+
HangUpUser = null,
31+
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
32+
Material = Material.TreatedWood,
33+
HoleSize = HoleSize.Large,
34+
ImageFileName = null,
35+
Comment = "This is a test",
36+
LastUpdated = DateTime.UtcNow
37+
}
38+
});
39+
}
40+
41+
/// <summary>
42+
/// Retrieves a nesting box by id
43+
/// </summary>
44+
[HttpGet("{id}")]
45+
public Task<ActionResult<NestingBox>> GetNestingBoxByIdAsync(string id)
46+
{
47+
if (id != "F000001")
48+
return Task.FromResult<ActionResult<NestingBox>>(NotFound());
49+
50+
return Task.FromResult<ActionResult<NestingBox>>(new NestingBox {
51+
Id = "F000001",
52+
Region = new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" },
53+
OldId = null,
54+
ForeignId = "x234362",
55+
CoordinateLongitude = -97.142212,
56+
CoordinateLatitude = 30.081692,
57+
HangUpDate = new DateTime(2012, 12, 12, 12, 12, 12),
58+
HangUpUser = null,
59+
Owner = new Owner { Id = 0, Name = "He-who-must-not-be-named" },
60+
Material = Material.TreatedWood,
61+
HoleSize = HoleSize.Large,
62+
ImageFileName = null,
63+
Comment = "This is a test",
64+
LastUpdated = DateTime.UtcNow
65+
});
66+
}
67+
}
68+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Nesteo.Server.Models;
6+
7+
namespace Nesteo.Server.Controllers.Api
8+
{
9+
[Route("api/v1/owners")]
10+
public class OwnersController : ApiControllerBase
11+
{
12+
/// <summary>
13+
/// Retrieves all owners
14+
/// </summary>
15+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
16+
[HttpGet]
17+
public Task<ActionResult<ICollection<Owner>>> GetOwnersAsync()
18+
{
19+
return Task.FromResult<ActionResult<ICollection<Owner>>>(new List<Owner> { new Owner { Id = 0, Name = "He-who-must-not-be-named" } });
20+
}
21+
22+
/// <summary>
23+
/// Retrieves an owner by id
24+
/// </summary>
25+
[HttpGet("{id}")]
26+
public Task<ActionResult<Owner>> GetOwnerByIdAsync(int id)
27+
{
28+
if (id != 0)
29+
return Task.FromResult<ActionResult<Owner>>(NotFound());
30+
31+
return Task.FromResult<ActionResult<Owner>>(new Owner { Id = 0, Name = "He-who-must-not-be-named" });
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Nesteo.Server.Models;
6+
7+
namespace Nesteo.Server.Controllers.Api
8+
{
9+
[Route("api/v1/regions")]
10+
public class RegionsController : ApiControllerBase
11+
{
12+
/// <summary>
13+
/// Retrieves all regions
14+
/// </summary>
15+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
16+
[HttpGet]
17+
public Task<ActionResult<ICollection<Region>>> GetRegionsAsync()
18+
{
19+
return Task.FromResult<ActionResult<ICollection<Region>>>(new List<Region> { new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" } });
20+
}
21+
22+
/// <summary>
23+
/// Retrieves a region by id
24+
/// </summary>
25+
[HttpGet("{id}")]
26+
public Task<ActionResult<Region>> GetRegionByIdAsync(int id)
27+
{
28+
if (id != 0)
29+
return Task.FromResult<ActionResult<Region>>(NotFound());
30+
31+
return Task.FromResult<ActionResult<Region>>(new Region { Id = 0, Name = "The only forest in germany", NestingBoxIdPrefix = "F" });
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Nesteo.Server.Models;
6+
7+
namespace Nesteo.Server.Controllers.Api
8+
{
9+
[Route("api/v1/species")]
10+
public class SpeciesController : ApiControllerBase
11+
{
12+
/// <summary>
13+
/// Retrieves all species
14+
/// </summary>
15+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
16+
[HttpGet]
17+
public Task<ActionResult<ICollection<Species>>> GetSpeciesAsync()
18+
{
19+
return Task.FromResult<ActionResult<ICollection<Species>>>(new List<Species> { new Species { Id = 0, Name = "Dodo" } });
20+
}
21+
22+
/// <summary>
23+
/// Retrieves a species by id
24+
/// </summary>
25+
[HttpGet("{id}")]
26+
public Task<ActionResult<Species>> GetSpeciesByIdAsync(int id)
27+
{
28+
if (id != 0)
29+
return Task.FromResult<ActionResult<Species>>(NotFound());
30+
31+
return Task.FromResult<ActionResult<Species>>(new Species { Id = 0, Name = "Dodo" });
32+
}
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Nesteo.Server.Models;
7+
using Nesteo.Server.Services;
8+
9+
namespace Nesteo.Server.Controllers.Api
10+
{
11+
[Route("api/v1/users")]
12+
public class UsersController : ApiControllerBase
13+
{
14+
private readonly IUserService _userService;
15+
16+
public UsersController(IUserService userService)
17+
{
18+
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
19+
}
20+
21+
/// <summary>
22+
/// Retrieves all users
23+
/// </summary>
24+
// TODO: Use IAsyncEnumerable<> after EF Core upgrade
25+
[HttpGet]
26+
public async Task<ActionResult<ICollection<User>>> GetUsersAsync()
27+
{
28+
return Ok(await _userService.GetAllUsersAsync().ConfigureAwait(false));
29+
}
30+
31+
/// <summary>
32+
/// Retrieves a user by id
33+
/// </summary>
34+
[HttpGet("{id}")]
35+
public async Task<ActionResult<User>> GetUserByIdAsync(string id)
36+
{
37+
// Retrieve user
38+
User user = await _userService.FindUserByIdAsync(id).ConfigureAwait(false);
39+
if (user == null)
40+
return NotFound();
41+
42+
return user;
43+
}
44+
}
45+
}

Nesteo.Server/Controllers/ApiControllerBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
namespace Nesteo.Server.Controllers
44
{
55
[ApiController]
6+
[Produces("application/json")]
67
public abstract class ApiControllerBase : Controller { }
78
}

Nesteo.Server/Data/Enums/Condition.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
14
namespace Nesteo.Server.Data.Enums
25
{
6+
[JsonConverter(typeof(StringEnumConverter))]
37
public enum Condition
48
{
59
Good,

0 commit comments

Comments
 (0)