diff --git a/Liber_Incantamentum.Application/Interfaces/IMageService.cs b/Liber_Incantamentum.Application/Interfaces/IMageService.cs index 95739fa..340878b 100644 --- a/Liber_Incantamentum.Application/Interfaces/IMageService.cs +++ b/Liber_Incantamentum.Application/Interfaces/IMageService.cs @@ -4,8 +4,8 @@ namespace Liber_Incantamentum.Application.Interfaces { public interface IMageService { - Task> GetAllMageAsync(MageDtoFilter filter); - Task UpdateMageAsync(MageDtoUpdate dto); + Task> GetAllMagesAsync(MageDtoFilter filter); + Task UpdateMageAsync(MageDtoUpdate dto, Guid id); Task DeleteMageAsync(Guid id); Task AddMageAsync(MageDtoCreate dto); Task? GetMageByIdAsync(Guid id); diff --git a/Liber_Incantamentum.Application/Interfaces/ISpellService.cs b/Liber_Incantamentum.Application/Interfaces/ISpellService.cs index 8d59733..e9d87cd 100644 --- a/Liber_Incantamentum.Application/Interfaces/ISpellService.cs +++ b/Liber_Incantamentum.Application/Interfaces/ISpellService.cs @@ -4,8 +4,8 @@ namespace Liber_Incantamentum.Application.Interfaces { public interface ISpellService { - Task> GetAllSpellAsync(SpellDtoFilter filter); - Task UpdateSpellAsync(SpellDtoUpdate dto); + Task> GetAllSpellsAsync(SpellDtoFilter filter); + Task UpdateSpellAsync(SpellDtoUpdate dto, Guid id); Task DeleteSpellAsync(Guid id); Task AddSpellAsync(SpellDtoCreate dto); Task? GetSpellByIdAsync(Guid id); diff --git a/Liber_Incantamentum.Application/Services/Generals/MageService.cs b/Liber_Incantamentum.Application/Services/Generals/MageService.cs index b2d5ef6..e95e730 100644 --- a/Liber_Incantamentum.Application/Services/Generals/MageService.cs +++ b/Liber_Incantamentum.Application/Services/Generals/MageService.cs @@ -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> GetAllMageAsync(MageDtoFilter filter) + public async Task> GetAllMagesAsync(MageDtoFilter filter) { - var entities = await _mageRepository.GetAllMageAsync(_mapper.Map(filter)); + var entities = await _mageRepository.GetAllMagesAsync(_mapper.Map(filter)); return _mapper.Map>(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(mage); } - public async Task UpdateMageAsync(MageDtoUpdate dto) + public async Task 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"); } diff --git a/Liber_Incantamentum.Application/Services/Generals/SpellService.cs b/Liber_Incantamentum.Application/Services/Generals/SpellService.cs index 05e8b71..b5ffcba 100644 --- a/Liber_Incantamentum.Application/Services/Generals/SpellService.cs +++ b/Liber_Incantamentum.Application/Services/Generals/SpellService.cs @@ -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(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> GetAllSpellAsync(SpellDtoFilter filter) + public async Task> GetAllSpellsAsync(SpellDtoFilter filter) { - var entities = await _spellRepository.GetAllSpellAsync(_mapper.Map(filter)); + var entities = await _spellRepository.GetAllSpellsAsync(_mapper.Map(filter)); return _mapper.Map>(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(mage); } - public async Task UpdateSpellAsync(SpellDtoUpdate dto) + public async Task 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(dto)); } diff --git a/Liber_Incantamentum.Domain/Filter/MageFilter.cs b/Liber_Incantamentum.Domain/Filter/MageFilter.cs index 3481ba8..39354be 100644 --- a/Liber_Incantamentum.Domain/Filter/MageFilter.cs +++ b/Liber_Incantamentum.Domain/Filter/MageFilter.cs @@ -1,6 +1,4 @@ -using Liber_Incantamentum.Domain.Entities; - -namespace Liber_Incantamentum.Domain.Filter +namespace Liber_Incantamentum.Domain.Filter { public class MageFilter { diff --git a/Liber_Incantamentum.Domain/Repositories/IMageRepository.cs b/Liber_Incantamentum.Domain/Repositories/IMageRepository.cs index 9d99760..984f116 100644 --- a/Liber_Incantamentum.Domain/Repositories/IMageRepository.cs +++ b/Liber_Incantamentum.Domain/Repositories/IMageRepository.cs @@ -5,7 +5,7 @@ namespace Liber_Incantamentum.Domain.Repositories { public interface IMageRepository { - Task?> GetAllMageAsync(MageFilter filterEntity); + Task> GetAllMagesAsync(MageFilter filterEntity); Task UpdateMageAsync(Mage entity); Task DeleteMageAsync(Guid id); Task AddMageAsync(Mage entity); diff --git a/Liber_Incantamentum.Domain/Repositories/ISpellRepository.cs b/Liber_Incantamentum.Domain/Repositories/ISpellRepository.cs index 22d758d..9fb1e9d 100644 --- a/Liber_Incantamentum.Domain/Repositories/ISpellRepository.cs +++ b/Liber_Incantamentum.Domain/Repositories/ISpellRepository.cs @@ -5,7 +5,7 @@ namespace Liber_Incantamentum.Domain.Repositories { public interface ISpellRepository { - Task?> GetAllSpellAsync(SpellFilter filterEntity); + Task> GetAllSpellsAsync(SpellFilter filterEntity); Task UpdateSpellAsync(Spell entity); Task DeleteSpellAsync(Guid id); Task AddSpellAsync(Spell entity); diff --git a/Liber_Incantamentum.Infrastructure/Liber_Incantamentum.Infrastructure.csproj b/Liber_Incantamentum.Infrastructure/Liber_Incantamentum.Infrastructure.csproj index 0ef0e05..760b8fb 100644 --- a/Liber_Incantamentum.Infrastructure/Liber_Incantamentum.Infrastructure.csproj +++ b/Liber_Incantamentum.Infrastructure/Liber_Incantamentum.Infrastructure.csproj @@ -24,6 +24,7 @@ + diff --git a/Liber_Incantamentum.Infrastructure/Repositories/MageRepository.cs b/Liber_Incantamentum.Infrastructure/Repositories/MageRepository.cs index 3d711ed..d23878a 100644 --- a/Liber_Incantamentum.Infrastructure/Repositories/MageRepository.cs +++ b/Liber_Incantamentum.Infrastructure/Repositories/MageRepository.cs @@ -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 AddMageAsync(Mage entity) + { + await _context.Mages.AddAsync(entity); + if (!_context.ChangeTracker.HasChanges()) return false; + return await _context.SaveChangesAsync() > 0; + } + + public async Task 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> 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 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 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; + } } } diff --git a/Liber_Incantamentum.Infrastructure/Repositories/SpellRepository.cs b/Liber_Incantamentum.Infrastructure/Repositories/SpellRepository.cs index 50c646a..022f00c 100644 --- a/Liber_Incantamentum.Infrastructure/Repositories/SpellRepository.cs +++ b/Liber_Incantamentum.Infrastructure/Repositories/SpellRepository.cs @@ -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 AddSpellAsync(Spell entity) + { + await _context.Spells.AddAsync(entity); + return await _context.SaveChangesAsync() > 0; + } + + public async Task DeleteSpellAsync(Guid id) + { + var spell = await GetSpellByIdAsync(id); + _context.Spells.Remove(spell); + return await _context.SaveChangesAsync() > 0; + } + + public async Task> 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 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 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; + } + } } diff --git a/Liber_Incantamentum/Controller/Example.cs b/Liber_Incantamentum/Controller/Example.cs new file mode 100644 index 0000000..a710f5e --- /dev/null +++ b/Liber_Incantamentum/Controller/Example.cs @@ -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/ + [HttpGet] + public IEnumerable Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api//5 + [HttpGet("{id}")] + public string Get(int id) + { + return "value"; + } + + // POST api/ + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Liber_Incantamentum/Controller/MageController.cs b/Liber_Incantamentum/Controller/MageController.cs new file mode 100644 index 0000000..e43c1a7 --- /dev/null +++ b/Liber_Incantamentum/Controller/MageController.cs @@ -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>> 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> GetMage([FromRoute] Guid id) + { + var mage = await _mageService.GetMageByIdAsync(id); + if (mage != null) return Ok(mage); + return NotFound(); + } + + [HttpPost("/mages")] + public async Task CreateMage([FromBody] MageDtoCreate mage) + { + if (await _mageService.AddMageAsync(mage)) return NoContent(); + return BadRequest(); + } + + [HttpPut("/mages/{id}")] + public async Task UpdateMage([FromBody] MageDtoUpdate mage, [FromRoute] Guid id) + { + if (await _mageService.UpdateMageAsync(mage, id)) return NoContent(); + return BadRequest(); + } + + [HttpPatch("/mages/{id}")] + public async Task PatchMage([FromBody] MageDtoUpdate mage, [FromRoute] Guid id) + { + if (await _mageService.UpdateMageAsync(mage, id)) return NoContent(); + return BadRequest(); + } + + [HttpDelete("/mages/{id}")] + public async Task DeleteMage([FromRoute] Guid id) + { + if (await _mageService.DeleteMageAsync(id)) return NoContent(); + return BadRequest(); + } + } +} diff --git a/Liber_Incantamentum/Controller/SpellController.cs b/Liber_Incantamentum/Controller/SpellController.cs new file mode 100644 index 0000000..86a9ad4 --- /dev/null +++ b/Liber_Incantamentum/Controller/SpellController.cs @@ -0,0 +1,7 @@ +namespace Liber_Incantamentum.API.Controller +{ + public class SpellController + { + + } +} diff --git a/Liber_Incantamentum/Program.cs b/Liber_Incantamentum/Program.cs index f034c11..6eb9fc2 100644 --- a/Liber_Incantamentum/Program.cs +++ b/Liber_Incantamentum/Program.cs @@ -1,5 +1,4 @@ using FluentValidation; -using AutoMapper; using Liber_Incantamentum.Application.Services.Mappings; using Liber_Incantamentum.Application.Services.Validations;