Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project User Cqrs implementation. #28

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions backend/src/ThreeTee.Api/Controllers/ProjectUserController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using ThreeTee.Application.Cqrs.ProjectUsers.Commands.CreateProjectUser;
using ThreeTee.Application.Cqrs.ProjectUsers.Commands.DeleteProjectUser;
using ThreeTee.Application.Cqrs.ProjectUsers.Commands.UpdateProjectUser;
using ThreeTee.Application.Cqrs.ProjectUsers.Queries;
using ThreeTee.Application.Interfaces;
using ThreeTee.Application.Models.ProjectUser;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand All @@ -13,12 +16,6 @@ namespace ThreeTee.Api.Controllers
[Authorize]
public class ProjectUserController : ApiControllerBase
{
private readonly IProjectUserService _projectUserService;

public ProjectUserController(IProjectUserService projectUserService)
{
_projectUserService = projectUserService;
}
// GET: api/<ClientController>
[HttpGet]
[Produces(typeof(List<ProjectUserResponse>))]
Expand All @@ -28,37 +25,39 @@ public async Task<IResult> Get([FromQuery] GetProjectUsersWithPaginationQuery qu
return TypedResults.Ok(items);
}

[HttpGet("{id}")]
//GET PROJECT USER LIST api/<ClientController>
[HttpPost("{UserId}")]
[Produces(typeof(List<ProjectUserResponse>))]
public async Task<IResult> Get(Guid id)
public async Task<IResult> Get(GetProjectUsersWithUserIdPaginationQuery query)
{
var items = await _projectUserService.GetByProjectId(id);
var items = await Mediator.Send(query);
return TypedResults.Ok(items);
}

// POST api/<ClientController>
[HttpPost]
public async Task<IResult> Post([FromBody] ProjectUserUpsertRequest value)
public async Task<IResult> Post([FromBody] CreateProjectUserCommand query)
{

var ret = await _projectUserService.InsertAsync(value);
if (ret == null) return TypedResults.BadRequest();
return TypedResults.Created(ret.ProjectId.ToString());
var items = await Mediator.Send(query);
if (items != Guid.Empty)
{ return TypedResults.Created(items.ToString()); }
return TypedResults.BadRequest();
}

// PUT api/<ClientController>
[HttpPut]
public async Task<IResult> Put(ProjectUserUpsertRequest request)
public async Task<IResult> Put(UpdateProjectUserCommand query)
{
var item = await _projectUserService.UpdateAsync(request);
var item = await Mediator.Send(query);
return TypedResults.Ok(item);
}

[HttpDelete("{id}")]
public async Task<IResult> Delete(Guid id)
// DELETE api/<ClientController>
[HttpDelete("{UserId}")]
public async Task<IResult> Delete(DeleteProjectUserCommand query)
{
await _projectUserService.DeleteAsync(id);
var item = await Mediator.Send(query);
return TypedResults.NoContent();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mapster;
using MediatR;
using ThreeTee.Core.Entities;

Expand All @@ -20,11 +21,12 @@ public CreateProjectUserCommandHandler(IEntitiesContext context)

public async Task<Guid> Handle(CreateProjectUserCommand request, CancellationToken cancellationToken)
{
var entity = new ProjectUser
{
ProjectId = request.ProjectId,
UserId = request.UserId,
};
var entity = request.Adapt<ProjectUser>();
//var entitys = new ProjectUser
//{
// ProjectId = request.ProjectId,
// UserId = entity.UserId,
//};

// entity.AddDomainEvent(new ProjectUserCreatedEvent(entity));

Expand Down
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;
}
}

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();
}
}

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;
}
}

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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public record GetProjectUsersWithPaginationQuery : IRequest<PaginatedList<Projec
public int PageSize { get; init; } = 10;
}

public class GetTodoItemsWithPaginationQueryHandler : IRequestHandler<GetProjectUsersWithPaginationQuery, PaginatedList<ProjectUserResponse>>
public class GetProjectUsersWithPaginationQueryHandler : IRequestHandler<GetProjectUsersWithPaginationQuery, PaginatedList<ProjectUserResponse>>
{
private readonly IEntitiesContext _context;
private readonly IMapper _mapper;

public GetTodoItemsWithPaginationQueryHandler(IEntitiesContext context, IMapper mapper)
public GetProjectUsersWithPaginationQueryHandler(IEntitiesContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
Expand Down
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);
}
}
Loading