Skip to content

Commit

Permalink
Apply api high quality rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
kasper090288 committed Mar 21, 2024
1 parent f4a9d18 commit d8a8296
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Football.Management.Application.Abstractions;
using Football.Management.Domain.Entities;
using Football.Management.Domain.Identities;
using Football.Management.Domain.Repositories;
using Football.Management.Domain.Repositories.Players;
using Football.Management.Domain.ValueObjects;

namespace Football.Management.Application.Players.Commands.CreatePlayer;
Expand All @@ -16,41 +16,41 @@ public CreatePlayerCommandHander(IPlayerRepository playerRepository)
_playerRepository = playerRepository;
}

public async Task<Result<PlayerId>> Handle(CreatePlayerCommand request, CancellationToken cancellationToken)
public Task<Result<PlayerId>> Handle(CreatePlayerCommand request, CancellationToken cancellationToken)
{
var firstName = PlayerFirstName.Create(request.FirstName);

if (firstName.IsFailed)
{
return firstName.ToResult<PlayerId>();
return Task.FromResult(firstName.ToResult<PlayerId>());
}

var lastName = PlayerLastName.Create(request.LastName);

if (lastName.IsFailed)
{
return lastName.ToResult<PlayerId>();
return Task.FromResult(lastName.ToResult<PlayerId>());
}

var attackSkill = PlayerAttackSkill.Create(request.AttackSkill);

if (attackSkill.IsFailed)
{
return attackSkill.ToResult<PlayerId>();
return Task.FromResult(attackSkill.ToResult<PlayerId>());
}

var midfieldSkill = PlayerMidfieldSkill.Create(request.MidfieldSkill);

if (midfieldSkill.IsFailed)
{
return midfieldSkill.ToResult<PlayerId>();
return Task.FromResult(midfieldSkill.ToResult<PlayerId>());
}

var defenseSkill = PlayerDefenseSkill.Create(request.DefenseSkill);

if (defenseSkill.IsFailed)
{
return defenseSkill.ToResult<PlayerId>();
return Task.FromResult(defenseSkill.ToResult<PlayerId>());
}

var player = Player.Create(
Expand All @@ -62,11 +62,24 @@ public async Task<Result<PlayerId>> Handle(CreatePlayerCommand request, Cancella

if (player.IsFailed)
{
return player.ToResult<PlayerId>();
return Task.FromResult(player.ToResult<PlayerId>());
}

await _playerRepository.AddAsync(player.Value, cancellationToken);
return _playerRepository
.AddAsync(player.Value, cancellationToken)
.ContinueWith(t =>
{
if (t.IsFaulted)
{
return Result.Fail<PlayerId>("Task has failed.");
}

if (t.Result.IsFailed)
{
return t.Result.ToResult<PlayerId>();
}

return player.Value.Id;
return Result.Ok(player.Value.Id);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentResults;
using Football.Management.Application.Abstractions;
using Football.Management.Domain.Entities;
using Football.Management.Domain.Repositories;
using Football.Management.Domain.Repositories.Players;

namespace Football.Management.Application.Players.Queries.GetPlayerById;

Expand All @@ -14,15 +14,8 @@ public GetPlayerByIdQueryHandler(IPlayerRepository playerRepository)
_playerRepository = playerRepository;
}

public async Task<Result<Player>> Handle(GetPlayerByIdQuery request, CancellationToken cancellationToken)
public Task<Result<Player>> Handle(GetPlayerByIdQuery request, CancellationToken cancellationToken)
{
var player = await _playerRepository.GetAsync(request.PlayerId, cancellationToken);

if (player is null)
{
return Result.Fail("Player was not found.");
}

return player;
return _playerRepository.GetAsync(request.PlayerId, cancellationToken);
}
}
10 changes: 0 additions & 10 deletions Football.Management.Domain/Repositories/IPlayerRepository.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using Football.Management.Domain.Entities;

namespace Football.Management.Domain.Repositories.Players.Errors;

public class PlayerExists(Player player) : Error
{
public Player Player { get; } = player;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using Football.Management.Domain.Identities;

namespace Football.Management.Domain.Repositories.Players.Errors;

public class PlayerNotFound(PlayerId PlayerId) : Error
{
public PlayerId PlayerId { get; } = PlayerId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FluentResults;
using Football.Management.Domain.Entities;
using Football.Management.Domain.Identities;

namespace Football.Management.Domain.Repositories.Players;

public interface IPlayerRepository
{
Task<Result> AddAsync(Player player, CancellationToken cancellationToken);
Task<Result<Player>> GetAsync(PlayerId playerId, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentResults" Version="3.15.2" />
</ItemGroup>

</Project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using FluentResults;
using Football.Management.Domain.Entities;
using Football.Management.Domain.Identities;
using Football.Management.Domain.Repositories.Players;
using Football.Management.Domain.Repositories.Players.Errors;
using Football.Management.Domain.ValueObjects;

namespace Football.Management.Persistence.Repositories.Players;

public sealed class PlayerRepositoryInMemory : IPlayerRepository
{
private List<Player> _players = new();

public PlayerRepositoryInMemory()
{
var player = Player.Create(
PlayerFirstName.Create("Kamil").ValueOrDefault,
PlayerLastName.Create("Grabara").ValueOrDefault,
PlayerAttackSkill.Create(0).ValueOrDefault,
PlayerMidfieldSkill.Create(5).ValueOrDefault,
PlayerDefenseSkill.Create(10).ValueOrDefault);

_players.Add(player.Value);
}

public async Task<Result> AddAsync(Player player, CancellationToken cancellationToken)
{
var playerResult = await GetAsync(player.Id, cancellationToken);

if (playerResult.IsSuccess)
{
return Result.Fail(new PlayerExists(player));
}

_players.Add(player);

return Result.Ok();
}

public Task<Result<Player>> GetAsync(PlayerId playerId, CancellationToken cancellationToken)
{
var player = _players.FirstOrDefault(i => i.Id.Id == playerId.Id);

if (player is null)
{
return Task.FromResult(Result.Fail<Player>(new PlayerNotFound(playerId)));
}

return Task.FromResult(Result.Ok(player));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using Football.Management.Domain.Identities;
using Football.Management.Application.Players.Queries.GetPlayerById;
using Football.Management.Presentation.Players.Responses;
using Football.Management.Domain.Repositories.Players.Errors;

namespace Football.Management.Presentation.Players.Controllers;

[ApiController]
[Route("api/players")]
[Route("api/player")]
public sealed class PlayersController : ControllerBase
{
private readonly ISender _sender;
Expand All @@ -20,11 +21,11 @@ public PlayersController(ISender sender)
}

[HttpPost]
public async Task<IActionResult> CreatePlayer(CreatePlayerRequest request, CancellationToken cancellationToken)
public async Task<IActionResult> CreatePlayer(PlayerPostRequest request, CancellationToken cancellationToken)
{
var command = new CreatePlayerCommand(request.FirstName, request.LastName, request.AttackSkill, request.MidfieldSkill, request.DefenseSkill);
var result = await _sender.Send(command, cancellationToken);
return result.IsSuccess ? Ok(result.Value) : BadRequest(result.Errors);
return result.IsSuccess ? Ok(result.Value) : BadRequest(new { result.Errors });
}

[HttpGet("{playerId}")]
Expand All @@ -44,6 +45,10 @@ public async Task<IActionResult> GetPlayerById(Guid playerId, CancellationToken
player.DefenseSkill.Value);
return Ok(response);
}
return NotFound(result.Errors);
if (result.Errors.Select(e => e.GetType()).Contains(typeof(PlayerNotFound)))
{
return NoContent();
}
return Ok(result.Errors);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Football.Management.Presentation.Players.Requests;

public sealed record PlayerPostRequest(string FirstName, string LastName, int AttackSkill, int MidfieldSkill, int DefenseSkill);
4 changes: 2 additions & 2 deletions Football.Management.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Football.Management.Domain.Repositories;
using Football.Management.Domain.Repositories.Players;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -15,7 +15,7 @@
// .AddClasses(false)
// .AsImplementedInterfaces()
// .WithScopedLifetime());
builder.Services.AddSingleton<IPlayerRepository, Football.Management.Persistence.Repositories.PlayerRepositoryInMemory>();
builder.Services.AddSingleton<IPlayerRepository, Football.Management.Persistence.Repositories.Players.PlayerRepositoryInMemory>();
builder.Services.AddMediatR(conf => conf.RegisterServicesFromAssembly(Football.Management.Application.AssemblyReference.Assembly));
builder.Services.AddControllers().AddApplicationPart(Football.Management.Presentation.AssemblyReference.Assembly);
builder.Services.AddSwaggerGen();
Expand Down

0 comments on commit d8a8296

Please sign in to comment.