diff --git a/MBS_COMMAND.Application/UserCases/Commands/AddProjectToGroupCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/AddProjectToGroupCommandHandler.cs new file mode 100644 index 0000000..badd7b0 --- /dev/null +++ b/MBS_COMMAND.Application/UserCases/Commands/AddProjectToGroupCommandHandler.cs @@ -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 groupRepository, + IUnitOfWork unitOfWork, + ICurrentUserService currentUserService) + : ICommandHandler +{ + public async Task 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(); + } +} \ No newline at end of file diff --git a/MBS_COMMAND.Contract/Services/Projects/Command.cs b/MBS_COMMAND.Contract/Services/Projects/Command.cs new file mode 100644 index 0000000..f5456dd --- /dev/null +++ b/MBS_COMMAND.Contract/Services/Projects/Command.cs @@ -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; +} \ No newline at end of file diff --git a/MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs b/MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs index dd35a23..6a94f40 100644 --- a/MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs +++ b/MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs @@ -17,7 +17,6 @@ public void AddRoutes(IEndpointRouteBuilder app) gr1.MapGet("accept-invitation/{groupId}/{memberId}", AcceptGroupInvitation); gr1.MapPost("mentor", AddMentorToGroup); } - private static async Task AddMentorToGroup(ISender sender, [FromBody] Command.AddMentorToGroup request) { var result = await sender.Send(request); diff --git a/MBS_COMMAND.Presentation/APIs/Projects/ProjectApi.cs b/MBS_COMMAND.Presentation/APIs/Projects/ProjectApi.cs new file mode 100644 index 0000000..1ed13bd --- /dev/null +++ b/MBS_COMMAND.Presentation/APIs/Projects/ProjectApi.cs @@ -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 AddProject(ISender sender, [FromBody] Command.AddProject request) + { + var result = await sender.Send(request); + return result.IsFailure ? HandlerFailure(result) : Results.Ok(result); + } +} \ No newline at end of file