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

Features/sorvkumr/cqrs controller methods #30

Merged
merged 8 commits into from
Aug 12, 2023
45 changes: 23 additions & 22 deletions backend/src/ThreeTee.Api/Controllers/DepartmentController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using ThreeTee.Application.Interfaces;
using ThreeTee.Application.Cqrs.Departments.Commands.CreateDepartment;
using ThreeTee.Application.Cqrs.Departments.Commands.DeleteDepartment;
using ThreeTee.Application.Cqrs.Departments.Commands.UpdateDepartment;
using ThreeTee.Application.Cqrs.Departments.Queries;
using ThreeTee.Application.Models.Departments;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand All @@ -8,56 +11,54 @@ namespace ThreeTee.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DepartmentController : ControllerBase
public class DepartmentController : ApiControllerBase
{
private readonly IDepartmentService _departmentService;
public DepartmentController(IDepartmentService departmentService)
{
_departmentService = departmentService;
}

// GET: api/<DepartmentController>
[HttpGet]
[Produces(typeof(List<DepartmentResponse>))]
public async Task<IResult> Get()
public async Task<IResult> Get([FromQuery]GetDepartmentsWithPaginationQuery query)
{
var items = await _departmentService.GetAsync();
return TypedResults.Ok(items);
var items = await Mediator.Send(query);
if (items != null) { return TypedResults.Ok(items); }
return TypedResults.NotFound();
}

// GET api/<DepartmentController>/5
[HttpGet("{id}")]
[Produces(typeof(List<DepartmentResponse>))]
public async Task<IResult> Get(Guid id)
public async Task<IResult> Get([FromQuery]GetDepartmentsByIdQuery query)
{
var item = await _departmentService.GetByIdAsync(id);
return TypedResults.Ok(item);
var item = await Mediator.Send(query);
if(item != null) { return TypedResults.Ok(item); }
return TypedResults.NotFound();
}

// POST api/<DepartmentController>
[HttpPost]
public async Task<IResult> Post([FromBody] DepartmentPostRequest value)
public async Task<IResult> Post([FromBody] CreateDepartmentCommand query)
{
var ret = await _departmentService.InsertAsync(value);
if(ret==null) { return null; }
var ret = await Mediator.Send(query);
if (ret == null) { return TypedResults.BadRequest(); }
return TypedResults.Ok(ret);
}

// PUT api/<DepartmentController>/5
[HttpPut]
public async Task<IResult> Put([FromBody] DepartmentPutRequest value)
public async Task<IResult> Put([FromBody] UpdateDepartmentCommand query)
{
var ret = await _departmentService.UpdateAsync(value);
if (ret == null) return null;
var ret = await Mediator.Send(query);
if (ret == null) return TypedResults.BadRequest();
return TypedResults.Ok(ret);
}

// DELETE api/<DepartmentController>/5
[HttpDelete("{id}")]

public async Task<IResult> Delete(Guid id)
public async Task<IResult> Delete(DeleteDepartmentCommand query)
{
await _departmentService.DeleteAsync(id);
var ret = await Mediator.Send(query);
if(ret!= Guid.Empty)
return TypedResults.BadRequest();
return TypedResults.NoContent();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Mapster;
using MediatR;
using ThreeTee.Core.Entities;

namespace ThreeTee.Application.Cqrs.Departments.Commands.CreateDepartment;
public class CreateDepartmentCommand : IRequest<Department>
{
public string Name { get; set; }
public string Code { get; set; }
}

public class CreateDepartmentCommandHandler : IRequestHandler<CreateDepartmentCommand, Department>
{
private readonly IEntitiesContext _context;
public CreateDepartmentCommandHandler(IEntitiesContext context)
{
_context = context;
}
public async Task<Department> Handle(CreateDepartmentCommand request, CancellationToken cancellationToken)
{
var department = request.Adapt<Department>();

_context.Departments.Add(department);
await _context.SaveChangesAsync(cancellationToken);
return department;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FluentValidation;

namespace ThreeTee.Application.Cqrs.Departments.Commands.CreateDepartment;
public class CreateDepartmentCommandValidator : AbstractValidator<CreateDepartmentCommand>
{
public CreateDepartmentCommandValidator()
{
RuleFor(e => e.Name)
.NotEmpty();
RuleFor(e => e.Code)
.NotEmpty();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FluentValidation;

namespace ThreeTee.Application.Cqrs.Departments.Commands.DeleteDepartment;
public class DeleteDesignationCommandValidator:AbstractValidator<DeleteDepartmentCommand>
{
public DeleteDesignationCommandValidator()
{
RuleFor(e => e.Id)
.NotEmpty();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace ThreeTee.Application.Cqrs.Departments.Commands.DeleteDepartment;
public class DeleteDepartmentCommand : IRequest<Guid>
{
public Guid Id { get; set; }
}
public class DeleteDepartmentCommandHanlder : IRequestHandler<DeleteDepartmentCommand, Guid>
{
private readonly IEntitiesContext _context;
public DeleteDepartmentCommandHanlder(IEntitiesContext context)
{
_context = context;
}

public async Task<Guid> Handle(DeleteDepartmentCommand request, CancellationToken cancellationToken)
{
var department = await _context.Departments.FirstOrDefaultAsync(e => e.Id == request.Id);
if (department != null)
{
_context.Departments.Remove(department);
await _context.SaveChangesAsync(cancellationToken);
return department.Id;
}
return Guid.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Mapster;
using MediatR;
using ThreeTee.Core.Entities;

namespace ThreeTee.Application.Cqrs.Departments.Commands.UpdateDepartment;
public class UpdateDepartmentCommand : IRequest<Department>
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
}

public class UpdateDepartmentCommandHandler : IRequestHandler<UpdateDepartmentCommand, Department>
{
private readonly IEntitiesContext _context;
public UpdateDepartmentCommandHandler(IEntitiesContext context)
{
_context = context;
}
public async Task<Department> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
{
var entity = request.Adapt<Department>();
_context.Departments.Update(entity);

await _context.SaveChangesAsync(cancellationToken);
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using FluentValidation;

namespace ThreeTee.Application.Cqrs.Departments.Commands.UpdateDepartment;
public class UpdateDepartmentCommandValidator : AbstractValidator<UpdateDepartmentCommand>
{
public UpdateDepartmentCommandValidator()
{
RuleFor(e => e.Name)
.NotEmpty();
RuleFor(e => e.Id)
.NotEmpty();
RuleFor(e => e.Code)
.NotEmpty();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ThreeTee.Application.Models.Departments;

namespace ThreeTee.Application.Cqrs.Departments.Queries;
public class GetDepartmentsByIdQuery : IRequest<DepartmentResponse>
{
public Guid Id { get; set; }
}

public class GetDepartmentsByIdQueryHandler : IRequestHandler<GetDepartmentsByIdQuery, DepartmentResponse>
{
private readonly IEntitiesContext _context;
public GetDepartmentsByIdQueryHandler(IEntitiesContext context)
{
_context = context;
}
public async Task<DepartmentResponse> Handle(GetDepartmentsByIdQuery request, CancellationToken cancellationToken)
{
var department = await _context.Departments.ProjectToType<DepartmentResponse>().FirstOrDefaultAsync(e => e.Id == request.Id, cancellationToken);
if (department != null)
{
//CHECK IT
var depVal = _context.DepartmentManager.FirstOrDefault(e => e.DepartmentId == request.Id)?.UserId;
//department.DepartmentManager = _context.ProjectUsers.FirstOrDefault(u=>u.UserId == depVal)?.User.UserName;
return department;
}
return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Mapster;
using MediatR;
using ThreeTee.Application.Mappings;
using ThreeTee.Application.Models;
using ThreeTee.Application.Models.Departments;

namespace ThreeTee.Application.Cqrs.Departments.Queries;
public class GetDepartmentsWithPaginationQuery : IRequest<PaginatedList<DepartmentResponse>>
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
}

public class GetDepartmentsWithPaginationQueryHandler : IRequestHandler<GetDepartmentsWithPaginationQuery, PaginatedList<DepartmentResponse>>
{
private readonly IEntitiesContext _context;
public GetDepartmentsWithPaginationQueryHandler(IEntitiesContext context)
{
_context = context;
}

public async Task<PaginatedList<DepartmentResponse>> Handle(GetDepartmentsWithPaginationQuery request, CancellationToken cancellationToken)
{
var departments = await _context.Departments
.ProjectToType<DepartmentResponse>()
.PaginatedListAsync(request.PageNumber, request.PageSize);
if (departments != null)
return departments;
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface IEntitiesContext
DbSet<ProjectUser> ProjectUsers { get; set; }
DbSet<Status> Statuses { get; set; }
DbSet<UserDepartment> UserDepartments { get; set; }
DbSet<DepartmentManager> DepartmentManager { get; set; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class DepartmentResponse : DepartmentPostRequest
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public string? LastTouchedBy { get; set; }
public string? DepartmentManager { get; set; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class EntitiesContext : IdentityDbContext<ApplicationUser, IdentityRole<G
public DbSet<ProjectUser> ProjectUsers { get; set; }
public DbSet<Status> Statuses { get; set; }
public DbSet<UserDepartment> UserDepartments { get; set; }
public DbSet<DepartmentManager> DepartmentManager { get; set; }

public EntitiesContext(DbContextOptions options) : base(options)
{
Expand Down
Loading