From 6ed5a1aab497bc986e1a0d16eed4c5f1ee0b3862 Mon Sep 17 00:00:00 2001 From: Eric <36512385+PhucNghi176@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:51:22 +0700 Subject: [PATCH] Add feature to add subjects to the system Introduced a new feature for adding subjects to the system: - Added `AddSubjectCommandHandler` class to handle the command for adding subjects. - Implemented `Handle` method to check for existing subjects and add new ones. - Added `Command.AddSubject` record to encapsulate data for adding subjects. - Created `SubjectApi` class to define API routes and handle HTTP POST requests for adding subjects. --- .../Sbujects/AddSubjectCommandHandler.cs | 36 +++++++++++++++++++ .../Services/Subjects/Command.cs | 8 +++++ .../APIs/Subjects/SubjectApi.cs | 20 +++++++++++ 3 files changed, 64 insertions(+) create mode 100644 MBS_COMMAND.Application/UserCases/Commands/Sbujects/AddSubjectCommandHandler.cs create mode 100644 MBS_COMMAND.Contract/Services/Subjects/Command.cs create mode 100644 MBS_COMMAND.Presentation/APIs/Subjects/SubjectApi.cs diff --git a/MBS_COMMAND.Application/UserCases/Commands/Sbujects/AddSubjectCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Sbujects/AddSubjectCommandHandler.cs new file mode 100644 index 0000000..beab04e --- /dev/null +++ b/MBS_COMMAND.Application/UserCases/Commands/Sbujects/AddSubjectCommandHandler.cs @@ -0,0 +1,36 @@ +using MBS_COMMAND.Contract.Abstractions.Messages; +using MBS_COMMAND.Contract.Abstractions.Shared; +using MBS_COMMAND.Contract.Services.Subjects; +using MBS_COMMAND.Domain.Abstractions; +using MBS_COMMAND.Domain.Abstractions.Repositories; +using MBS_COMMAND.Domain.Entities; + +namespace MBS_COMMAND.Application.UserCases.Commands.Sbujects; + +public class AddSubjectCommandHandler( + IRepositoryBase subjectRepository, + IRepositoryBase semesterRepository, + IUnitOfWork unitOfWork) + : ICommandHandler +{ + + public async Task Handle(Command.AddSubject request, CancellationToken cancellationToken) + { + var currentSemester = await semesterRepository.FindSingleAsync(x => x.IsActive, cancellationToken); + if (currentSemester == null) + return Result.Failure(new Error("404", "Semester Not Found")); + var isCurrentSubjectExist = subjectRepository.FindAll(x => x.Name == request.Name && x.SemesterId == currentSemester.Id); + if (isCurrentSubjectExist.Any()) + return Result.Failure(new Error("404", "Subject Already Exist In This Semester")); + var subject = new Subject + { + Id = Guid.NewGuid(), + Name = request.Name, + Status = 1, + SemesterId = currentSemester.Id + }; + subjectRepository.Add(subject); + await unitOfWork.SaveChangesAsync(cancellationToken); + return Result.Success(); + } +} \ No newline at end of file diff --git a/MBS_COMMAND.Contract/Services/Subjects/Command.cs b/MBS_COMMAND.Contract/Services/Subjects/Command.cs new file mode 100644 index 0000000..5c97068 --- /dev/null +++ b/MBS_COMMAND.Contract/Services/Subjects/Command.cs @@ -0,0 +1,8 @@ +using MBS_COMMAND.Contract.Abstractions.Messages; + +namespace MBS_COMMAND.Contract.Services.Subjects; + +public static class Command +{ + public record AddSubject(string Name) : ICommand; +} \ No newline at end of file diff --git a/MBS_COMMAND.Presentation/APIs/Subjects/SubjectApi.cs b/MBS_COMMAND.Presentation/APIs/Subjects/SubjectApi.cs new file mode 100644 index 0000000..d56b3b0 --- /dev/null +++ b/MBS_COMMAND.Presentation/APIs/Subjects/SubjectApi.cs @@ -0,0 +1,20 @@ +using MBS_COMMAND.Contract.Services.Subjects; +using MBS_COMMAND.Presentation.Abstractions; + +namespace MBS_COMMAND.Presentation.APIs.Subjects; + +public class SubjectApi : ApiEndpoint,ICarterModule +{ + private const string BaseUrl = "/api/v{version:apiVersion}/subjects"; + public void AddRoutes(IEndpointRouteBuilder app) + { + var gr1 = app.NewVersionedApi("Subjects").MapGroup(BaseUrl).HasApiVersion(1); + gr1.MapPost(string.Empty, CreateSubject); + } + + private static async Task CreateSubject(ISender sender,[FromBody] Command.AddSubject command) + { + var result = await sender.Send(command); + return result.IsFailure ? HandlerFailure(result) : Results.Ok(result); + } +} \ No newline at end of file