Created base class for CRUD api calls

This commit is contained in:
quentin 2024-05-20 10:46:56 -05:00
parent 9fb3e52079
commit cce57a4aec
2 changed files with 143 additions and 1 deletions

141
API/Controllers/CRUDBase.cs Normal file
View File

@ -0,0 +1,141 @@
using API.Authentication.Interfaces;
using API.DTO;
using API.Services;
using DAL.Models;
using DAL.Models.Audits;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using MUser = DAL.Models.User;
namespace API.Controllers
{
public class CRUDBase<TLoggerCategory, TDTO, TUpdateDTO, TModel, TAuditModel, TAuthentication, TService> : ControllerBase
where TAuthentication : IGenericAuthentication<TDTO, TModel>
where TModel : Model<TModel, TAuditModel>
where TAuditModel : AuditModel<TModel>
where TDTO : IAdaptable<TModel>, new()
where TUpdateDTO : IUpdateAdaptable<TModel>
where TService : ServiceBase<TService, TDTO, TModel, TAuditModel, TAuthentication>
{
public readonly ILogger<TLoggerCategory> Logger;
public readonly UserService UserService;
public readonly TService Service;
public CRUDBase(ILogger<TLoggerCategory> logger, UserService userService, TService service)
{
Logger = logger;
UserService = userService;
Service = service;
}
[HttpGet]
public virtual ActionResult<List<TDTO>> get()
{
MUser? user = getUser(User);
if (user == null)
return Unauthorized();
IEnumerable<TModel>? result = Service.get(user);
if (result == null)
return Forbid();
List<TDTO> dtos = [];
Parallel.ForEach(result, item =>
{
TDTO dto = new TDTO();
dto.adaptFromModel(item);
dtos.Add(dto);
});
return Ok(dtos);
}
[HttpGet("{id}")]
public virtual ActionResult<TDTO> get(ulong id)
{
MUser? user = getUser(User);
if (user == null)
return Unauthorized();
TModel? result = Service.get(id, user);
if (result == null)
return Forbid();
TDTO dto = new TDTO();
dto.adaptFromModel(result);
return Ok(dto);
}
[HttpPost]
public virtual ActionResult<TDTO> add(TDTO createDTO)
{
MUser? user = getUser(User);
if (user == null)
return Unauthorized();
TModel? result = Service.add(createDTO, user);
if (result == null)
return Forbid();
TDTO dto = new TDTO();
dto.adaptFromModel(result);
return Ok(dto);
}
[HttpPut("{id}")]
public virtual ActionResult<TDTO> update(ulong id, TUpdateDTO updateDTO)
{
MUser? user = getUser(User);
if (user == null)
return Unauthorized();
TModel? result = Service.get(id, user);
if (result == null)
return Forbid();
updateDTO.adaptModel(ref result);
TModel? newResult = Service.update(result, user);
if (newResult == null)
return Forbid();
TDTO dto = new TDTO();
dto.adaptFromModel(newResult);
return Ok(dto);
}
[HttpDelete("{id}")]
public virtual ActionResult delete(ulong id)
{
MUser? user = getUser(User);
if (user == null)
return Unauthorized();
TModel? result = Service.get(id, user);
if (result == null)
return Forbid();
TAuditModel? auditModel = Service.delete(result, user);
if (auditModel == null)
return Forbid();
// todo in the future we should return the audit
return NoContent();
}
[NonAction]
public MUser? getUser(ClaimsPrincipal user)
{
Claim? idClaim = user.FindFirst(ClaimTypes.NameIdentifier);
if (idClaim == null)
return null;
return UserService.getNoAuthentication(UInt64.Parse(idClaim.Value));
}
}
}

View File

@ -3,6 +3,7 @@ using API.DTO;
using API.Services.Interfaces;
using DAL.Contexts;
using DAL.Models;
using DAL.Models.Audits;
using System.Linq.Expressions;
namespace API.Services
@ -10,7 +11,7 @@ namespace API.Services
public class ServiceBase<TLoggerCategory, TDTO, TModel, TAudit, TAuthentication> : IGenericService<TDTO, TModel, TAudit>
where TAuthentication : IGenericAuthentication<TDTO, TModel>
where TModel : Model<TModel, TAudit>
where TAudit : class
where TAudit : AuditModel<TModel>
where TDTO : IAdaptable<TModel>
{
private readonly TAuthentication _auth;