forked from MarcosHCK/sep3cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/amandanoris/sep3cs
- Loading branch information
Showing
138 changed files
with
8,557 additions
and
317 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/Application/Challenges/Commands/AddPlayer/AddPlayerCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using DataClash.Application.Common.Exceptions; | ||
using DataClash.Application.Common.Interfaces; | ||
using DataClash.Application.Common.Security; | ||
using DataClash.Domain.Entities; | ||
using DataClash.Domain.Enums; | ||
using DataClash.Domain.Events; | ||
using MediatR; | ||
|
||
namespace DataClash.Application.Challenges.Commands.AddPlayer | ||
{ | ||
[Authorize] | ||
public record AddPlayerCommand : IRequest | ||
{ | ||
public long ChallengeId { get; init; } | ||
public long PlayerId { get; init; } | ||
public long WonThrophies { get; init; } | ||
} | ||
|
||
public class AddPlayerCommandHandler : IRequestHandler<AddPlayerCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly ICurrentPlayerService _currentPlayer; | ||
private readonly ICurrentUserService _currentUser; | ||
private readonly IIdentityService _identityService; | ||
|
||
public AddPlayerCommandHandler (IApplicationDbContext context, ICurrentPlayerService currentPlayer, ICurrentUserService currentUser, IIdentityService identityService) | ||
{ | ||
_context = context; | ||
_currentPlayer = currentPlayer; | ||
_currentUser = currentUser; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task Handle (AddPlayerCommand request, CancellationToken cancellationToken) | ||
{ | ||
if (_currentPlayer.PlayerId != request.PlayerId | ||
&& await _identityService.IsInRoleAsync (_currentUser.UserId!, Roles.Administrator)) | ||
throw new ForbiddenAccessException (); | ||
else | ||
{ | ||
var challenge = await _context.Challenges.FindAsync (new object[] { request.ChallengeId }, cancellationToken) | ||
?? throw new NotFoundException (nameof (Challenge), request.ChallengeId); | ||
|
||
var playerChallenge = new PlayerChallenge | ||
{ | ||
ChallengeId = request.ChallengeId, | ||
PlayerId = request.PlayerId, | ||
WonThrophies = request.WonThrophies, | ||
}; | ||
|
||
challenge.AddDomainEvent (new PlayerAddedEvent<PlayerChallenge> (playerChallenge)); | ||
|
||
await _context.PlayerChallenges.AddAsync (playerChallenge, cancellationToken); | ||
await _context.SaveChangesAsync (cancellationToken); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Application/Challenges/Commands/AddPlayer/AddPlayerCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using FluentValidation; | ||
|
||
namespace DataClash.Application.Challenges.Commands.AddPlayer | ||
{ | ||
public class AddPlayerCommandValidator : AbstractValidator<AddPlayerCommand> | ||
{ | ||
public AddPlayerCommandValidator () | ||
{ | ||
RuleFor (v => v.ChallengeId).NotEmpty (); | ||
RuleFor (v => v.PlayerId).NotEmpty (); | ||
RuleFor (v => v.WonThrophies).GreaterThanOrEqualTo (0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Application/Challenges/Commands/RemovePlayer/RemovePlayerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using DataClash.Application.Common.Exceptions; | ||
using DataClash.Application.Common.Interfaces; | ||
using DataClash.Application.Common.Security; | ||
using DataClash.Domain.Entities; | ||
using DataClash.Domain.Enums; | ||
using DataClash.Domain.Events; | ||
using MediatR; | ||
|
||
namespace DataClash.Application.Challenges.Commands.RemovePlayer | ||
{ | ||
[Authorize] | ||
public record RemovePlayerCommand : IRequest | ||
{ | ||
public long ChallengeId { get; init; } | ||
public long PlayerId { get; init; } | ||
} | ||
|
||
public class RemovePlayerCommandHandler : IRequestHandler<RemovePlayerCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly ICurrentPlayerService _currentPlayer; | ||
private readonly ICurrentUserService _currentUser; | ||
private readonly IIdentityService _identityService; | ||
|
||
public RemovePlayerCommandHandler (IApplicationDbContext context, ICurrentPlayerService currentPlayer, ICurrentUserService currentUser, IIdentityService identityService) | ||
{ | ||
_context = context; | ||
_currentPlayer = currentPlayer; | ||
_currentUser = currentUser; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task Handle (RemovePlayerCommand request, CancellationToken cancellationToken) | ||
{ | ||
if (_currentPlayer.PlayerId != request.PlayerId | ||
&& await _identityService.IsInRoleAsync (_currentUser.UserId!, Roles.Administrator)) | ||
throw new ForbiddenAccessException (); | ||
else | ||
{ | ||
var challenge = await _context.Challenges.FindAsync (new object[] { request.ChallengeId }, cancellationToken) | ||
?? throw new NotFoundException (nameof (Challenge), request.ChallengeId); | ||
var playerChallenge = await _context.PlayerChallenges.FindAsync (new object[] { request.ChallengeId, request.PlayerId }, cancellationToken) | ||
?? throw new NotFoundException (nameof (PlayerChallenge), new object[] { request.ChallengeId, request.PlayerId }); | ||
|
||
_context.PlayerChallenges.Remove (playerChallenge); | ||
challenge.AddDomainEvent (new PlayerRemovedEvent<PlayerChallenge> (playerChallenge)); | ||
await _context.SaveChangesAsync (cancellationToken); | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Application/Challenges/Commands/RemovePlayer/RemovePlayerCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using FluentValidation; | ||
|
||
namespace DataClash.Application.Challenges.Commands.RemovePlayer | ||
{ | ||
public class RemovePlayerCommandValidator : AbstractValidator<RemovePlayerCommand> | ||
{ | ||
public RemovePlayerCommandValidator () | ||
{ | ||
RuleFor (v => v.ChallengeId).NotEmpty (); | ||
RuleFor (v => v.PlayerId).NotEmpty (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Application/Challenges/Commands/UpdatePlayer/UpdatePlayerCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using DataClash.Application.Challenges.Commands.AddPlayer; | ||
using DataClash.Application.Common.Exceptions; | ||
using DataClash.Application.Common.Interfaces; | ||
using DataClash.Application.Common.Security; | ||
using DataClash.Domain.Entities; | ||
using DataClash.Domain.Enums; | ||
using MediatR; | ||
|
||
namespace DataClash.Application.Challenges.Commands.UpdatePlayer | ||
{ | ||
[Authorize] | ||
public record UpdatePlayerCommand : AddPlayerCommand; | ||
|
||
public class UpdatePlayerCommandHandler : IRequestHandler<UpdatePlayerCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly ICurrentPlayerService _currentPlayer; | ||
private readonly ICurrentUserService _currentUser; | ||
private readonly IIdentityService _identityService; | ||
|
||
public UpdatePlayerCommandHandler (IApplicationDbContext context, ICurrentPlayerService currentPlayer, ICurrentUserService currentUser, IIdentityService identityService) | ||
{ | ||
_context = context; | ||
_currentPlayer = currentPlayer; | ||
_currentUser = currentUser; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task Handle (UpdatePlayerCommand request, CancellationToken cancellationToken) | ||
{ | ||
if (_currentPlayer.PlayerId != request.PlayerId | ||
&& await _identityService.IsInRoleAsync (_currentUser.UserId!, Roles.Administrator)) | ||
throw new ForbiddenAccessException (); | ||
else | ||
{ | ||
var challenge = await _context.Challenges.FindAsync (new object[] { request.ChallengeId }, cancellationToken) | ||
?? throw new NotFoundException (nameof (Challenge), request.ChallengeId); | ||
var playerChallenge = await _context.PlayerChallenges.FindAsync (new object[] { request.ChallengeId, request.PlayerId }, cancellationToken) | ||
?? throw new NotFoundException (nameof (PlayerChallenge), new object[] { request.ChallengeId, request.PlayerId }); | ||
|
||
playerChallenge.WonThrophies = request.WonThrophies; | ||
|
||
await _context.SaveChangesAsync (cancellationToken); | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Application/Challenges/Commands/UpdatePlayer/UpdatePlayerCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (c) 2023-2025 | ||
* This file is part of sep3cs. | ||
* | ||
* sep3cs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* sep3cs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using FluentValidation; | ||
|
||
namespace DataClash.Application.Challenges.Commands.UpdatePlayer | ||
{ | ||
public class UpdatePlayerCommandValidator : AbstractValidator<UpdatePlayerCommand> | ||
{ | ||
public UpdatePlayerCommandValidator () | ||
{ | ||
RuleFor (v => v.ChallengeId).NotEmpty (); | ||
RuleFor (v => v.PlayerId).NotEmpty (); | ||
RuleFor (v => v.WonThrophies).GreaterThanOrEqualTo (0); | ||
} | ||
} | ||
} |
Oops, something went wrong.