initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class CreateTenantRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public bool IsActive { get; set; } = true;
|
||||
}
|
||||
|
||||
public class CreateTenantResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateTenantCommand(CreateTenantRequest Data) : IRequest<CreateTenantResponse>;
|
||||
|
||||
public class CreateTenantHandler : IRequestHandler<CreateTenantCommand, CreateTenantResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateTenantHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateTenantResponse> Handle(CreateTenantCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Tenant
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Tenant", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.Tenant
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
Street = request.Data.Street,
|
||||
City = request.Data.City,
|
||||
State = request.Data.State,
|
||||
ZipCode = request.Data.ZipCode,
|
||||
Country = request.Data.Country,
|
||||
PhoneNumber = request.Data.PhoneNumber,
|
||||
FaxNumber = request.Data.FaxNumber,
|
||||
EmailAddress = request.Data.EmailAddress,
|
||||
Website = request.Data.Website,
|
||||
IsActive = request.Data.IsActive
|
||||
};
|
||||
|
||||
_context.Tenant.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateTenantResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class CreateTenantValidator : AbstractValidator<CreateTenantRequest>
|
||||
{
|
||||
public CreateTenantValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Tenant Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public record DeleteTenantByIdRequest(string Id);
|
||||
|
||||
public record DeleteTenantByIdCommand(DeleteTenantByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteTenantByIdHandler : IRequestHandler<DeleteTenantByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteTenantByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteTenantByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Tenant
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Tenant.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class TenantUserItemResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? UserId { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
|
||||
public class GetTenantByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<TenantUserItemResponse> Users { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetTenantByIdQuery(string Id) : IRequest<GetTenantByIdResponse?>;
|
||||
|
||||
public class GetTenantByIdHandler : IRequestHandler<GetTenantByIdQuery, GetTenantByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTenantByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetTenantByIdResponse?> Handle(GetTenantByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Tenant
|
||||
.AsNoTracking()
|
||||
.Include(x => x.TenantUserList)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetTenantByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Street = x.Street,
|
||||
City = x.City,
|
||||
State = x.State,
|
||||
ZipCode = x.ZipCode,
|
||||
Country = x.Country,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
FaxNumber = x.FaxNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
Website = x.Website,
|
||||
IsActive = x.IsActive,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
Users = x.TenantUserList
|
||||
.OrderBy(u => u.Summary)
|
||||
.Select(u => new TenantUserItemResponse
|
||||
{
|
||||
Id = u.Id,
|
||||
UserId = u.UserId,
|
||||
Summary = u.Summary,
|
||||
IsActive = u.IsActive
|
||||
}).ToList()
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class GetTenantListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? City { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
|
||||
public record GetTenantListQuery() : IRequest<List<GetTenantListResponse>>;
|
||||
|
||||
public class GetTenantListHandler : IRequestHandler<GetTenantListQuery, List<GetTenantListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTenantListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetTenantListResponse>> Handle(GetTenantListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Tenant
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetTenantListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
City = x.City,
|
||||
IsActive = x.IsActive
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class LookupTenantResponse
|
||||
{
|
||||
public List<LookupItem> Tenants { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record LookupTenantQuery() : IRequest<LookupTenantResponse>;
|
||||
|
||||
public class LookupTenantHandler : IRequestHandler<LookupTenantQuery, LookupTenantResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public LookupTenantHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<LookupTenantResponse> Handle(LookupTenantQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new LookupTenantResponse();
|
||||
|
||||
result.Tenants = await _context.Tenant
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class UpdateTenantRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Street { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? State { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? FaxNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Website { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTenantResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateTenantCommand(UpdateTenantRequest Data) : IRequest<UpdateTenantResponse>;
|
||||
|
||||
public class UpdateTenantHandler : IRequestHandler<UpdateTenantCommand, UpdateTenantResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateTenantHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateTenantResponse> Handle(UpdateTenantCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Tenant
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Tenant", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Tenant
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateTenantResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.Street = request.Data.Street;
|
||||
entity.City = request.Data.City;
|
||||
entity.State = request.Data.State;
|
||||
entity.ZipCode = request.Data.ZipCode;
|
||||
entity.Country = request.Data.Country;
|
||||
entity.PhoneNumber = request.Data.PhoneNumber;
|
||||
entity.FaxNumber = request.Data.FaxNumber;
|
||||
entity.EmailAddress = request.Data.EmailAddress;
|
||||
entity.Website = request.Data.Website;
|
||||
entity.IsActive = request.Data.IsActive;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateTenantResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Multitenant.Tenant.Cqrs;
|
||||
|
||||
public class UpdateTenantValidator : AbstractValidator<UpdateTenantRequest>
|
||||
{
|
||||
public UpdateTenantValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Tenant Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user