-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericControllerBase.cs
More file actions
87 lines (77 loc) · 3 KB
/
GenericControllerBase.cs
File metadata and controls
87 lines (77 loc) · 3 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
using System.Text.Json;
using AdventureWorksDemo.Data.Entities;
using AdventureWorksDemo.Data.Models;
using AdventureWorksDemo.Data.Paging;
using AdventureWorksDemo.Data.Services;
using Microsoft.AspNetCore.Mvc;
namespace AdventureWorksDemo.API.Controllers
{
[ApiController]
public abstract class GenericControllerBase<TModel>(ILogger logger,
dynamic service) : ControllerBase
where TModel : class
{
internal readonly ILogger _logger = logger;
internal readonly IService _service = service;
[HttpPost()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> AddAsync([FromBody] TModel model)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(AddAsync));
IServiceResult<TModel> result = await ((IAddService<TModel>)_service).AddAsync(model);
if (result != null)
return Ok(result);
else
return BadRequest(result);
}
[HttpDelete("id")]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> DeleteAsync(int id)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(DeleteAsync));
var result = await ((IDeleteService<int>)_service).DeleteAsync(id);
if (result.IsSuccess)
return Ok(result.Message);
return BadRequest(result);
}
[HttpGet()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public virtual async Task<IActionResult> FindAllAsync([FromBody] PageingFilter pageingFilter)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(FindAllAsync), "pageingFilter");
PagedList<TModel> result = await ((IFindService<TModel, int>)_service).FindAllAsync(pageingFilter);
Response.Headers.Append("X-Pagination", JsonSerializer.Serialize((IPagedList)result));
if (result != null && result!.Any())
return Ok(result);
else
return NotFound(result);
}
[HttpGet("id")]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
public virtual async Task<IActionResult?> FindAsync(int id)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(FindAllAsync));
return Ok(await ((IFindService<TModel, int>)_service).FindAsync(id));
}
[HttpPut()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> UpdateAsync([FromBody] TModel model)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(UpdateAsync), "model");
var result = await ((IUpdateService<TModel>)_service).UpdateAsync(model);
if (result != null)
return Ok(result);
else
return BadRequest(result);
}
internal void WriteToTraceLog(string namespaceName, string methodName, string parmeterNames = "")
{
string Message = $"{namespaceName}.{methodName}({parmeterNames})";
_logger.LogTrace("{Message}", Message);
}
}
}