-
Notifications
You must be signed in to change notification settings - Fork 1
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 #25 from bentran1vn/Features/Group/SendMailAndAccept
Add feature to add subjects to the system
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
MBS_COMMAND.Application/UserCases/Commands/Sbujects/AddSubjectCommandHandler.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,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<Subject, Guid> subjectRepository, | ||
IRepositoryBase<Semester, Guid> semesterRepository, | ||
IUnitOfWork unitOfWork) | ||
: ICommandHandler<Command.AddSubject> | ||
{ | ||
|
||
public async Task<Result> 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(); | ||
} | ||
} |
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,8 @@ | ||
using MBS_COMMAND.Contract.Abstractions.Messages; | ||
|
||
namespace MBS_COMMAND.Contract.Services.Subjects; | ||
|
||
public static class Command | ||
{ | ||
public record AddSubject(string Name) : ICommand; | ||
} |
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 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<IResult> CreateSubject(ISender sender,[FromBody] Command.AddSubject command) | ||
{ | ||
var result = await sender.Send(command); | ||
return result.IsFailure ? HandlerFailure(result) : Results.Ok(result); | ||
} | ||
} |