-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project User Cqrs implementation. (#28)
* Project User Cqrs implementation. * Fixed issue for ProjectUser Module cqrs.
- Loading branch information
Showing
8 changed files
with
170 additions
and
28 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
...eTee.Application/Cqrs/ProjectUsers/Commands/DeleteProjectUser/DeleteProjectUserCommand.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,31 @@ | ||
using MediatR; | ||
|
||
namespace ThreeTee.Application.Cqrs.ProjectUsers.Commands.DeleteProjectUser; | ||
|
||
public record DeleteProjectUserCommand : IRequest<bool> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid ProjectId { get; set; } | ||
} | ||
|
||
public class DeleteProjectUserCommandHandler : IRequestHandler<DeleteProjectUserCommand, bool> | ||
{ | ||
private readonly IEntitiesContext _context; | ||
public DeleteProjectUserCommandHandler(IEntitiesContext context) | ||
{ | ||
_context = context; | ||
} | ||
public async Task<bool> Handle(DeleteProjectUserCommand request, CancellationToken cancellationToken) | ||
{ | ||
var projectUser = _context.ProjectUsers.Where(e => e.UserId == request.UserId) | ||
.FirstOrDefault(e => e.ProjectId == request.ProjectId); | ||
|
||
if (projectUser is null) | ||
return false; | ||
|
||
_context.ProjectUsers.Remove(projectUser); | ||
await _context.SaveChangesAsync(cancellationToken); | ||
return true; | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
...ication/Cqrs/ProjectUsers/Commands/DeleteProjectUser/DeleteProjectUserCommandValidator.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,15 @@ | ||
using FluentValidation; | ||
|
||
namespace ThreeTee.Application.Cqrs.ProjectUsers.Commands.DeleteProjectUser; | ||
|
||
public class DeleteProjectUserCommandValidator:AbstractValidator<DeleteProjectUserCommand> | ||
{ | ||
public DeleteProjectUserCommandValidator() | ||
{ | ||
RuleFor(e=>e.UserId) | ||
.NotEmpty(); | ||
RuleFor(e => e.ProjectId) | ||
.NotEmpty(); | ||
} | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
...eTee.Application/Cqrs/ProjectUsers/Commands/UpdateProjectUser/UpdateProjectUserCommand.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,40 @@ | ||
using FluentValidation; | ||
using Mapster; | ||
using MediatR; | ||
using ThreeTee.Core.Entities; | ||
|
||
namespace ThreeTee.Application.Cqrs.ProjectUsers.Commands.UpdateProjectUser; | ||
|
||
public record UpdateProjectUserCommand : IRequest<ProjectUser> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Guid ProjectId { get; set; } | ||
public Guid OldProjectId { get; set; } | ||
} | ||
|
||
public class UpdateProjectUserCommandHandler : IRequestHandler<UpdateProjectUserCommand, ProjectUser> | ||
{ | ||
private readonly IEntitiesContext _context; | ||
public UpdateProjectUserCommandHandler(IEntitiesContext context) | ||
{ | ||
_context = context; | ||
} | ||
public async Task<ProjectUser> Handle(UpdateProjectUserCommand request, CancellationToken cancellationToken) | ||
{ | ||
//Check user for project present. If not create new one. | ||
|
||
var projectUser = _context.ProjectUsers.Where(e => e.UserId == request.UserId) | ||
.FirstOrDefault(e => e.ProjectId == request.ProjectId); | ||
if (projectUser == null) | ||
{ | ||
var entity = request.Adapt<ProjectUser>(); | ||
|
||
_context.ProjectUsers.Add(entity); | ||
await _context.SaveChangesAsync(cancellationToken); | ||
|
||
return entity; | ||
} | ||
return projectUser; | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
...ication/Cqrs/ProjectUsers/Commands/UpdateProjectUser/UpdateProjectUserCommandValidator.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,19 @@ | ||
using FluentValidation; | ||
|
||
namespace ThreeTee.Application.Cqrs.ProjectUsers.Commands.UpdateProjectUser; | ||
|
||
public class UpdateProjectUserCommandValidator:AbstractValidator<UpdateProjectUserCommand> | ||
{ | ||
public UpdateProjectUserCommandValidator() | ||
{ | ||
RuleFor(v => v.UserId) | ||
.NotEmpty(); | ||
RuleFor(v => v.ProjectId) | ||
.NotEmpty(); | ||
RuleFor(v => v.OldProjectId) | ||
.NotEmpty(); | ||
RuleFor(e => e.ProjectId) | ||
.NotEqual(v => v.OldProjectId); | ||
} | ||
} | ||
|
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
36 changes: 36 additions & 0 deletions
36
...src/ThreeTee.Application/Cqrs/ProjectUsers/Queries/GetProjectUsersWithUserIdPagination.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 Mapster; | ||
using MapsterMapper; | ||
using MediatR; | ||
using ThreeTee.Application.Mappings; | ||
using ThreeTee.Application.Models; | ||
using ThreeTee.Application.Models.ProjectUser; | ||
|
||
namespace ThreeTee.Application.Cqrs.ProjectUsers.Queries; | ||
|
||
public record GetProjectUsersWithUserIdPaginationQuery : IRequest<PaginatedList<ProjectUserResponse>> | ||
{ | ||
public int PageNumber { get; init; } = 1; | ||
public int PageSize { get; init; } = 10; | ||
public Guid UserId { get; set; } | ||
} | ||
|
||
public class GetProjectUsersWithUserIdPaginationQueryHandler : IRequestHandler<GetProjectUsersWithUserIdPaginationQuery, PaginatedList<ProjectUserResponse>> | ||
{ | ||
private readonly IEntitiesContext _context; | ||
private readonly IMapper _mapper; | ||
|
||
public GetProjectUsersWithUserIdPaginationQueryHandler(IEntitiesContext context, IMapper mapper) | ||
{ | ||
_context = context; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task<PaginatedList<ProjectUserResponse>> Handle(GetProjectUsersWithUserIdPaginationQuery request, CancellationToken cancellationToken) | ||
{ | ||
return await _context.ProjectUsers | ||
.Where(e=>e.UserId==request.UserId) | ||
// .OrderBy(pu=>pu.LastTouchedBy) | ||
.ProjectToType<ProjectUserResponse>() | ||
.PaginatedListAsync(request.PageNumber, request.PageSize); | ||
} | ||
} |
9b29940
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
three-t – ./
three-t-git-main-succkarz.vercel.app
threetee.online
three-t.vercel.app
three-t-succkarz.vercel.app
www.threetee.online