diff --git a/backend/src/ThreeTee.Api/Controllers/DepartmentController.cs b/backend/src/ThreeTee.Api/Controllers/DepartmentController.cs index 8714c71..77a9a2d 100644 --- a/backend/src/ThreeTee.Api/Controllers/DepartmentController.cs +++ b/backend/src/ThreeTee.Api/Controllers/DepartmentController.cs @@ -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 @@ -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/ [HttpGet] [Produces(typeof(List))] - public async Task Get() + public async Task 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//5 [HttpGet("{id}")] [Produces(typeof(List))] - public async Task Get(Guid id) + public async Task 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/ [HttpPost] - public async Task Post([FromBody] DepartmentPostRequest value) + public async Task 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//5 [HttpPut] - public async Task Put([FromBody] DepartmentPutRequest value) + public async Task 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//5 [HttpDelete("{id}")] - public async Task Delete(Guid id) + public async Task Delete(DeleteDepartmentCommand query) { - await _departmentService.DeleteAsync(id); + var ret = await Mediator.Send(query); + if(ret!= Guid.Empty) + return TypedResults.BadRequest(); return TypedResults.NoContent(); } } diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs new file mode 100644 index 0000000..ffe9ac4 --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommand.cs @@ -0,0 +1,28 @@ +using Mapster; +using MediatR; +using ThreeTee.Core.Entities; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.CreateDepartment; +public class CreateDepartmentCommand : IRequest +{ + public string Name { get; set; } + public string Code { get; set; } +} + +public class CreateDepartmentCommandHandler : IRequestHandler +{ + private readonly IEntitiesContext _context; + public CreateDepartmentCommandHandler(IEntitiesContext context) + { + _context = context; + } + public async Task Handle(CreateDepartmentCommand request, CancellationToken cancellationToken) + { + var department = request.Adapt(); + + _context.Departments.Add(department); + await _context.SaveChangesAsync(cancellationToken); + return department; + } +} + diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommandValidator.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommandValidator.cs new file mode 100644 index 0000000..205d29c --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/CreateDepartment/CreateDepartmentCommandValidator.cs @@ -0,0 +1,14 @@ +using FluentValidation; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.CreateDepartment; +public class CreateDepartmentCommandValidator : AbstractValidator +{ + public CreateDepartmentCommandValidator() + { + RuleFor(e => e.Name) + .NotEmpty(); + RuleFor(e => e.Code) + .NotEmpty(); + } +} + diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDepartmentCommandValidator.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDepartmentCommandValidator.cs new file mode 100644 index 0000000..49c0dfa --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDepartmentCommandValidator.cs @@ -0,0 +1,12 @@ +using FluentValidation; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.DeleteDepartment; +public class DeleteDesignationCommandValidator:AbstractValidator +{ + public DeleteDesignationCommandValidator() + { + RuleFor(e => e.Id) + .NotEmpty(); + } +} + diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDesignationCommand.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDesignationCommand.cs new file mode 100644 index 0000000..0f61a98 --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/DeleteDepartment/DeleteDesignationCommand.cs @@ -0,0 +1,28 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.DeleteDepartment; +public class DeleteDepartmentCommand : IRequest +{ + public Guid Id { get; set; } +} +public class DeleteDepartmentCommandHanlder : IRequestHandler +{ + private readonly IEntitiesContext _context; + public DeleteDepartmentCommandHanlder(IEntitiesContext context) + { + _context = context; + } + + public async Task 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; + } +} diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommand.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommand.cs new file mode 100644 index 0000000..0102b19 --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommand.cs @@ -0,0 +1,28 @@ +using Mapster; +using MediatR; +using ThreeTee.Core.Entities; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.UpdateDepartment; +public class UpdateDepartmentCommand : IRequest +{ + public Guid Id { get; set; } + public string Name { get; set; } + public string Code { get; set; } +} + +public class UpdateDepartmentCommandHandler : IRequestHandler +{ + private readonly IEntitiesContext _context; + public UpdateDepartmentCommandHandler(IEntitiesContext context) + { + _context = context; + } + public async Task Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken) + { + var entity = request.Adapt(); + _context.Departments.Update(entity); + + await _context.SaveChangesAsync(cancellationToken); + return entity; + } +} diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommandValidator.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommandValidator.cs new file mode 100644 index 0000000..c57bb8d --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Commands/UpdateDepartment/UpdateDepartmentCommandValidator.cs @@ -0,0 +1,16 @@ +using FluentValidation; + +namespace ThreeTee.Application.Cqrs.Departments.Commands.UpdateDepartment; +public class UpdateDepartmentCommandValidator : AbstractValidator +{ + public UpdateDepartmentCommandValidator() + { + RuleFor(e => e.Name) + .NotEmpty(); + RuleFor(e => e.Id) + .NotEmpty(); + RuleFor(e => e.Code) + .NotEmpty(); + } +} + diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsById.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsById.cs new file mode 100644 index 0000000..ab459b1 --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsById.cs @@ -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 +{ + public Guid Id { get; set; } +} + +public class GetDepartmentsByIdQueryHandler : IRequestHandler +{ + private readonly IEntitiesContext _context; + public GetDepartmentsByIdQueryHandler(IEntitiesContext context) + { + _context = context; + } + public async Task Handle(GetDepartmentsByIdQuery request, CancellationToken cancellationToken) + { + var department = await _context.Departments.ProjectToType().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; + } +} + diff --git a/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsWithPagination.cs b/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsWithPagination.cs new file mode 100644 index 0000000..f98db4e --- /dev/null +++ b/backend/src/ThreeTee.Application/Cqrs/Departments/Queries/GetDepartmentsWithPagination.cs @@ -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> +{ + public int PageNumber { get; set; } = 1; + public int PageSize { get; set; } = 10; +} + +public class GetDepartmentsWithPaginationQueryHandler : IRequestHandler> +{ + private readonly IEntitiesContext _context; + public GetDepartmentsWithPaginationQueryHandler(IEntitiesContext context) + { + _context = context; + } + + public async Task> Handle(GetDepartmentsWithPaginationQuery request, CancellationToken cancellationToken) + { + var departments = await _context.Departments + .ProjectToType() + .PaginatedListAsync(request.PageNumber, request.PageSize); + if (departments != null) + return departments; + return null; + } +} + diff --git a/backend/src/ThreeTee.Application/Interfaces/IEntitiesContext.cs b/backend/src/ThreeTee.Application/Interfaces/IEntitiesContext.cs index 1e47193..a7a171e 100644 --- a/backend/src/ThreeTee.Application/Interfaces/IEntitiesContext.cs +++ b/backend/src/ThreeTee.Application/Interfaces/IEntitiesContext.cs @@ -13,5 +13,6 @@ public interface IEntitiesContext DbSet ProjectUsers { get; set; } DbSet Statuses { get; set; } DbSet UserDepartments { get; set; } + DbSet DepartmentManager { get; set; } Task SaveChangesAsync(CancellationToken cancellationToken); } \ No newline at end of file diff --git a/backend/src/ThreeTee.Application/Models/Departments/DepartmentResponse.cs b/backend/src/ThreeTee.Application/Models/Departments/DepartmentResponse.cs index 44f5e81..429f6fe 100644 --- a/backend/src/ThreeTee.Application/Models/Departments/DepartmentResponse.cs +++ b/backend/src/ThreeTee.Application/Models/Departments/DepartmentResponse.cs @@ -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; } } diff --git a/backend/src/ThreeTee.Infrastructure.Persistence.Npgsql/Data/EntitiesContext.cs b/backend/src/ThreeTee.Infrastructure.Persistence.Npgsql/Data/EntitiesContext.cs index 1921888..ef2f9dd 100644 --- a/backend/src/ThreeTee.Infrastructure.Persistence.Npgsql/Data/EntitiesContext.cs +++ b/backend/src/ThreeTee.Infrastructure.Persistence.Npgsql/Data/EntitiesContext.cs @@ -17,6 +17,7 @@ public class EntitiesContext : IdentityDbContext ProjectUsers { get; set; } public DbSet Statuses { get; set; } public DbSet UserDepartments { get; set; } + public DbSet DepartmentManager { get; set; } public EntitiesContext(DbContextOptions options) : base(options) {