-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherProfilesController.cs
57 lines (48 loc) · 2.15 KB
/
WeatherProfilesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using ExampleBlazorApp.Server.Repositories;
using ExampleBlazorApp.Shared;
using Microsoft.AspNetCore.Mvc;
using RandomSkunk.Results;
using RandomSkunk.Results.AspNetCore;
namespace ExampleBlazorApp.Server.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherProfilesController : ControllerBase
{
private readonly IWeatherRepository _weatherRepository;
public WeatherProfilesController(IWeatherRepository weatherRepository)
{
_weatherRepository = weatherRepository;
}
[HttpGet]
public async Task<IActionResult> GetWeatherProfiles()
{
// Get the list of weather profiles from the database as a Result<IReadOnlyList<WeatherProfile>>
// using our WeatherRepository.
Result<IReadOnlyList<WeatherProfile>> weatherProfilesResult =
await _weatherRepository.GetWeatherProfiles();
// Using the RandomSkunk.Results.AspNetCore package, convert the
// Result<IReadOnlyList<WeatherProfile>> into an equivalent IActionResult.
IActionResult actionResult = weatherProfilesResult.ToActionResult();
return actionResult;
}
[HttpPost]
public async Task<IActionResult> AddWeatherProfile([FromBody] WeatherProfile weatherProfile)
{
// Add the weather profile to the database using our WeatherRepository.
Result profileAddedResult = await _weatherRepository.AddWeatherProfile(weatherProfile);
// Using the RandomSkunk.Results.AspNetCore package, convert the Result into an
// equivalent IActionResult.
IActionResult actionResult = profileAddedResult.ToActionResult();
return actionResult;
}
[HttpPut]
public async Task<IActionResult> EditWeatherProfile([FromBody] WeatherProfile weatherProfile)
{
// Edit the weather profile to the database using our WeatherRepository.
Result profileEditedResult = await _weatherRepository.EditWeatherProfile(weatherProfile);
// Using the RandomSkunk.Results.AspNetCore package, convert the Result into an
// equivalent IActionResult.
IActionResult actionResult = profileEditedResult.ToActionResult();
return actionResult;
}
}