More code

This commit is contained in:
blyssco 2025-08-16 21:01:14 +02:00
parent 2d8d8ed754
commit a27a1351cf
14 changed files with 283 additions and 33 deletions

View File

@ -4,8 +4,8 @@ namespace Liber_Incantamentum.Application.Interfaces
{
public interface IMageService
{
Task<ICollection<MageDto>> GetAllMageAsync(MageDtoFilter filter);
Task<bool> UpdateMageAsync(MageDtoUpdate dto);
Task<ICollection<MageDto>> GetAllMagesAsync(MageDtoFilter filter);
Task<bool> UpdateMageAsync(MageDtoUpdate dto, Guid id);
Task<bool> DeleteMageAsync(Guid id);
Task<bool> AddMageAsync(MageDtoCreate dto);
Task<MageDto>? GetMageByIdAsync(Guid id);

View File

@ -4,8 +4,8 @@ namespace Liber_Incantamentum.Application.Interfaces
{
public interface ISpellService
{
Task<ICollection<SpellDto>> GetAllSpellAsync(SpellDtoFilter filter);
Task<bool> UpdateSpellAsync(SpellDtoUpdate dto);
Task<ICollection<SpellDto>> GetAllSpellsAsync(SpellDtoFilter filter);
Task<bool> UpdateSpellAsync(SpellDtoUpdate dto, Guid id);
Task<bool> DeleteSpellAsync(Guid id);
Task<bool> AddSpellAsync(SpellDtoCreate dto);
Task<SpellDto>? GetSpellByIdAsync(Guid id);

View File

@ -24,7 +24,7 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The DTO received is null");
}
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Name = dto.Name, Rank = dto.Rank, Specialisation = dto.Specialisation });
var alreadyExistingMage = await GetAllMagesAsync(new MageDtoFilter { Name = dto.Name, Rank = dto.Rank, Specialisation = dto.Specialisation });
if (alreadyExistingMage.Any())
{
throw new AlreadyExistingException("This mage does already exists");
@ -38,7 +38,7 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The id is null");
}
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Id = id });
var alreadyExistingMage = await GetAllMagesAsync(new MageDtoFilter { Id = id });
if (!alreadyExistingMage.Any())
{
throw new NotFoundException("This mage does not exists");
@ -46,9 +46,9 @@ namespace Liber_Incantamentum.Application.Services.Generals
return await _mageRepository.DeleteMageAsync(id);
}
public async Task<ICollection<MageDto>> GetAllMageAsync(MageDtoFilter filter)
public async Task<ICollection<MageDto>> GetAllMagesAsync(MageDtoFilter filter)
{
var entities = await _mageRepository.GetAllMageAsync(_mapper.Map<MageFilter>(filter));
var entities = await _mageRepository.GetAllMagesAsync(_mapper.Map<MageFilter>(filter));
return _mapper.Map<ICollection<MageDto>>(entities);
}
@ -58,7 +58,7 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The id is null");
}
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Id = id });
var alreadyExistingMage = await GetAllMagesAsync(new MageDtoFilter { Id = id });
if (!alreadyExistingMage.Any())
{
throw new NotFoundException("This mage does not exists");
@ -67,14 +67,14 @@ namespace Liber_Incantamentum.Application.Services.Generals
return _mapper.Map<MageDto>(mage);
}
public async Task<bool> UpdateMageAsync(MageDtoUpdate dto)
public async Task<bool> UpdateMageAsync(MageDtoUpdate dto, Guid id)
{
if (dto == null)
{
throw new ArgumentNullException("The DTO received is null");
}
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Name = dto.Name, Rank = dto.Rank, Specialisation = dto.Specialisation });
if (!alreadyExistingMage.Any())
var alreadyExistingMage = await GetMageByIdAsync(id);
if (alreadyExistingMage == null)
{
throw new NotFoundException("This mage does not exists");
}

View File

@ -23,10 +23,10 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The DTO received is null");
}
var alreadyExistingSpell = await GetAllSpellAsync(new SpellDtoFilter { Name = dto.Name, Description = dto.Description, Type = dto.Type, CreationDate = dto.CreationDate, MageId = dto.MageId });
var alreadyExistingSpell = await GetAllSpellsAsync(new SpellDtoFilter { Name = dto.Name, Description = dto.Description, Type = dto.Type, CreationDate = dto.CreationDate, MageId = dto.MageId });
if (alreadyExistingSpell.Any())
{
throw new AlreadyExistingException("This mage does already exists");
throw new AlreadyExistingException("This spell does already exists");
}
return await _spellRepository.AddSpellAsync(_mapper.Map<Spell>(dto));
}
@ -37,17 +37,17 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The id is null");
}
var alreadyExistingSpell = await GetAllSpellAsync(new SpellDtoFilter { Id = id });
var alreadyExistingSpell = await GetAllSpellsAsync(new SpellDtoFilter { Id = id });
if (!alreadyExistingSpell.Any())
{
throw new AlreadyExistingException("This mage does not exists");
throw new AlreadyExistingException("This spell does not exists");
}
return await _spellRepository.DeleteSpellAsync(id);
}
public async Task<ICollection<SpellDto>> GetAllSpellAsync(SpellDtoFilter filter)
public async Task<ICollection<SpellDto>> GetAllSpellsAsync(SpellDtoFilter filter)
{
var entities = await _spellRepository.GetAllSpellAsync(_mapper.Map<SpellFilter>(filter));
var entities = await _spellRepository.GetAllSpellsAsync(_mapper.Map<SpellFilter>(filter));
return _mapper.Map<ICollection<SpellDto>>(entities);
}
@ -57,25 +57,25 @@ namespace Liber_Incantamentum.Application.Services.Generals
{
throw new ArgumentNullException("The id is null");
}
var alreadyExistingMage = await GetAllSpellAsync(new SpellDtoFilter { Id = id });
var alreadyExistingMage = await GetAllSpellsAsync(new SpellDtoFilter { Id = id });
if (!alreadyExistingMage.Any())
{
throw new NotFoundException("This mage does not exists");
throw new NotFoundException("This spell does not exists");
}
var mage = await _spellRepository.GetSpellByIdAsync(id);
return _mapper.Map<SpellDto>(mage);
}
public async Task<bool> UpdateSpellAsync(SpellDtoUpdate dto)
public async Task<bool> UpdateSpellAsync(SpellDtoUpdate dto, Guid i)
{
if (dto == null)
{
throw new ArgumentNullException("The DTO received is null");
}
var alreadyExistingSpell = await GetAllSpellAsync(new SpellDtoFilter { Id = dto.Id, Name = dto.Name, Description = dto.Description, Type = dto.Type, CreationDate = dto.CreationDate, MageId = dto.MageId });
var alreadyExistingSpell = await GetAllSpellsAsync(new SpellDtoFilter { Id = dto.Id, Name = dto.Name, Description = dto.Description, Type = dto.Type, CreationDate = dto.CreationDate, MageId = dto.MageId });
if (!alreadyExistingSpell.Any())
{
throw new NotFoundException("This mage does not exists");
throw new NotFoundException("This spell does not exists");
}
return await _spellRepository.UpdateSpellAsync(_mapper.Map<Spell>(dto));
}

View File

@ -1,6 +1,4 @@
using Liber_Incantamentum.Domain.Entities;
namespace Liber_Incantamentum.Domain.Filter
namespace Liber_Incantamentum.Domain.Filter
{
public class MageFilter
{

View File

@ -5,7 +5,7 @@ namespace Liber_Incantamentum.Domain.Repositories
{
public interface IMageRepository
{
Task<ICollection<Mage>?> GetAllMageAsync(MageFilter filterEntity);
Task<ICollection<Mage>> GetAllMagesAsync(MageFilter filterEntity);
Task<bool> UpdateMageAsync(Mage entity);
Task<bool> DeleteMageAsync(Guid id);
Task<bool> AddMageAsync(Mage entity);

View File

@ -5,7 +5,7 @@ namespace Liber_Incantamentum.Domain.Repositories
{
public interface ISpellRepository
{
Task<ICollection<Spell>?> GetAllSpellAsync(SpellFilter filterEntity);
Task<ICollection<Spell>> GetAllSpellsAsync(SpellFilter filterEntity);
Task<bool> UpdateSpellAsync(Spell entity);
Task<bool> DeleteSpellAsync(Guid id);
Task<bool> AddSpellAsync(Spell entity);

View File

@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\Liber_Incantamentum.Domain\Liber_Incantamentum.Domain.csproj" />
<ProjectReference Include="..\Liber_Incantamentum.Application\Liber_Incantamentum.Application.csproj" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,81 @@
namespace Liber_Incantamentum.Infrastructure.Repositories
using Liber_Incantamentum.Application.Exceptions;
using Liber_Incantamentum.Domain.Entities;
using Liber_Incantamentum.Domain.Filter;
using Liber_Incantamentum.Domain.Repositories;
using Liber_Incantamentum.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
namespace Liber_Incantamentum.Infrastructure.Repositories
{
internal class MageRepository
public class MageRepository : IMageRepository
{
private readonly AppDbContext _context;
public MageRepository(AppDbContext context)
{
_context = context;
}
public async Task<bool> AddMageAsync(Mage entity)
{
await _context.Mages.AddAsync(entity);
if (!_context.ChangeTracker.HasChanges()) return false;
return await _context.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteMageAsync(Guid id)
{
var mage = await GetMageByIdAsync(id);
_context.Mages.Remove(mage);
if (!_context.ChangeTracker.HasChanges()) return false;
return await _context.SaveChangesAsync() > 0;
}
public async Task<ICollection<Mage>> GetAllMagesAsync(MageFilter filterEntity)
{
var query = _context.Mages.AsQueryable();
if (filterEntity.Id != Guid.Empty)
query = query.Where(m => m.Id == filterEntity.Id);
if (!string.IsNullOrEmpty(filterEntity.Name))
query = query.Where(m => EF.Functions.ILike(m.Name, filterEntity.Name));
if (!string.IsNullOrEmpty(filterEntity.Rank))
query = query.Where(m => EF.Functions.ILike(m.Rank, filterEntity.Rank));
if (!string.IsNullOrEmpty(filterEntity.Specialisation))
query = query.Where(m => EF.Functions.ILike(m.Specialisation, filterEntity.Specialisation));
var mages = await query.ToListAsync();
return mages;
}
public async Task<Mage> GetMageByIdAsync(Guid id)
{
var mage = await _context.Mages.FindAsync(id);
if (mage == null)
{
throw new NotFoundException($"No mage has been found with this id {id}");
}
return mage;
}
public async Task<bool> UpdateMageAsync(Mage newMageValues)
{
var mageToUpdate = await GetMageByIdAsync(newMageValues.Id);
_context.Entry(mageToUpdate).CurrentValues.SetValues(newMageValues);
foreach (var spell in newMageValues.Spells)
if (!mageToUpdate.Spells.Any(s => s.Id == spell.Id))
mageToUpdate.Spells.Add(spell);
foreach (var spell in mageToUpdate.Spells.ToList())
if (!newMageValues.Spells.Any(s => s.Id == spell.Id))
mageToUpdate.Spells.Remove(spell);
if (!_context.ChangeTracker.HasChanges()) return false;
return await _context.SaveChangesAsync() > 0;
}
}
}

View File

@ -1,6 +1,72 @@
namespace Liber_Incantamentum.Infrastructure.Repositories
using Liber_Incantamentum.Domain.Entities;
using Liber_Incantamentum.Domain.Filter;
using Liber_Incantamentum.Domain.Repositories;
using Liber_Incantamentum.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Liber_Incantamentum.Application.Exceptions;
namespace Liber_Incantamentum.Infrastructure.Repositories
{
internal class SpellRepository
public class SpellRepository : ISpellRepository
{
private readonly AppDbContext _context;
public SpellRepository(AppDbContext context)
{
_context = context;
}
public async Task<bool> AddSpellAsync(Spell entity)
{
await _context.Spells.AddAsync(entity);
return await _context.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteSpellAsync(Guid id)
{
var spell = await GetSpellByIdAsync(id);
_context.Spells.Remove(spell);
return await _context.SaveChangesAsync() > 0;
}
public async Task<ICollection<Spell>> GetAllSpellsAsync(SpellFilter filterEntity)
{
var query = _context.Spells.AsQueryable();
if (filterEntity.Id != Guid.Empty)
query = query.Where(m => m.Id == filterEntity.Id);
if (!string.IsNullOrEmpty(filterEntity.Name))
query = query.Where(m => EF.Functions.ILike(m.Name, filterEntity.Name));
if (!string.IsNullOrEmpty(filterEntity.Description))
query = query.Where(m => EF.Functions.ILike(m.Description, filterEntity.Description));
if (!string.IsNullOrEmpty(filterEntity.Type))
query = query.Where(m => EF.Functions.ILike(m.Type, filterEntity.Type));
if (filterEntity.CreationDate != default)
query = query.Where(m => m.CreationDate == filterEntity.CreationDate);
if (filterEntity.MageId != Guid.Empty)
query = query.Where(m => m.MageId == filterEntity.MageId);
var mages = await query.ToListAsync();
return mages;
}
public async Task<Spell> GetSpellByIdAsync(Guid id)
{
var spell = await _context.Spells.FindAsync(id);
if (spell == null) throw new NotFoundException($"No spell has been found with the id : {id}");
return spell;
}
public async Task<bool> UpdateSpellAsync(Spell entity)
{
var spell = await GetSpellByIdAsync(entity.Id);
_context.Entry(spell).CurrentValues.SetValues(entity);
if (!_context.ChangeTracker.HasChanges()) return false;
return await _context.SaveChangesAsync() > 0;
}
}
}

View File

@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Liber_Incantamentum.API.Controller
{
[Route("api/[controller]")]
[ApiController]
public class Example : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
// POST api/<ValuesController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

View File

@ -0,0 +1,61 @@
using Liber_Incantamentum.Application.DTOs.Mage;
using Liber_Incantamentum.Application.Services.Generals;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Liber_Incantamentum.API.Controller
{
public class MageController : ControllerBase
{
private readonly MageService _mageService;
public MageController(MageService mageService)
{
_mageService = mageService;
}
[HttpGet("/mages")]
public async Task<ActionResult<ICollection<MageDto>>> GetAllMages([FromQuery] MageDtoFilter filter)
{
var mages = await _mageService.GetAllMagesAsync(filter);
if (mages == null || mages.Any()) return Ok(mages);
return NotFound();
}
[HttpGet("/mages/{id}")]
public async Task<ActionResult<MageDto>> GetMage([FromRoute] Guid id)
{
var mage = await _mageService.GetMageByIdAsync(id);
if (mage != null) return Ok(mage);
return NotFound();
}
[HttpPost("/mages")]
public async Task<IActionResult> CreateMage([FromBody] MageDtoCreate mage)
{
if (await _mageService.AddMageAsync(mage)) return NoContent();
return BadRequest();
}
[HttpPut("/mages/{id}")]
public async Task<IActionResult> UpdateMage([FromBody] MageDtoUpdate mage, [FromRoute] Guid id)
{
if (await _mageService.UpdateMageAsync(mage, id)) return NoContent();
return BadRequest();
}
[HttpPatch("/mages/{id}")]
public async Task<IActionResult> PatchMage([FromBody] MageDtoUpdate mage, [FromRoute] Guid id)
{
if (await _mageService.UpdateMageAsync(mage, id)) return NoContent();
return BadRequest();
}
[HttpDelete("/mages/{id}")]
public async Task<IActionResult> DeleteMage([FromRoute] Guid id)
{
if (await _mageService.DeleteMageAsync(id)) return NoContent();
return BadRequest();
}
}
}

View File

@ -0,0 +1,7 @@
namespace Liber_Incantamentum.API.Controller
{
public class SpellController
{
}
}

View File

@ -1,5 +1,4 @@
using FluentValidation;
using AutoMapper;
using Liber_Incantamentum.Application.Services.Mappings;
using Liber_Incantamentum.Application.Services.Validations;