-
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 pull request #16 from GuilhermeBley/main
New endpoints configurations
- Loading branch information
Showing
36 changed files
with
982 additions
and
177 deletions.
There are no files selected for viewing
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
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
94 changes: 94 additions & 0 deletions
94
...ningApi.Application/Commands/Training/FinishTrainingPeriod/FinishTrainingPeriodHandler.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,94 @@ | ||
| ||
using Bl.Gym.TrainingApi.Application.Providers; | ||
using Bl.Gym.TrainingApi.Application.Repositories; | ||
using Bl.Gym.TrainingApi.Application.Services; | ||
|
||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.FinishTrainingPeriod; | ||
|
||
public class FinishTrainingPeriodHandler | ||
: IRequestHandler<FinishTrainingPeriodRequest, FinishTrainingPeriodResponse> | ||
{ | ||
private readonly IIdentityProvider _identityProvider; | ||
private readonly TrainingContext _context; | ||
private readonly GymRoleCheckerService _gymChecker; | ||
private readonly ILogger<FinishTrainingPeriodHandler> _logger; | ||
|
||
public FinishTrainingPeriodHandler( | ||
IIdentityProvider identityProvider, | ||
TrainingContext context, | ||
GymRoleCheckerService gymChecker, | ||
ILogger<FinishTrainingPeriodHandler> logger) | ||
{ | ||
_identityProvider = identityProvider; | ||
_context = context; | ||
_gymChecker = gymChecker; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<FinishTrainingPeriodResponse> Handle( | ||
FinishTrainingPeriodRequest request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var user = await _identityProvider.GetCurrentAsync(cancellationToken); | ||
|
||
user.ThrowIfDoesntContainRole(Domain.Security.UserClaim.SeeTraining); | ||
|
||
var periodToUpdate = await _context | ||
.TrainingsPeriod | ||
.Where(e => e.Id == request.PeriodId) | ||
.FirstOrDefaultAsync(cancellationToken) | ||
?? throw CoreException.CreateByCode(CoreExceptionCode.NotFound); | ||
|
||
var periodEntity = periodToUpdate.MapToEntity(); | ||
|
||
periodEntity.Update(periodEntity.StartedAt ?? DateTime.UtcNow, DateTime.UtcNow); | ||
|
||
await _gymChecker.ThrowIfUserIsntInTheSectionAsync(user.RequiredUserId(), periodToUpdate.SectionId, cancellationToken); | ||
|
||
var sheetFound = await | ||
(from sheet in _context.UserTrainingSheets.AsNoTracking() | ||
join section in _context.TrainingSections.AsNoTracking() | ||
on sheet.Id equals section.UserTrainingSheetId | ||
select sheet).FirstOrDefaultAsync(cancellationToken) | ||
?? throw CoreException.CreateByCode(CoreExceptionCode.NotFound); | ||
|
||
if (sheetFound.Status != Domain.Enum.UserTrainingStatus.InProgress) | ||
{ | ||
_logger.LogInformation("Sheet {0} was not started.", sheetFound.Id); | ||
throw CoreException.CreateByCode(CoreExceptionCode.ThisUserSheetIsntStarted); | ||
} | ||
|
||
var sectionFound = await _context | ||
.TrainingSections | ||
.AsNoTracking() | ||
.Where(e => e.Id == periodToUpdate.SectionId) | ||
.FirstOrDefaultAsync(cancellationToken) | ||
?? throw CoreException.CreateByCode(CoreExceptionCode.NotFound); | ||
|
||
var entityToUpdate = sectionFound.MapToEntity(); | ||
|
||
entityToUpdate.UpdateCurrentDaysCount(entityToUpdate.CurrentDaysCount) | ||
.EnsureSuccess(); | ||
|
||
await using var transaction = await _context.Database.BeginTransactionAsync(cancellationToken); | ||
|
||
var affetedRows = await _context | ||
.TrainingSections | ||
.Where(e => e.Id == periodToUpdate.SectionId) | ||
.Where(e => e.ConcurrencyStamp == entityToUpdate.ConcurrencyStamp) | ||
.ExecuteUpdateAsync(setter => setter.SetProperty(p => p.CurrentDaysCount, entityToUpdate.CurrentDaysCount)); | ||
|
||
periodToUpdate.EndedAt = periodEntity.EndedAt; | ||
periodToUpdate.UpdatedAt = periodEntity.UpdatedAt; | ||
periodToUpdate.StartedAt = periodEntity.StartedAt; | ||
periodToUpdate.Observation = periodEntity.Observation; | ||
|
||
if (affetedRows != 0) | ||
throw CoreException.CreateByCode(CoreExceptionCode.ThisEntityWasAlreadyUpdateByAnotherSource); | ||
|
||
await _context.SaveChangesAsync(); | ||
await transaction.CommitAsync(); | ||
|
||
return new(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ningApi.Application/Commands/Training/FinishTrainingPeriod/FinishTrainingPeriodRequest.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,5 @@ | ||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.FinishTrainingPeriod; | ||
|
||
public record FinishTrainingPeriodRequest( | ||
Guid PeriodId) | ||
: IRequest<FinishTrainingPeriodResponse>; |
3 changes: 3 additions & 0 deletions
3
...ingApi.Application/Commands/Training/FinishTrainingPeriod/FinishTrainingPeriodResponse.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,3 @@ | ||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.FinishTrainingPeriod; | ||
|
||
public record FinishTrainingPeriodResponse(); |
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
72 changes: 72 additions & 0 deletions
72
...ainingApi.Application/Commands/Training/StartTrainingPeriod/StartTrainingPeriodHandler.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,72 @@ | ||
| ||
using Bl.Gym.TrainingApi.Application.Model.Training; | ||
using Bl.Gym.TrainingApi.Application.Providers; | ||
using Bl.Gym.TrainingApi.Application.Repositories; | ||
using Bl.Gym.TrainingApi.Application.Services; | ||
using Bl.Gym.TrainingApi.Domain.Entities.Training; | ||
|
||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.StartTrainingPeriod; | ||
|
||
public class StartTrainingPeriodHandler | ||
: IRequestHandler<StartTrainingPeriodRequest, StartTrainingPeriodResponse> | ||
{ | ||
private readonly TrainingContext _trainingContext; | ||
private readonly GymRoleCheckerService _gymRoleCheckerService; | ||
private readonly IIdentityProvider _identityProvider; | ||
private readonly ILogger<StartTrainingPeriodHandler> _logger; | ||
|
||
public StartTrainingPeriodHandler( | ||
TrainingContext trainingContext, | ||
GymRoleCheckerService gymRoleCheckerService, | ||
IIdentityProvider identityProvider, | ||
ILogger<StartTrainingPeriodHandler> logger) | ||
{ | ||
_trainingContext = trainingContext; | ||
_gymRoleCheckerService = gymRoleCheckerService; | ||
_identityProvider = identityProvider; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<StartTrainingPeriodResponse> Handle( | ||
StartTrainingPeriodRequest request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var user = await _identityProvider.GetCurrentAsync(cancellationToken); | ||
|
||
var userId = user.RequiredUserId(); | ||
|
||
var containsLastNotFinished = await _trainingContext | ||
.TrainingsPeriod | ||
.AsNoTracking() | ||
.Where(e => e.UserId == userId) | ||
.Where(e => e.EndedAt == null) | ||
.AnyAsync(cancellationToken); | ||
|
||
if (containsLastNotFinished) | ||
{ | ||
_logger.LogInformation("There is already an opened training period to user {0}.", userId); | ||
throw CoreException.CreateByCode(CoreExceptionCode.Conflict); | ||
} | ||
|
||
await _gymRoleCheckerService.ThrowIfUserIsntInTheSectionAsync(userId, request.TrainingSectionId, cancellationToken); | ||
|
||
var entity = TrainingUserPeriod.Create( | ||
id: Guid.NewGuid(), | ||
userId: userId, | ||
sectionId: request.TrainingSectionId, | ||
startedAt: DateTime.UtcNow, | ||
endedAt: null, | ||
observation: string.Empty, | ||
createdAt: DateTime.UtcNow, | ||
updatedAt: DateTime.UtcNow) | ||
.RequiredResult; | ||
|
||
var entityResult = await _trainingContext | ||
.TrainingsPeriod | ||
.AddAsync(TrainingUserPeriodModel.MapFromEntity(entity), cancellationToken); | ||
|
||
await _trainingContext.SaveChangesAsync(); | ||
|
||
return new(entityResult.Entity.Id); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ainingApi.Application/Commands/Training/StartTrainingPeriod/StartTrainingPeriodRequest.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,5 @@ | ||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.StartTrainingPeriod; | ||
|
||
public record StartTrainingPeriodRequest( | ||
Guid TrainingSectionId) | ||
: IRequest<StartTrainingPeriodResponse>; |
4 changes: 4 additions & 0 deletions
4
...iningApi.Application/Commands/Training/StartTrainingPeriod/StartTrainingPeriodResponse.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,4 @@ | ||
namespace Bl.Gym.TrainingApi.Application.Commands.Training.StartTrainingPeriod; | ||
|
||
public record StartTrainingPeriodResponse( | ||
Guid PeriodIdCreated); |
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
1 change: 1 addition & 0 deletions
1
...ds/Training/UpdateCurrentDaysCountFromSection/UpdateCurrentDaysCountFromSectionRequest.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
51 changes: 51 additions & 0 deletions
51
src/training-api/Bl.Gym.TrainingApi.Application/Model/Training/TrainingUserPeriodModel.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,51 @@ | ||
using Bl.Gym.TrainingApi.Domain.Entities.Identity; | ||
using Bl.Gym.TrainingApi.Domain.Entities.Training; | ||
using static System.Collections.Specialized.BitVector32; | ||
|
||
namespace Bl.Gym.TrainingApi.Application.Model.Training; | ||
|
||
/// <summary> | ||
/// This model represents an relation between User and Training Section. | ||
/// </summary> | ||
public class TrainingUserPeriodModel | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid UserId { get; set; } | ||
public Guid SectionId { get; set; } | ||
|
||
public DateTime? StartedAt { get; set; } | ||
public DateTime? EndedAt { get; set; } | ||
public string? Observation { get; set; } | ||
|
||
public DateTime UpdatedAt { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
|
||
public TrainingUserPeriod MapToEntity() | ||
{ | ||
return TrainingUserPeriod.Create( | ||
id: Id, | ||
userId: UserId, | ||
sectionId: SectionId, | ||
startedAt: StartedAt, | ||
endedAt: EndedAt, | ||
observation: Observation, | ||
updatedAt: UpdatedAt, | ||
createdAt: CreatedAt) | ||
.RequiredResult; | ||
} | ||
|
||
public static TrainingUserPeriodModel MapFromEntity(TrainingUserPeriod entity) | ||
{ | ||
return new() | ||
{ | ||
CreatedAt = entity.CreatedAt, | ||
Id = entity.Id, | ||
EndedAt = entity.EndedAt, | ||
Observation = entity.Observation, | ||
SectionId = entity.SectionId, | ||
StartedAt = entity.StartedAt, | ||
UpdatedAt = entity.UpdatedAt, | ||
UserId = entity.UserId, | ||
}; | ||
} | ||
} |
Oops, something went wrong.