-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
EmployeesController.cs
131 lines (121 loc) · 4.9 KB
/
EmployeesController.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using HappyCode.NetCoreBoilerplate.Core;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using HappyCode.NetCoreBoilerplate.Core.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Mvc;
namespace HappyCode.NetCoreBoilerplate.Api.Controllers
{
[FeatureGate(FeatureFlags.DockerCompose)]
[Route("api/employees")]
public class EmployeesController : ApiControllerBase
{
private readonly IEmployeeRepository _employeeRepository;
private readonly IFeatureManager _featureManager;
public EmployeesController(IEmployeeRepository employeeRepository, IFeatureManager featureManager)
{
_employeeRepository = employeeRepository;
_featureManager = featureManager;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<EmployeeDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllAsync(
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.GetAllAsync(cancellationToken);
return Ok(result);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(EmployeeDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetAsync(
int id,
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.GetByIdAsync(id, cancellationToken);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpGet("{id}/details")]
[ProducesResponseType(typeof(EmployeeDetailsDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetWithDetailsAsync(
int id,
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.GetByIdWithDetailsAsync(id, cancellationToken);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpGet("oldest")]
[ProducesResponseType(typeof(EmployeeDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetOldestAsync(
CancellationToken cancellationToken = default)
{
if (await _featureManager.IsEnabledAsync(FeatureFlags.Santa.ToString()))
{
return Ok(new EmployeeDto
{
Id = int.MaxValue,
FirstName = "Santa",
LastName = "Claus",
BirthDate = new DateTime(270, 3, 15),
Gender = "M",
});
}
var result = await _employeeRepository.GetOldestAsync(cancellationToken);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpPut("{id}")]
[ProducesResponseType(typeof(EmployeeDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> PutAsync(
[FromRoute] int id,
[FromBody] EmployeePutDto employeePutDto,
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.UpdateAsync(id, employeePutDto, cancellationToken);
if (result is null)
{
return NotFound();
}
return Ok(result);
}
[HttpPost]
[ProducesResponseType(typeof(EmployeeDto), StatusCodes.Status201Created)]
public async Task<IActionResult> PostAsync(
[FromBody] EmployeePostDto employeePostDto,
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.InsertAsync(employeePostDto, cancellationToken);
Response.Headers.Append("x-date-created", DateTime.UtcNow.ToString("s"));
return CreatedAtAction("Get", new { id = result.Id }, result);
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteAsync(
int id,
CancellationToken cancellationToken = default)
{
var result = await _employeeRepository.DeleteByIdAsync(id, cancellationToken);
if (result)
{
return NoContent();
}
return NotFound();
}
}
}