Skip to content

Commit

Permalink
Merge pull request #25 from bentran1vn/Features/Group/SendMailAndAccept
Browse files Browse the repository at this point in the history
Add feature to add subjects to the system
  • Loading branch information
PhucNghi176 authored Oct 17, 2024
2 parents 09f5ded + 6ed5a1a commit bb66a78
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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();
}
}
8 changes: 8 additions & 0 deletions MBS_COMMAND.Contract/Services/Subjects/Command.cs
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;
}
20 changes: 20 additions & 0 deletions MBS_COMMAND.Presentation/APIs/Subjects/SubjectApi.cs
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);
}
}

0 comments on commit bb66a78

Please sign in to comment.