Skip to content

Commit

Permalink
Merge pull request #3 from RieBi/update-1
Browse files Browse the repository at this point in the history
Update 1
  • Loading branch information
RieBi authored Aug 4, 2024
2 parents c028304 + d271912 commit f0fc9bc
Show file tree
Hide file tree
Showing 91 changed files with 299 additions and 393 deletions.
4 changes: 3 additions & 1 deletion Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="MediatR" Version="12.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
</ItemGroup>

Expand All @@ -20,7 +20,9 @@
<ItemGroup>
<Using Include="AutoMapper" />
<Using Include="Data" />
<Using Include="FluentResults" />
<Using Include="MediatR" />
<Using Include="Microsoft.EntityFrameworkCore" />
</ItemGroup>

</Project>
12 changes: 0 additions & 12 deletions Application/Authorization/DependencyInjection.cs

This file was deleted.

53 changes: 0 additions & 53 deletions Application/Authorization/IsOwnerHandler.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
namespace Application.Commands.BattleCommands;
public class CreateNamedBattleCommand(int characterId1, int characterId2, string battleName) : IRequest<int>
{
public int CharacterId1 { get; } = characterId1;
public int CharacterId2 { get; } = characterId2;
public string BattleName { get; } = battleName;
}
public record CreateNamedBattleCommand(int CharacterId1, int CharacterId2, string BattleName) : IRequest<Result<int>>;
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Application.Services.BattleManagement;

namespace Application.Commands.BattleCommands;
public class CreateNamedBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateNamedBattleCommand, int>
internal class CreateNamedBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateNamedBattleCommand, Result<int>>
{
private readonly IBattleService _battleService = battleService;

public async Task<int> Handle(CreateNamedBattleCommand request, CancellationToken cancellationToken)
public async Task<Result<int>> Handle(CreateNamedBattleCommand request, CancellationToken cancellationToken)
{
var battle = await _battleService.ExecuteBattleAsync(request.CharacterId1, request.CharacterId2, request.BattleName);

if (battle is null)
return -1;
return Result.Fail("Battle could not be created");

return battle.Id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
using Data.Models;

namespace Application.Commands.BattleCommands;
public class CreateRandomBattleCommand(int characterId) : IRequest<int>
{
public int CharacterId { get; set; } = characterId;
}
namespace Application.Commands.BattleCommands;
public record CreateRandomBattleCommand(int CharacterId) : IRequest<int>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Application.Services.BattleManagement;
using Microsoft.EntityFrameworkCore;

namespace Application.Commands.BattleCommands;
public class CreateRandomBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateRandomBattleCommand, int>
internal class CreateRandomBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateRandomBattleCommand, int>
{
private readonly IBattleService _battleService = battleService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
namespace Application.Commands.BattleCommands;
public class CreateSpecificBattleCommand(int characterAId, int characterBId) : IRequest<int>
{
public int CharacterAId { get; set; } = characterAId;
public int CharacterBId { get; set; } = characterBId;
}
public record CreateSpecificBattleCommand(int CharacterAId, int CharacterBId) : IRequest<Result<int>>;
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

using Application.Services.BattleManagement;
using Application.Services.BattleManagement;

namespace Application.Commands.BattleCommands;
public class CreateSpecificBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateSpecificBattleCommand, int>
internal class CreateSpecificBattleCommandHandler(IBattleService battleService) : IRequestHandler<CreateSpecificBattleCommand, Result<int>>
{
private readonly IBattleService _battleService = battleService;

public async Task<int> Handle(CreateSpecificBattleCommand request, CancellationToken cancellationToken)
public async Task<Result<int>> Handle(CreateSpecificBattleCommand request, CancellationToken cancellationToken)
{
if (request.CharacterAId == request.CharacterBId)
return -1;
return Result.Fail("A character cannot fight itself.");

var battle = await _battleService.ExecuteBattleAsync(request.CharacterAId, request.CharacterBId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Security.Claims;

namespace Application.Commands.CharacterCommands;
public class CreateCharacterCommand(CharacterCreateDto characterCreateDto, ClaimsPrincipal user) : IRequest<int>
{
public CharacterCreateDto CharacterCreateDto { get; } = characterCreateDto;
public ClaimsPrincipal User { get; } = user;
}
public record CreateCharacterCommand(CharacterCreateDto CharacterCreateDto, ClaimsPrincipal User) : IRequest<Result<int>>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
using Application.Services.UserManagement;

namespace Application.Commands.CharacterCommands;
public class CreateCharacterCommandHandler(DataContext context, IUserInfoService userInfo) : IRequestHandler<CreateCharacterCommand, int>
internal class CreateCharacterCommandHandler(DataContext context, IUserInfoService userInfo) : IRequestHandler<CreateCharacterCommand, Result<int>>
{
private readonly DataContext _context = context;
private readonly IUserInfoService _userInfo = userInfo;

public async Task<int> Handle(CreateCharacterCommand request, CancellationToken cancellationToken)
public async Task<Result<int>> Handle(CreateCharacterCommand request, CancellationToken cancellationToken)
{
var character = await new CharacterConverter(_context)
.TryConvertFromDto(request.CharacterCreateDto, cancellationToken);

if (character is null)
return -1;
return Result.Fail("Couldn't create a character");

character.OwnerEmail = await _userInfo.GetUserEmailAsync(request.User);

await _context.Characters.AddAsync(character, cancellationToken);
_context.Characters.Add(character);
await _context.SaveChangesAsync(cancellationToken);

return character.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
namespace Application.Commands.CharacterCommands;
public class DeleteCharacterCommand(int id) : IRequest<Unit>
{
public int Id { get; } = id;
}
public record DeleteCharacterCommand(int Id) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Application.Commands.CharacterCommands;
public class DeleteCharacterCommandHandler(DataContext context) : IRequestHandler<DeleteCharacterCommand, Unit>
internal class DeleteCharacterCommandHandler(DataContext context) : IRequestHandler<DeleteCharacterCommand, Unit>
{
private readonly DataContext _context = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
namespace Application.Commands.CharacterCommands;
public class EditCharacterCommand(int id, CharacterCreateDto characterCreateDto) : IRequest<Unit>
{
public int Id { get; } = id;
public CharacterCreateDto characterCreateDto { get; } = characterCreateDto;
}
public record EditCharacterCommand(int Id, CharacterCreateDto CharacterCreateDto) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Application.Converters;
using Microsoft.EntityFrameworkCore;

namespace Application.Commands.CharacterCommands;
public class EditCharacterCommandHandler(DataContext context) : IRequestHandler<EditCharacterCommand, Unit>
internal class EditCharacterCommandHandler(DataContext context) : IRequestHandler<EditCharacterCommand, Unit>
{
private readonly DataContext _context = context;

Expand All @@ -16,7 +15,7 @@ public async Task<Unit> Handle(EditCharacterCommand request, CancellationToken c
return Unit.Value;

character = await new CharacterConverter(_context)
.TryConvertFromDto(request.characterCreateDto, cancellationToken, character);
.TryConvertFromDto(request.CharacterCreateDto, cancellationToken, character);

if (character is null)
return Unit.Value;
Expand Down
6 changes: 1 addition & 5 deletions Application/Commands/CountryCommands/CreateCountryCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Security.Claims;

namespace Application.Commands.CountryCommands;
public class CreateCountryCommand(CountryCreateDto countryCreateDto, ClaimsPrincipal user) : IRequest<int>
{
public CountryCreateDto CountryCreateDto { get; } = countryCreateDto;
public ClaimsPrincipal User { get; } = user;
}
public record CreateCountryCommand(CountryCreateDto CountryCreateDto, ClaimsPrincipal User) : IRequest<Result<int>>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
using Application.Services.UserManagement;

namespace Application.Commands.CountryCommands;
public class CreateCountryCommandHandler(DataContext context, IUserInfoService userInfo) : IRequestHandler<CreateCountryCommand, int>
internal class CreateCountryCommandHandler(DataContext context, IUserInfoService userInfo) : IRequestHandler<CreateCountryCommand, Result<int>>
{
private readonly DataContext _context = context;
private readonly IUserInfoService _userInfo = userInfo;

public async Task<int> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
public async Task<Result<int>> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
{
var country = await new CountryConverter(_context)
.TryConvertFromDto(request.CountryCreateDto, cancellationToken);

if (country is null)
return -1;
return Result.Fail("Couldn't create a country");

country.OwnerEmail = await _userInfo.GetUserEmailAsync(request.User);

await _context.Countries.AddAsync(country, cancellationToken);
_context.Countries.Add(country);
await _context.SaveChangesAsync(cancellationToken);

return country.Id;
Expand Down
5 changes: 1 addition & 4 deletions Application/Commands/CountryCommands/DeleteCountryCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
namespace Application.Commands.CountryCommands;
public class DeleteCountryCommand(int id) : IRequest<Unit>
{
public int Id { get; } = id;
}
public record DeleteCountryCommand(int Id) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Application.Commands.CountryCommands;
public class DeleteCountryCommandHandler(DataContext context) : IRequestHandler<DeleteCountryCommand, Unit>
internal class DeleteCountryCommandHandler(DataContext context) : IRequestHandler<DeleteCountryCommand, Unit>
{
private readonly DataContext _context = context;

Expand Down
6 changes: 1 addition & 5 deletions Application/Commands/CountryCommands/EditCountryCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
namespace Application.Commands.CountryCommands;
public class EditCountryCommand(int id, CountryCreateDto countryCreateDto) : IRequest<Unit>
{
public int Id { get; } = id;
public CountryCreateDto CountryCreateDto { get; } = countryCreateDto;
}
public record EditCountryCommand(int Id, CountryCreateDto CountryCreateDto) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Application.Converters;
using Microsoft.EntityFrameworkCore;

namespace Application.Commands.CountryCommands;
public class EditCountryCommandHandler(DataContext context, IMapper mapper) : IRequestHandler<EditCountryCommand, Unit>
internal class EditCountryCommandHandler(DataContext context) : IRequestHandler<EditCountryCommand, Unit>
{
private readonly DataContext _context = context;
private readonly IMapper _mapper = mapper;

public async Task<Unit> Handle(EditCountryCommand request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Data.Models;

namespace Application.Commands.NaturalResourceCommands;
public class CreateNaturalResourceCommand(NaturalResource resource) : IRequest<int>
{
public NaturalResource Resource { get; } = resource;
}
public record CreateNaturalResourceCommand(NaturalResource Resource) : IRequest<int>;
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Data.Models;

namespace Application.Commands.NaturalResourceCommands;
public class CreateNaturalResourceCommandHandler(DataContext context) : IRequestHandler<CreateNaturalResourceCommand, int>
internal class CreateNaturalResourceCommandHandler(DataContext context) : IRequestHandler<CreateNaturalResourceCommand, int>
{
private readonly DataContext _context = context;

public async Task<int> Handle(CreateNaturalResourceCommand request, CancellationToken cancellationToken)
{
await _context.NaturalResources.AddAsync(request.Resource, cancellationToken);
_context.NaturalResources.Add(request.Resource);
await _context.SaveChangesAsync(cancellationToken);

return request.Resource.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
namespace Application.Commands.NaturalResourceCommands;
public class DeleteNaturalResourceCommand(int id) : IRequest<Unit>
{
public int Id { get; } = id;
}
public record DeleteNaturalResourceCommand(int Id) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Application.Commands.NaturalResourceCommands;
public class DeleteNaturalResourceCommandHandler(DataContext context) : IRequestHandler<DeleteNaturalResourceCommand, Unit>
internal class DeleteNaturalResourceCommandHandler(DataContext context) : IRequestHandler<DeleteNaturalResourceCommand, Unit>
{
private readonly DataContext _context = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Data.Models;

namespace Application.Commands.NaturalResourceCommands;
public class EditNaturalResourceCommand(NaturalResource naturalResource) : IRequest<Unit>
{
public NaturalResource Resource { get; } = naturalResource;
}
public record EditNaturalResourceCommand(NaturalResource Resource) : IRequest<Unit>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Application.Commands.NaturalResourceCommands;
public class EditNaturalResourceCommandHandler(DataContext context) : IRequestHandler<EditNaturalResourceCommand, Unit>
internal class EditNaturalResourceCommandHandler(DataContext context) : IRequestHandler<EditNaturalResourceCommand, Unit>
{
private readonly DataContext _context = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Data.Models;

namespace Application.Commands.SpecialtyCommands;
public class CreateSpecialtyCommand(Specialty specialty) : IRequest<int>
{
public Specialty Specialty { get; } = specialty;
}
public record CreateSpecialtyCommand(Specialty Specialty) : IRequest<int>;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Application.Commands.SpecialtyCommands;
public class CreateSpecialtyCommandHandler(DataContext context) : IRequestHandler<CreateSpecialtyCommand, int>
internal class CreateSpecialtyCommandHandler(DataContext context) : IRequestHandler<CreateSpecialtyCommand, int>
{
private readonly DataContext _context = context;

public async Task<int> Handle(CreateSpecialtyCommand request, CancellationToken cancellationToken)
{
await _context.Specialties.AddAsync(request.Specialty, cancellationToken);
_context.Specialties.Add(request.Specialty);
await _context.SaveChangesAsync(cancellationToken);

return request.Specialty.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
namespace Application.Commands.SpecialtyCommands;
public class DeleteSpecialtyCommand(int id) : IRequest<Unit>
{
public int Id { get; } = id;
}
public record DeleteSpecialtyCommand(int Id) : IRequest<Unit>;
Loading

0 comments on commit f0fc9bc

Please sign in to comment.