-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAutoCrudController.cs
76 lines (66 loc) · 2.43 KB
/
AutoCrudController.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
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace AutoCrud
{
public abstract class AutoCrudController<Entity, PrimaryKey> : AutoCrudControllerBase<Entity, PrimaryKey>,
IAutoCrudController<Entity, PrimaryKey> where Entity : class
{
protected AutoCrudController(IAutoCrudRepository<Entity, PrimaryKey> repository, ILogger logger) : base(
repository, logger)
{
}
[HttpGet]
public virtual IActionResult GetPage([FromQuery] int page = 0)
{
BeforeGetPage();
Log($"Fetching {GetEntityNameInPluralForLogging()} on page : {page}");
var entities = _repository.GetPage(page, GetPageSize());
return Ok(entities);
}
[HttpGet("{key}")]
public IActionResult Find([FromRoute] PrimaryKey key)
{
BeforeFindEntity();
Log($"Fetching {GetEntityNameInSingularForLogging()} with key : {key}");
var entity = _repository.Find(key);
return Ok(entity);
}
[HttpPost]
public IActionResult Create([FromBody] Entity entity)
{
BeforeCreate();
Log($"Creating new {GetEntityNameInSingularForLogging()}");
var createdEntity = _repository.Create(entity);
PostProcessCreate(entity);
return Created(GetCreatedEntityUri(createdEntity), createdEntity);
}
[HttpPut("{key}")]
public IActionResult Update([FromRoute] PrimaryKey key, [FromBody] Entity entity)
{
BeforeUpdate();
Log($"Updating {GetEntityNameInSingularForLogging()} with key : {key}");
SetPrimaryKeyValueToEntity(entity, key);
var editedEntity = _repository.Update(entity);
PostProcessUpdate(entity);
return Ok(editedEntity);
}
[HttpDelete("{key}")]
public IActionResult Delete([FromRoute] PrimaryKey key)
{
BeforeDelete();
Log($"Deleting {GetEntityNameInSingularForLogging()} with key : {key}");
var deletedEntity = _repository.Delete(key);
PostProcessDelete(deletedEntity);
return NoContent();
}
protected void PostProcessCreate(Entity entity)
{
}
protected void PostProcessUpdate(Entity entity)
{
}
protected void PostProcessDelete(Entity entity)
{
}
}
}