Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/amandanoris/sep3cs
Browse files Browse the repository at this point in the history
  • Loading branch information
amandanoris committed Dec 3, 2023
2 parents 4b04da0 + c1922f8 commit c973ed2
Show file tree
Hide file tree
Showing 138 changed files with 8,557 additions and 317 deletions.
74 changes: 74 additions & 0 deletions src/Application/Challenges/Commands/AddPlayer/AddPlayerCommand.cs
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);
}
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public record CreateChallengeCommand : IRequest<long>
public TimeSpan Duration { get; init; }
public long MaxLooses { get; init; }
public long MinLevel { get; init; }
public string? Name { get; init; }
public string Name { get; init; } = null!;

}

public class CreateChallengeCommandHandler : IRequestHandler<CreateChallengeCommand, long>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public CreateChallengeCommandValidator ()
{

RuleFor (v => v.BeginDay).NotEmpty ();
RuleFor (v => v.Bounty).NotEmpty ();
RuleFor (v => v.Cost).NotEmpty ();
RuleFor (v => v.Bounty).GreaterThanOrEqualTo(0);
RuleFor (v => v.Cost).GreaterThanOrEqualTo(0);
RuleFor (v => v.Description).NotEmpty ();
RuleFor (v => v.Duration).NotEmpty ();
RuleFor (v => v.MaxLooses).NotEmpty ();
RuleFor (v => v.MinLevel).NotEmpty ();
RuleFor (v => v.MaxLooses).GreaterThanOrEqualTo(0);
RuleFor (v => v.MinLevel).GreaterThanOrEqualTo(0);
RuleFor (v => v.Name).NotEmpty ();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace DataClash.Application.Challenges.Commands.DeleteChallenge
{
[Authorize (Roles = "Administrator")]
public record DeleteChallengeCommand (long Id) : IRequest;

public class DeleteChallengeCommandHandler : IRequestHandler<DeleteChallengeCommand>
{
private readonly IApplicationDbContext _context;
Expand Down
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);
}
}
}
}
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 ();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public record UpdateChallengeCommand : IRequest
public TimeSpan Duration { get; init; }
public long MaxLooses { get; init; }
public long MinLevel { get; init; }
public string? Name { get; init; }
public string Name { get; init; } = null!;


}
Expand All @@ -60,6 +60,7 @@ public async Task Handle (UpdateChallengeCommand request, CancellationToken canc
entity.MinLevel= request.MinLevel;
entity.Name= request.Name;


entity.AddDomainEvent (new ChallengeUpdatedEvent (entity));
await _context.SaveChangesAsync (cancellationToken);
}
Expand Down
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);
}
}
}
}
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);
}
}
}
Loading

0 comments on commit c973ed2

Please sign in to comment.