-
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.
- Loading branch information
1 parent
19d4567
commit daefc3b
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
MBS_COMMAND.Application/UserCases/Commands/AddProjectToGroupCommandHandler.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,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(); | ||
} | ||
} |
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.Projects; | ||
|
||
public static class Command | ||
{ | ||
public record AddProject(string Name, string Description, Guid GroupId) : 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
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,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); | ||
} | ||
} |