-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataCrudControllerBase.cs
More file actions
105 lines (94 loc) · 3.11 KB
/
DataCrudControllerBase.cs
File metadata and controls
105 lines (94 loc) · 3.11 KB
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
using Keeptrack.Common.System;
using Keeptrack.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace Keeptrack.WebApi.Controllers;
/// <summary>
/// Data CRUD (Create, Request, Update, Delete) Controller abstract class.
/// </summary>
/// <typeparam name="TDto">Data Transfer Object</typeparam>
/// <typeparam name="TModel">Domain Model</typeparam>
[ApiController]
public abstract class DataCrudControllerBase<TDto, TModel>(IMapper mapper, IDataRepository<TModel> dataRepository)
: ControllerBase
where TModel : class, IHasIdAndOwnerId
{
[HttpGet]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<ActionResult<PagedResult<TDto>>> Get([FromQuery] PagedRequest pagedRequest, [FromQuery] TDto input)
{
var models = await dataRepository.FindAllAsync(GetUserId(),
pagedRequest.Page,
pagedRequest.PageSize,
pagedRequest.Search,
mapper.Map<TModel>(input));
return Ok(models.Map(mapper.Map<TDto>));
}
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public async Task<ActionResult<TDto>> GetById(string id)
{
if (string.IsNullOrEmpty(id))
{
return BadRequest();
}
var model = await dataRepository.FindOneAsync(id, GetUserId());
if (model == null)
{
return NotFound();
}
return Ok(mapper.Map<TDto>(model));
}
[HttpPost]
[Consumes("application/json", "text/json")]
[Produces("application/json")]
[ProducesResponseType(201)]
public async Task<IActionResult> Post([FromBody] TDto dto)
{
var input = mapper.Map<TModel>(dto);
input.OwnerId = GetUserId();
var model = await dataRepository.CreateAsync(input);
return CreatedAtAction(nameof(GetById), new { id = model.Id }, mapper.Map<TDto>(model));
}
[HttpPut("{id}")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<IActionResult> Put(string id, [FromBody] TDto dto)
{
if (string.IsNullOrEmpty(id))
{
return BadRequest();
}
var input = mapper.Map<TModel>(dto);
input.OwnerId = GetUserId();
await dataRepository.UpdateAsync(id, input, GetUserId());
return NoContent();
}
[HttpDelete("{id}")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<IActionResult> Delete(string id)
{
if (string.IsNullOrEmpty(id))
{
return BadRequest();
}
await dataRepository.DeleteAsync(id, GetUserId());
return NoContent();
}
/// <summary>
/// Get authenticated user id.
/// </summary>
/// <returns></returns>
private string GetUserId()
{
var userId = User.Claims.FirstOrDefault(x => x.Type == "user_id")?.Value;
return string.IsNullOrEmpty(userId) ? throw new UnauthorizedAccessException() : userId;
}
}