-
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.
- Loading branch information
1 parent
4cf49bc
commit fe52ca3
Showing
27 changed files
with
464 additions
and
23 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
6 changes: 6 additions & 0 deletions
6
...tion/Modules.Question.Application/Question/QuestionSet/Create/CreateQuestionSetCommand.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,6 @@ | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Create; | ||
|
||
public sealed record CreateQuestionSetCommand(string Name, string? SetCode , string? Details) : ICommand<Result<QuestionSetResponse>>; |
29 changes: 29 additions & 0 deletions
29
...dules.Question.Application/Question/QuestionSet/Create/CreateQuestionSetCommandHandler.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 @@ | ||
using AutoMapper; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using Modules.Question.Core.QuestionAggregate; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Create; | ||
internal sealed class CreateQuestionSetCommandHandler(IQuestionSetRepository repository, IUnitOfWork unitOfWork, IMapper mapper) : ICommandHandler<CreateQuestionSetCommand, Result<QuestionSetResponse>> | ||
{ | ||
/// <summary>Handles a request</summary> | ||
/// <param name="command">The request</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>Response from the request</returns> | ||
public async Task<Result<QuestionSetResponse>> Handle(CreateQuestionSetCommand command, CancellationToken cancellationToken) | ||
{ | ||
var questionSet = Core.QuestionAggregate.QuestionSet.Create(command.Name, command.SetCode, command.Details); | ||
|
||
if (!questionSet.IsSuccess || questionSet.Value is null) | ||
{ | ||
return questionSet.Error; | ||
} | ||
|
||
var set = questionSet.Value; | ||
|
||
repository.Add(set); | ||
await unitOfWork.CommitAsync(cancellationToken); | ||
|
||
return mapper.Map<Core.QuestionAggregate.QuestionSet, QuestionSetResponse>(set); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...les.Question.Application/Question/QuestionSet/Create/CreateQuestionSetCommandValidator.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,28 @@ | ||
using FluentValidation; | ||
using Modules.Question.Core.QuestionAggregate; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Create; | ||
public sealed class CreateQuestionSetCommandValidator : AbstractValidator<CreateQuestionSetCommand> | ||
{ | ||
public CreateQuestionSetCommandValidator(IQuestionSetRepository repository) | ||
{ | ||
RuleFor(x => x.Name) | ||
.NotEmpty() | ||
.WithMessage("Name can not be empty") | ||
.Length(5, 50) | ||
.WithMessage("Name can not be less than 5 characters and can not be more than 50 characters") | ||
.MustAsync(async (name, token) => | ||
{ | ||
var isNameAlreadyExists = await repository.AnyAsync(x => | ||
x.Name.ToLower() == name.ToLower()); | ||
return !isNameAlreadyExists; | ||
}) | ||
.WithMessage("Question Set name already exists"); | ||
|
||
RuleFor(x => x.Details) | ||
.Length(10, 150) | ||
.When(x => !string.IsNullOrWhiteSpace(x.Details)) | ||
.WithMessage("Description can not be less than 10 characters and can not be more than 150 characters"); | ||
|
||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...tion/Modules.Question.Application/Question/QuestionSet/Delete/DeleteQuestionSetCommand.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 @@ | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Delete; | ||
public sealed record DeleteQuestionSetCommand(long QuestionSetId) : ICommand<Result<bool>>; |
22 changes: 22 additions & 0 deletions
22
...dules.Question.Application/Question/QuestionSet/Delete/DeleteQuestionSetCommandHandler.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,22 @@ | ||
using Modules.Question.Core.QuestionAggregate; | ||
using Modules.Question.Core.Tag; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Delete; | ||
internal sealed class DeleteQuestionSetCommandHandler(IQuestionSetRepository repository, IUnitOfWork unitOfWork) : ICommandHandler<DeleteQuestionSetCommand, Result<bool>> | ||
{ | ||
public async Task<Result<bool>> Handle(DeleteQuestionSetCommand request, CancellationToken cancellationToken = default) | ||
{ | ||
var questionSet = await repository.FirstOrDefaultAsync(x => x.QuestionSetId == request.QuestionSetId); | ||
|
||
if (questionSet == null) | ||
{ | ||
return TagErrors.TagNotFound; | ||
} | ||
|
||
repository.Delete(questionSet); | ||
await unitOfWork.CommitAsync(cancellationToken); | ||
|
||
return true; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/Question/Modules.Question.Application/Question/QuestionSet/Dtos/QuestionSetResponse.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 Modules.Question.Application.Question.QuestionSet.Dtos; | ||
|
||
public sealed record QuestionSetResponse(long QuestionSetId, string Name, string? SetCode, string? Details); |
7 changes: 7 additions & 0 deletions
7
...uestion/Modules.Question.Application/Question/QuestionSet/Query/GetAllQuestionSetQuery.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,7 @@ | ||
using common; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Query; | ||
|
||
public sealed record GetAllQuestionSetQuery : QueryStringParameter, IQuery<Result<PagedListDto<QuestionSetResponse>>>; |
18 changes: 18 additions & 0 deletions
18
.../Modules.Question.Application/Question/QuestionSet/Query/GetAllQuestionSetQueryHandler.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,18 @@ | ||
using AutoMapper; | ||
using common; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using Modules.Question.Core.QuestionAggregate; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Query; | ||
internal sealed class GetAllQuestionSetQueryHandler(IQuestionSetRepository repository, IMapper mapper) | ||
: IQueryHandler<GetAllQuestionSetQuery, Result<PagedListDto<QuestionSetResponse>>> | ||
{ | ||
public async Task<Result<PagedListDto<QuestionSetResponse>>> Handle(GetAllQuestionSetQuery request, CancellationToken cancellationToken) | ||
{ | ||
var sets = await repository.GetAllAsync(pageNumber: request.PageNumber, pageSize: request.PageSize, | ||
cancellationToken: cancellationToken); | ||
|
||
return mapper.Map<PagedListDto<QuestionSetResponse>>(sets); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...estion/Modules.Question.Application/Question/QuestionSet/Query/GetQuestionSetByIdQuery.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 @@ | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Query; | ||
public sealed record GetQuestionSetByIdQuery(long QuestionSetId) : IQuery<Result<QuestionSetResponse>>; |
20 changes: 20 additions & 0 deletions
20
...Modules.Question.Application/Question/QuestionSet/Query/GetQuestionSetByIdQueryHandler.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,20 @@ | ||
using AutoMapper; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using Modules.Question.Core.QuestionAggregate; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Query; | ||
internal class GetQuestionSetByIdQueryHandler(IQuestionSetRepository repository, | ||
IMapper mapper) : IQueryHandler<GetQuestionSetByIdQuery, Result<QuestionSetResponse>> | ||
{ | ||
public async Task<Result<QuestionSetResponse>> Handle(GetQuestionSetByIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
var setDetails = await repository.FirstOrDefaultAsync(x => x.QuestionSetId == request.QuestionSetId); | ||
if (setDetails == null) | ||
{ | ||
return QuestionErrors.QuestionSetNotFound; | ||
} | ||
|
||
return mapper.Map<QuestionSetResponse>(setDetails); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...s/Question/Modules.Question.Application/Question/QuestionSet/QuestionSetMappingProfile.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,18 @@ | ||
using AutoMapper; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet; | ||
internal sealed class QuestionSetMappingProfile : Profile | ||
{ | ||
public QuestionSetMappingProfile() | ||
{ | ||
CreateMap<Core.QuestionAggregate.QuestionSet, QuestionSetResponse>() | ||
.ConstructUsing(set => new QuestionSetResponse( | ||
set.QuestionSetId, | ||
set.Name, | ||
set.SetCode, | ||
set.Details | ||
)) | ||
.ReverseMap(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...tion/Modules.Question.Application/Question/QuestionSet/Update/UpdateQuestionSetCommand.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,6 @@ | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Update; | ||
|
||
public sealed record UpdateQuestionSetCommand(long QuestionSetId, string Name, string? SetCode, string? Details) : ICommand<Result<QuestionSetResponse>>; |
31 changes: 31 additions & 0 deletions
31
...dules.Question.Application/Question/QuestionSet/Update/UpdateQuestionSetCommandHandler.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,31 @@ | ||
using AutoMapper; | ||
using Modules.Question.Application.Question.QuestionSet.Dtos; | ||
using Modules.Question.Core.QuestionAggregate; | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Update; | ||
internal sealed class UpdateQuestionSetCommandHandler(IQuestionSetRepository repository, | ||
IUnitOfWork unitOfWork, | ||
IMapper mapper) : ICommandHandler<UpdateQuestionSetCommand, Result<QuestionSetResponse>> | ||
{ | ||
public async Task<Result<QuestionSetResponse>> Handle(UpdateQuestionSetCommand request, CancellationToken cancellationToken = default) | ||
{ | ||
var set = await repository.FirstOrDefaultAsync(x => x.QuestionSetId == request.QuestionSetId); | ||
if (set == null) | ||
{ | ||
return QuestionErrors.QuestionSetNotFound; | ||
} | ||
|
||
var updatedSet = set.Update(request.Name, request.SetCode, request.Details); | ||
|
||
if (!updatedSet.IsSuccess || updatedSet.Value is null) | ||
{ | ||
return updatedSet.Error; | ||
} | ||
|
||
repository.Update(updatedSet.Value); | ||
await unitOfWork.CommitAsync(cancellationToken); | ||
|
||
return mapper.Map<QuestionSetResponse>(updatedSet.Value); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...les.Question.Application/Question/QuestionSet/Update/UpdateQuestionSetCommandValidator.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,33 @@ | ||
using FluentValidation; | ||
using Modules.Question.Core.QuestionAggregate; | ||
|
||
namespace Modules.Question.Application.Question.QuestionSet.Update; | ||
internal sealed class UpdateQuestionSetCommandValidator : AbstractValidator<UpdateQuestionSetCommand> | ||
{ | ||
public UpdateQuestionSetCommandValidator(IQuestionSetRepository repository) | ||
{ | ||
RuleFor(x => x.QuestionSetId) | ||
.NotNull() | ||
.WithMessage("Question Set Id can not null") | ||
.GreaterThan(0) | ||
.WithMessage("Question Set Id value can not be 0 or less than 0"); | ||
|
||
RuleFor(x => x.Name) | ||
.NotEmpty() | ||
.WithMessage("Name can not be empty") | ||
.Length(5, 50) | ||
.WithMessage("Name can not be less than 5 characters and can not be more than 50 characters") | ||
.MustAsync(async (command, name, token) => | ||
{ | ||
var isNameAlreadyExists = await repository.AnyAsync(x => | ||
x.Name.ToLower() == name.ToLower() && x.QuestionSetId != command.QuestionSetId); | ||
return !isNameAlreadyExists; | ||
}) | ||
.WithMessage("Question Set name is already exists"); | ||
|
||
RuleFor(x => x.Details) | ||
.Length(10, 150) | ||
.When(x => !string.IsNullOrWhiteSpace(x.Details)) | ||
.WithMessage("Details can not be less than 10 characters and can not be more than 150 characters"); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Modules/Question/Modules.Question.Core/QuestionAggregate/IQuestionSetRepository.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 @@ | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Core.QuestionAggregate; | ||
public interface IQuestionSetRepository : IRepository<QuestionSet>; |
7 changes: 7 additions & 0 deletions
7
src/Modules/Question/Modules.Question.Core/QuestionAggregate/QuestionErrors.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,7 @@ | ||
using SharedKernel.Core; | ||
|
||
namespace Modules.Question.Core.QuestionAggregate; | ||
public struct QuestionErrors | ||
{ | ||
public static Error QuestionSetNotFound => Error.NotFound("QuestionSet.QuestionSetNotFound", "Question Set not found."); | ||
} |
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
Oops, something went wrong.