Files
LaDOSE/LaDOSE.Src/LaDOSE.Api/Controllers/GenericControllerDTO.cs

53 lines
1.3 KiB
C#
Raw Normal View History

2019-03-14 01:31:56 +01:00
using System;
using System.Collections.Generic;
2019-03-12 22:23:38 +01:00
using System.Linq;
2020-09-13 22:50:52 +02:00
using AutoMapper;
2019-03-12 22:23:38 +01:00
using LaDOSE.Business.Interface;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
public class GenericControllerDTO<T, TU, D> : Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
{
2020-09-13 22:50:52 +02:00
protected IMapper _mapper;
2019-03-12 22:23:38 +01:00
protected T _service;
2020-09-13 22:50:52 +02:00
public GenericControllerDTO(IMapper mapper, T service)
2019-03-12 22:23:38 +01:00
{
2020-09-13 22:50:52 +02:00
_mapper = mapper;
2019-03-12 22:23:38 +01:00
_service = service;
}
[HttpPost]
public D Post([FromBody]D dto)
{
2020-09-13 22:50:52 +02:00
TU entity = _mapper.Map<TU>(dto);
return _mapper.Map<D>(_service.AddOrUpdate(entity));
2019-03-12 22:23:38 +01:00
}
[HttpGet]
public List<D> Get()
{
2020-09-13 22:50:52 +02:00
return _mapper.Map<List<D>>(_service.GetAll().ToList());
2019-03-12 22:23:38 +01:00
}
[HttpGet("{id}")]
public D Get(int id)
{
2020-09-13 22:50:52 +02:00
return _mapper.Map<D>(_service.GetById(id));
2019-03-12 22:23:38 +01:00
}
2019-03-14 01:31:56 +01:00
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
try
{
return _service.Delete((int)id) ? (IActionResult)NoContent() : NotFound();
}
catch (Exception)
{
return BadRequest();
}
}
2019-03-12 22:23:38 +01:00
}
}