Update application part
This commit is contained in:
parent
39b8312807
commit
2d8d8ed754
@ -1,4 +1,6 @@
|
||||
namespace Liber_Incantamentum.Application.DTOs.Mage
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.DTOs.Mage
|
||||
{
|
||||
public class MageDtoUpdate
|
||||
{
|
||||
@ -6,5 +8,6 @@
|
||||
public required string Name { get; set; }
|
||||
public required string Rank { get; set; }
|
||||
public required string Specialisation { get; set; }
|
||||
public ICollection<SpellDto> Spells { get; set; } = new List<SpellDto>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class SpellDtoFilter
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Type { get; set; }
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
namespace Liber_Incantamentum.Application.Exceptions
|
||||
{
|
||||
public class AlreadyExistingException : Exception
|
||||
{
|
||||
public AlreadyExistingException() { }
|
||||
public AlreadyExistingException(string message) : base(message) { }
|
||||
public AlreadyExistingException(string? message, Exception? innerException) : base(message, innerException) { }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
namespace Liber_Incantamentum.Application.Exceptions
|
||||
{
|
||||
public class ValidationException : Exception
|
||||
{
|
||||
public ValidationException() { }
|
||||
public ValidationException(string message) : base(message) { }
|
||||
public ValidationException(string? message, Exception? innerException) : base(message, innerException) { }
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Interfaces.Generals
|
||||
{
|
||||
public interface ISpellService
|
||||
{
|
||||
Task<ICollection<SpellDto>?> GetAllSpellAsync(SpellDtoFilter filter);
|
||||
Task<bool> UpdateSpellAsync(SpellDtoUpdate dto);
|
||||
Task<bool> DeleteSpellAsync(Guid id);
|
||||
Task<bool> AddSpellAsync(SpellDtoCreate dto);
|
||||
Task<SpellDto> GetSpellByIdAsync(Guid id);
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,13 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Interfaces.Generals
|
||||
namespace Liber_Incantamentum.Application.Interfaces
|
||||
{
|
||||
public interface IMageService
|
||||
{
|
||||
Task<ICollection<MageDto>?> GetAllMageAsync(MageDtoFilter filter);
|
||||
Task<ICollection<MageDto>> GetAllMageAsync(MageDtoFilter filter);
|
||||
Task<bool> UpdateMageAsync(MageDtoUpdate dto);
|
||||
Task<bool> DeleteMageAsync(Guid id);
|
||||
Task<bool> AddMageAsync(MageDtoCreate dto);
|
||||
Task<MageDto> GetMageByIdAsync(Guid id);
|
||||
Task<MageDto>? GetMageByIdAsync(Guid id);
|
||||
}
|
||||
}
|
||||
13
Liber_Incantamentum.Application/Interfaces/ISpellService.cs
Normal file
13
Liber_Incantamentum.Application/Interfaces/ISpellService.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Interfaces
|
||||
{
|
||||
public interface ISpellService
|
||||
{
|
||||
Task<ICollection<SpellDto>> GetAllSpellAsync(SpellDtoFilter filter);
|
||||
Task<bool> UpdateSpellAsync(SpellDtoUpdate dto);
|
||||
Task<bool> DeleteSpellAsync(Guid id);
|
||||
Task<bool> AddSpellAsync(SpellDtoCreate dto);
|
||||
Task<SpellDto>? GetSpellByIdAsync(Guid id);
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Interfaces.Mappings
|
||||
{
|
||||
public interface IMapper
|
||||
{
|
||||
Mage MapMageDtoCreateToMageEntity(MageDtoCreate value);
|
||||
MageFilter MapMageDtoFilterToMageFilter(MageDtoFilter value);
|
||||
Mage MapMageDtoUpdateToMageEntity(MageDtoUpdate value);
|
||||
ICollection<MageDto>? MapMageEntityCollectionToMageDtoCollection(ICollection<Mage>? value);
|
||||
MageDto mapMageEntityToMageDto(Mage mage);
|
||||
Spell MapSpellDtoCreateToSpellEntity(SpellDtoCreate value);
|
||||
SpellFilter MapSpellDtoFilterToSpellFilter(SpellDtoFilter value);
|
||||
Spell MapSpellDtoUpdateToSpellEntity(SpellDtoUpdate value);
|
||||
ICollection<SpellDto>? MapSpellEntityCollectionToSpellDtoCollection(ICollection<Spell>? value);
|
||||
SpellDto mapSpellEntityToSpellDto(Spell spell);
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Interfaces.Validations
|
||||
{
|
||||
public interface IValidator
|
||||
{
|
||||
void ValidateGuid(Guid value);
|
||||
void ValidateMageDto(MageDto value);
|
||||
void ValidateMageDtoCreate(MageDtoCreate value);
|
||||
void ValidateMageDtoUpdate(MageDtoUpdate value);
|
||||
void ValidateMageDtoFilter(MageDtoFilter value);
|
||||
void ValidateSpellDto(SpellDto value);
|
||||
void ValidateSpellDtoCreate(SpellDtoCreate value);
|
||||
void ValidateSpellDtoUpdate(SpellDtoUpdate value);
|
||||
void ValidateSpellDtoFilter(SpellDtoFilter value);
|
||||
}
|
||||
}
|
||||
@ -10,4 +10,9 @@
|
||||
<ProjectReference Include="..\Liber_Incantamentum.Domain\Liber_Incantamentum.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="15.0.1" />
|
||||
<PackageReference Include="FluentValidation" Version="12.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,51 +1,84 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.Interfaces.Generals;
|
||||
using Liber_Incantamentum.Application.Interfaces.Mappings;
|
||||
using Liber_Incantamentum.Application.Interfaces.Validations;
|
||||
using AutoMapper;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.Exceptions;
|
||||
using Liber_Incantamentum.Application.Interfaces;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
using Liber_Incantamentum.Domain.Repositories;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Generals
|
||||
{
|
||||
public class MageService : IMageService
|
||||
{
|
||||
private IValidator _validator;
|
||||
private IMageRepository _mageRepository;
|
||||
private IMapper _mapper;
|
||||
public MageService(IMapper mapper, IMageRepository mageRepository, IValidator validator)
|
||||
private readonly IMageRepository _mageRepository;
|
||||
private readonly IMapper _mapper;
|
||||
public MageService(IMageRepository mageRepository, IMapper mapper)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_mageRepository = mageRepository;
|
||||
_validator = validator;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<bool> AddMageAsync(MageDtoCreate dto)
|
||||
{
|
||||
_validator.ValidateMageDtoCreate(dto);
|
||||
return await _mageRepository.AddMageAsync(_mapper.MapMageDtoCreateToMageEntity(dto));
|
||||
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())
|
||||
{
|
||||
throw new AlreadyExistingException("This mage does already exists");
|
||||
}
|
||||
return await _mageRepository.AddMageAsync(_mapper.Map<Mage>(dto));
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteMageAsync(Guid id)
|
||||
{
|
||||
_validator.ValidateGuid(id);
|
||||
if (id == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("The id is null");
|
||||
}
|
||||
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Id = id });
|
||||
if (!alreadyExistingMage.Any())
|
||||
{
|
||||
throw new NotFoundException("This mage does not exists");
|
||||
}
|
||||
return await _mageRepository.DeleteMageAsync(id);
|
||||
}
|
||||
|
||||
public async Task<ICollection<MageDto>?> GetAllMageAsync(MageDtoFilter filter)
|
||||
public async Task<ICollection<MageDto>> GetAllMageAsync(MageDtoFilter filter)
|
||||
{
|
||||
_validator.ValidateMageDtoFilter(filter);
|
||||
return _mapper.MapMageEntityCollectionToMageDtoCollection(await _mageRepository.GetAllMageAsync(_mapper.MapMageDtoFilterToMageFilter(filter)));
|
||||
var entities = await _mageRepository.GetAllMageAsync(_mapper.Map<MageFilter>(filter));
|
||||
return _mapper.Map<ICollection<MageDto>>(entities);
|
||||
}
|
||||
|
||||
public async Task<MageDto> GetMageByIdAsync(Guid id)
|
||||
public async Task<MageDto>? GetMageByIdAsync(Guid id)
|
||||
{
|
||||
_validator.ValidateGuid(id);
|
||||
return _mapper.mapMageEntityToMageDto(await _mageRepository.GetMageByIdAsync(id));
|
||||
if (id == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("The id is null");
|
||||
}
|
||||
var alreadyExistingMage = await GetAllMageAsync(new MageDtoFilter { Id = id });
|
||||
if (!alreadyExistingMage.Any())
|
||||
{
|
||||
throw new NotFoundException("This mage does not exists");
|
||||
}
|
||||
var mage = await _mageRepository.GetMageByIdAsync(id);
|
||||
return _mapper.Map<MageDto>(mage);
|
||||
}
|
||||
|
||||
public Task<bool> UpdateMageAsync(MageDtoUpdate dto)
|
||||
public async Task<bool> UpdateMageAsync(MageDtoUpdate dto)
|
||||
{
|
||||
_validator.ValidateMageDtoUpdate(dto);
|
||||
return _mageRepository.UpdateMageAsync(_mapper.MapMageDtoUpdateToMageEntity(dto));
|
||||
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())
|
||||
{
|
||||
throw new NotFoundException("This mage does not exists");
|
||||
}
|
||||
return await _mageRepository.UpdateMageAsync(_mapper.Map<Mage>(dto));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,51 +1,83 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using AutoMapper;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
using Liber_Incantamentum.Application.Interfaces.Generals;
|
||||
using Liber_Incantamentum.Application.Interfaces.Mappings;
|
||||
using Liber_Incantamentum.Application.Interfaces.Validations;
|
||||
using Liber_Incantamentum.Application.Exceptions;
|
||||
using Liber_Incantamentum.Application.Interfaces;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
using Liber_Incantamentum.Domain.Repositories;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Generals
|
||||
{
|
||||
public class SpellService : ISpellService
|
||||
{
|
||||
private IValidator _validator;
|
||||
private ISpellRepository _spellRepository;
|
||||
private IMapper _mapper;
|
||||
public SpellService(IValidator validator, ISpellRepository spellRepository, IMapper mapper)
|
||||
private readonly ISpellRepository _spellRepository;
|
||||
private readonly IMapper _mapper;
|
||||
public SpellService(ISpellRepository spellRepository, IMapper mapper)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_validator = validator;
|
||||
_spellRepository = spellRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
public async Task<bool> AddSpellAsync(SpellDtoCreate dto)
|
||||
{
|
||||
_validator.ValidateSpellDtoCreate(dto);
|
||||
return await _spellRepository.AddSpellAsync(_mapper.MapSpellDtoCreateToSpellEntity(dto));
|
||||
if (dto == null)
|
||||
{
|
||||
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 });
|
||||
if (alreadyExistingSpell.Any())
|
||||
{
|
||||
throw new AlreadyExistingException("This mage does already exists");
|
||||
}
|
||||
return await _spellRepository.AddSpellAsync(_mapper.Map<Spell>(dto));
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteSpellAsync(Guid id)
|
||||
{
|
||||
_validator.ValidateGuid(id);
|
||||
if (id == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("The id is null");
|
||||
}
|
||||
var alreadyExistingSpell = await GetAllSpellAsync(new SpellDtoFilter { Id = id });
|
||||
if (!alreadyExistingSpell.Any())
|
||||
{
|
||||
throw new AlreadyExistingException("This mage does not exists");
|
||||
}
|
||||
return await _spellRepository.DeleteSpellAsync(id);
|
||||
}
|
||||
|
||||
public async Task<ICollection<SpellDto>?> GetAllSpellAsync(SpellDtoFilter filter)
|
||||
public async Task<ICollection<SpellDto>> GetAllSpellAsync(SpellDtoFilter filter)
|
||||
{
|
||||
_validator.ValidateSpellDtoFilter(filter);
|
||||
return _mapper.MapSpellEntityCollectionToSpellDtoCollection(await _spellRepository.GetAllSpellAsync(_mapper.MapSpellDtoFilterToSpellFilter(filter)));
|
||||
var entities = await _spellRepository.GetAllSpellAsync(_mapper.Map<SpellFilter>(filter));
|
||||
return _mapper.Map<ICollection<SpellDto>>(entities);
|
||||
}
|
||||
|
||||
public async Task<SpellDto> GetSpellByIdAsync(Guid id)
|
||||
public async Task<SpellDto>? GetSpellByIdAsync(Guid id)
|
||||
{
|
||||
_validator.ValidateGuid(id);
|
||||
return _mapper.mapSpellEntityToSpellDto(await _spellRepository.GetSpellByIdAsync(id));
|
||||
if (id == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("The id is null");
|
||||
}
|
||||
var alreadyExistingMage = await GetAllSpellAsync(new SpellDtoFilter { Id = id });
|
||||
if (!alreadyExistingMage.Any())
|
||||
{
|
||||
throw new NotFoundException("This mage does not exists");
|
||||
}
|
||||
var mage = await _spellRepository.GetSpellByIdAsync(id);
|
||||
return _mapper.Map<SpellDto>(mage);
|
||||
}
|
||||
|
||||
public Task<bool> UpdateSpellAsync(SpellDtoUpdate dto)
|
||||
public async Task<bool> UpdateSpellAsync(SpellDtoUpdate dto)
|
||||
{
|
||||
_validator.ValidateSpellDtoUpdate(dto);
|
||||
return _spellRepository.UpdateSpellAsync(_mapper.MapSpellDtoUpdateToSpellEntity(dto));
|
||||
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 });
|
||||
if (!alreadyExistingSpell.Any())
|
||||
{
|
||||
throw new NotFoundException("This mage does not exists");
|
||||
}
|
||||
return await _spellRepository.UpdateSpellAsync(_mapper.Map<Spell>(dto));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
using AutoMapper;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Mappings
|
||||
{
|
||||
public class MageMappingProfile : Profile
|
||||
{
|
||||
public MageMappingProfile()
|
||||
{
|
||||
CreateMap<MageDtoCreate, Mage>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Spells, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<MageDtoUpdate, Mage>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<MageDtoFilter, MageFilter>()
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<MageDto, Mage>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,141 +0,0 @@
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
using Liber_Incantamentum.Application.Interfaces.Mappings;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Mappings
|
||||
{
|
||||
public class Mapper : IMapper
|
||||
{
|
||||
public Mage MapMageDtoCreateToMageEntity(MageDtoCreate value)
|
||||
{
|
||||
return new Mage()
|
||||
{
|
||||
Name = value.Name,
|
||||
Rank = value.Rank,
|
||||
Specialisation = value.Specialisation
|
||||
};
|
||||
}
|
||||
|
||||
public MageFilter MapMageDtoFilterToMageFilter(MageDtoFilter value)
|
||||
{
|
||||
return new MageFilter()
|
||||
{
|
||||
Id = value.Id,
|
||||
Name = value.Name,
|
||||
Rank = value.Rank,
|
||||
Specialisation = value.Specialisation
|
||||
};
|
||||
}
|
||||
|
||||
public Mage MapMageDtoUpdateToMageEntity(MageDtoUpdate value)
|
||||
{
|
||||
return new Mage()
|
||||
{
|
||||
Name = value.Name,
|
||||
Rank = value.Rank,
|
||||
Specialisation = value.Specialisation
|
||||
};
|
||||
}
|
||||
|
||||
public ICollection<MageDto>? MapMageEntityCollectionToMageDtoCollection(ICollection<Mage>? value)
|
||||
{
|
||||
var mageDtoCollection = new List<MageDto>();
|
||||
foreach (var entity in value)
|
||||
{
|
||||
MageDto dto = new MageDto()
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name,
|
||||
Rank = entity.Rank,
|
||||
Specialisation = entity.Specialisation
|
||||
};
|
||||
|
||||
mageDtoCollection.Add(dto);
|
||||
}
|
||||
|
||||
return mageDtoCollection;
|
||||
}
|
||||
|
||||
public MageDto mapMageEntityToMageDto(Mage mage)
|
||||
{
|
||||
return new MageDto()
|
||||
{
|
||||
Id = mage.Id,
|
||||
Name = mage.Name,
|
||||
Rank = mage.Rank,
|
||||
Specialisation = mage.Specialisation
|
||||
};
|
||||
}
|
||||
|
||||
public Spell MapSpellDtoCreateToSpellEntity(SpellDtoCreate value)
|
||||
{
|
||||
return new Spell()
|
||||
{
|
||||
Name = value.Name,
|
||||
Description = value.Description,
|
||||
Type = value.Type,
|
||||
CreationDate = value.CreationDate,
|
||||
MageId = value.MageId
|
||||
};
|
||||
}
|
||||
|
||||
public SpellFilter MapSpellDtoFilterToSpellFilter(SpellDtoFilter value)
|
||||
{
|
||||
return new SpellFilter()
|
||||
{
|
||||
Id = value.Id,
|
||||
Name = value.Name,
|
||||
Description = value.Description,
|
||||
Type = value.Type,
|
||||
CreationDate = value.CreationDate,
|
||||
MageId = value.MageId
|
||||
};
|
||||
}
|
||||
|
||||
public Spell MapSpellDtoUpdateToSpellEntity(SpellDtoUpdate value)
|
||||
{
|
||||
return new Spell()
|
||||
{
|
||||
Name = value.Name,
|
||||
Description = value.Description,
|
||||
Type = value.Type,
|
||||
CreationDate = value.CreationDate,
|
||||
MageId = value.MageId
|
||||
};
|
||||
}
|
||||
|
||||
public ICollection<SpellDto>? MapSpellEntityCollectionToSpellDtoCollection(ICollection<Spell>? value)
|
||||
{
|
||||
var spellDtoCollection = new List<SpellDto>();
|
||||
foreach (var x in value)
|
||||
{
|
||||
var dto = new SpellDto()
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Type = x.Type,
|
||||
CreationDate = x.CreationDate,
|
||||
MageId = x.MageId
|
||||
};
|
||||
spellDtoCollection.Add(dto);
|
||||
}
|
||||
return spellDtoCollection;
|
||||
}
|
||||
|
||||
public SpellDto mapSpellEntityToSpellDto(Spell spell)
|
||||
{
|
||||
return new SpellDto()
|
||||
{
|
||||
Id = spell.Id,
|
||||
Name = spell.Name,
|
||||
Description = spell.Description,
|
||||
Type = spell.Type,
|
||||
CreationDate = spell.CreationDate,
|
||||
MageId = spell.MageId
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
using AutoMapper;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
using Liber_Incantamentum.Domain.Entities;
|
||||
using Liber_Incantamentum.Domain.Filter;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Mappings
|
||||
{
|
||||
public class SpellMappingProfile : Profile
|
||||
{
|
||||
public SpellMappingProfile()
|
||||
{
|
||||
CreateMap<SpellDto, Spell>()
|
||||
.ForMember(x => x.Id, x => x.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<SpellDtoCreate, Spell>()
|
||||
.ForMember(x => x.Id, x => x.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<SpellDtoFilter, SpellFilter>()
|
||||
.ReverseMap();
|
||||
|
||||
CreateMap<SpellDtoUpdate, Spell>()
|
||||
.ForMember(x => x.Id, x => x.Ignore())
|
||||
.ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class MageDtoCreateValidator : AbstractValidator<MageDtoCreate>
|
||||
{
|
||||
public MageDtoCreateValidator()
|
||||
{
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The name is required");
|
||||
RuleFor(m => m.Rank)
|
||||
.NotEmpty().WithMessage("The Rank is required");
|
||||
RuleFor(m => m.Specialisation)
|
||||
.NotEmpty().WithMessage("The specialisation is required");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class MageDtoFilterValidator : AbstractValidator<MageDtoFilter>
|
||||
{
|
||||
public MageDtoFilterValidator()
|
||||
{
|
||||
When(m => m.Id.HasValue, () =>
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The id cannot be empty if provided");
|
||||
});
|
||||
When(m => !string.IsNullOrEmpty(m.Name), () =>
|
||||
{
|
||||
RuleFor(m => m.Name)
|
||||
.MinimumLength(2).WithMessage("Name must be at least 2 characters.");
|
||||
});
|
||||
When(m => !string.IsNullOrEmpty(m.Rank), () =>
|
||||
{
|
||||
RuleFor(m => m.Rank)
|
||||
.MinimumLength(2).WithMessage("Name must be at least 2 characters.");
|
||||
});
|
||||
When(m => !string.IsNullOrEmpty(m.Specialisation), () =>
|
||||
{
|
||||
RuleFor(m => m.Specialisation)
|
||||
.MinimumLength(2).WithMessage("Name must be at least 2 characters.");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class MageDtoUpdateValidator : AbstractValidator<MageDtoUpdate>
|
||||
{
|
||||
public MageDtoUpdateValidator()
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The Id is required");
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The Name is required");
|
||||
RuleFor(m => m.Rank)
|
||||
.NotEmpty().WithMessage("The Rank is required");
|
||||
RuleFor(m => m.Specialisation)
|
||||
.NotEmpty().WithMessage("The Specialisation is required");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Mage;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class MageDtoValidator : AbstractValidator<MageDto>
|
||||
{
|
||||
public MageDtoValidator()
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The Id is required");
|
||||
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The Name is required");
|
||||
|
||||
RuleFor(m => m.Rank)
|
||||
.NotEmpty().WithMessage("The Rank is required");
|
||||
|
||||
RuleFor(m => m.Specialisation)
|
||||
.NotEmpty().WithMessage("The Specialisation is required");
|
||||
|
||||
RuleFor(m => m.Spells)
|
||||
.NotNull().WithMessage("Spell collection is required")
|
||||
.NotEmpty().WithMessage("At least one spell is required");
|
||||
|
||||
RuleForEach(m => m.Spells)
|
||||
.SetValidator(new SpellDtoValidator());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class SpellDtoCreateValidator : AbstractValidator<SpellDtoCreate>
|
||||
{
|
||||
public SpellDtoCreateValidator()
|
||||
{
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The Name is required");
|
||||
RuleFor(m => m.Description)
|
||||
.NotEmpty().WithMessage("The description is required");
|
||||
RuleFor(m => m.Type)
|
||||
.NotEmpty().WithMessage("The type is required");
|
||||
RuleFor(m => m.CreationDate)
|
||||
.LessThanOrEqualTo(DateTime.UtcNow);
|
||||
RuleFor(m => m.MageId)
|
||||
.NotEmpty().WithMessage("The mage id is required");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class SpellDtoFilterValidator : AbstractValidator<SpellDtoFilter>
|
||||
{
|
||||
public SpellDtoFilterValidator()
|
||||
{
|
||||
When(m => m.Id.HasValue, () =>
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The id cannot be empty if provided");
|
||||
});
|
||||
When(m => !string.IsNullOrEmpty(m.Name), () =>
|
||||
{
|
||||
RuleFor(m => m.Name)
|
||||
.MinimumLength(2).WithMessage("Name must be at least 2 characters.");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class SpellDtoUpdateValidator : AbstractValidator<SpellDtoUpdate>
|
||||
{
|
||||
public SpellDtoUpdateValidator()
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The Id cannot be empty");
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The Name cannot be empty");
|
||||
RuleFor(m => m.Description)
|
||||
.NotEmpty().WithMessage("The Description cannot be empty");
|
||||
RuleFor(m => m.Type)
|
||||
.NotEmpty().WithMessage("The Type cannot be empty");
|
||||
RuleFor(m => m.CreationDate)
|
||||
.LessThanOrEqualTo(DateTime.UtcNow);
|
||||
RuleFor(m => m.MageId)
|
||||
.NotEmpty().WithMessage("The mage id cannot be empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using Liber_Incantamentum.Application.DTOs.Spell;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class SpellDtoValidator : AbstractValidator<SpellDto>
|
||||
{
|
||||
public SpellDtoValidator()
|
||||
{
|
||||
RuleFor(m => m.Id)
|
||||
.NotEmpty().WithMessage("The Id cannot be empty");
|
||||
RuleFor(m => m.Name)
|
||||
.NotEmpty().WithMessage("The Name cannot be empty");
|
||||
RuleFor(m => m.Description)
|
||||
.NotEmpty().WithMessage("The Description cannot be empty");
|
||||
RuleFor(m => m.Type)
|
||||
.NotEmpty().WithMessage("The Type cannot be empty");
|
||||
RuleFor(m => m.CreationDate)
|
||||
.LessThanOrEqualTo(DateTime.UtcNow);
|
||||
RuleFor(m => m.MageId)
|
||||
.NotEmpty().WithMessage("The mage id cannot be empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
using Liber_Incantamentum.Application.Interfaces.Validations;
|
||||
|
||||
namespace Liber_Incantamentum.Application.Services.Validations
|
||||
{
|
||||
public class Validator : IValidator
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,10 @@
|
||||
public class Mage
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Mage()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
}
|
||||
public required string Name { get; set; }
|
||||
public required string Rank { get; set; }
|
||||
public required string Specialisation { get; set; }
|
||||
|
||||
@ -3,6 +3,10 @@
|
||||
public class Spell
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Spell()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
}
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public required string Type { get; set; }
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
@ -18,7 +18,14 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
|
||||
<PackageReference Include="FluentValidation" Version="12.0.0" />
|
||||
<PackageReference Include="AutoMapper" Version="15.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Liber_Incantamentum.Application\Liber_Incantamentum.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,3 +1,10 @@
|
||||
using FluentValidation;
|
||||
using AutoMapper;
|
||||
using Liber_Incantamentum.Application.Services.Mappings;
|
||||
using Liber_Incantamentum.Application.Services.Validations;
|
||||
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
@ -6,6 +13,10 @@ builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddValidatorsFromAssemblyContaining<MageDtoCreateValidator>();
|
||||
|
||||
builder.Services.AddAutoMapper(cfg => { }, typeof(MageMappingProfile).Assembly);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user