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
|
|
|
|
|
|
{
|
2022-07-30 20:00:31 +02:00
|
|
|
|
public abstract class GenericControllerDTO<T, TU, D> : Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
|
2019-03-12 22:23:38 +01:00
|
|
|
|
{
|
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]
|
2022-07-30 20:00:31 +02:00
|
|
|
|
public virtual D Post([FromBody]D dto)
|
2019-03-12 22:23:38 +01:00
|
|
|
|
{
|
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]
|
2022-07-30 20:00:31 +02:00
|
|
|
|
public virtual List<D> Get()
|
2019-03-12 22:23:38 +01:00
|
|
|
|
{
|
|
|
|
|
|
|
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}")]
|
2022-07-30 20:00:31 +02:00
|
|
|
|
public virtual D Get(int id)
|
2019-03-12 22:23:38 +01:00
|
|
|
|
{
|
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}")]
|
2022-07-30 20:00:31 +02:00
|
|
|
|
public virtual IActionResult Delete(int id)
|
2019-03-14 01:31:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return _service.Delete((int)id) ? (IActionResult)NoContent() : NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-03-12 22:23:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|