-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreController.cs
96 lines (80 loc) · 3.58 KB
/
CoreController.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
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using PayCoreClassWork3.WebAPI.Core.API.Abstract;
using PayCoreClassWork3.WebAPI.Core.Business.Abstract;
using PayCoreClassWork3.WebAPI.Core.DataAccess.Concrete;
using PayCoreClassWork3.WebAPI.Core.Dto.Abstract;
using PayCoreClassWork3.WebAPI.Core.Entity.Concrete;
using PayCoreClassWork3.WebAPI.Utilities;
namespace PayCoreClassWork3.WebAPI.Core.API.Concrete.Controllers
{
// Diğer controller'lar buradan kalıtım alır.
// Bu controller ise sisteme ait olan ControllerBase sınıfından kalıtım alır.
// Bu controller bir köprü görevi görmektedir. NonAction bir metod olan GetById metoduna sahiptir.
// TEntity, CoreEntity sınıfından kalıtım almış bir sınıf olmak zorundadır.
// TDto, ICoreDto interface'ini implemente almış bir nesne olmak zorundadır.
// TService, IService'ın generic operatörünün değerine yazılan tipten implemente etmiş bir sınıf olmak zorundadır.
public class CoreController<TEntity, TDto, TService> : ControllerBase, ICoreController<TEntity, TDto>
where TEntity : CoreEntity
where TDto : ICoreDto
where TService : IService<TEntity>
{
private readonly TService _service;
private readonly IMapper _mapper;
// Gerekli kurucu metod yazılır. Generic olarak belirlenen servis tipindeki nesnenin bağımlılığı eklenir.
public CoreController(TService service, IMapper mapper)
{
_service = service;
_mapper = mapper;
}
// Tüm kayıtları listeleme.
[HttpGet]
public virtual async Task<List<TEntity>> GetAll()
{
return await _service.GetAll();
}
// Id'ye göre tek bir kayıt getirme.
[HttpGet("{id}")]
public virtual async Task<ActionResult<TEntity>> GetById(long? id)
{
if (id == null)
return BadRequest(SystemMessage.ID_NULL);
var entity = await _service.GetById((long)id);
if (entity == null)
return NotFound(SystemMessage.NOT_FOUND);
return entity;
}
// Yeni bir kayıt ekleme.
[HttpPost]
public virtual async Task<ActionResult> Add([FromBody] TDto dto)
{
await _service.StartTransactionalOperation(Operation.Add, _mapper.Map<TEntity>(dto));
return Ok(SystemMessage.ADDED);
}
// Mevcut bir kaydı güncelleme.
[HttpPut]
public virtual async Task<ActionResult> Update(long? id, [FromBody] TDto dto)
{
if (id == null)
return BadRequest(SystemMessage.ID_NULL);
var entity = await _service.GetById((long)id);
if (entity == null)
return NotFound(SystemMessage.NOT_FOUND);
var mapped = _mapper.Map<TEntity>(dto);
await _service.StartTransactionalOperation(Operation.Update, entity, mapped);
return Ok(SystemMessage.UPDATED);
}
// Mevcut bir kaydı silme.
[HttpDelete("{id}")]
public virtual async Task<ActionResult> Delete(long? id)
{
if (id == null)
return BadRequest(SystemMessage.ID_NULL);
var entity = await _service.GetById((long)id);
if (entity == null)
return NotFound(SystemMessage.NOT_FOUND);
await _service.StartTransactionalOperation(Operation.Delete, entity);
return Ok(SystemMessage.DELETED);
}
}
}