Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNghi176 committed Oct 18, 2024
1 parent 19d4567 commit daefc3b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using MBS_COMMAND.Contract.Abstractions.Messages;
using MBS_COMMAND.Contract.Abstractions.Shared;
using MBS_COMMAND.Contract.Services.Projects;
using MBS_COMMAND.Domain.Abstractions;
using MBS_COMMAND.Domain.Abstractions.Repositories;
using MBS_COMMAND.Domain.Entities;

namespace MBS_COMMAND.Application.UserCases.Commands;

public class AddProjectToGroupCommandHandler(
IRepositoryBase<Group, Guid> groupRepository,
IUnitOfWork unitOfWork,
ICurrentUserService currentUserService)
: ICommandHandler<Command.AddProject>
{
public async Task<Result> Handle(Command.AddProject request, CancellationToken cancellationToken)
{
var group = await groupRepository.FindByIdAsync(request.GroupId, cancellationToken);
if (group == null)
{
return Result.Failure(new Error("404", "Group not found"));
}
if (group.LeaderId.ToString() != currentUserService.UserId)
{
return Result.Failure(new Error("403", "Only group leader can add project to group"));
}
if(group.ProjectId!=null)
{
return Result.Failure(new Error("400", "Group already has a project"));
}

group.Project=new Project
{
Name=request.Name,
Description=request.Description,
};
groupRepository.Update(group);
await unitOfWork.SaveChangesAsync(cancellationToken);
return Result.Success();
}
}
8 changes: 8 additions & 0 deletions MBS_COMMAND.Contract/Services/Projects/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.Projects;

public static class Command
{
public record AddProject(string Name, string Description, Guid GroupId) : ICommand;
}
1 change: 0 additions & 1 deletion MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public void AddRoutes(IEndpointRouteBuilder app)
gr1.MapGet("accept-invitation/{groupId}/{memberId}", AcceptGroupInvitation);
gr1.MapPost("mentor", AddMentorToGroup);
}

private static async Task<IResult> AddMentorToGroup(ISender sender, [FromBody] Command.AddMentorToGroup request)
{
var result = await sender.Send(request);
Expand Down
19 changes: 19 additions & 0 deletions MBS_COMMAND.Presentation/APIs/Projects/ProjectApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MBS_COMMAND.Contract.Services.Projects;
using MBS_COMMAND.Presentation.Abstractions;

namespace MBS_COMMAND.Presentation.APIs.Projects;

public class ProjectApi : ApiEndpoint,ICarterModule
{
private const string BaseUrl = "/api/v{version:apiVersion}/projects";
public void AddRoutes(IEndpointRouteBuilder app)
{
var gr1= app.NewVersionedApi("Projects").MapGroup(BaseUrl).HasApiVersion(1);
gr1.MapPost(string.Empty, AddProject).RequireAuthorization();
}
private static async Task<IResult> AddProject(ISender sender, [FromBody] Command.AddProject request)
{
var result = await sender.Send(request);
return result.IsFailure ? HandlerFailure(result) : Results.Ok(result);
}
}

0 comments on commit daefc3b

Please sign in to comment.