initial commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class CreateBudgetRequest
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? BudgetDate { get; set; } = DateTime.Today;
|
||||
public BudgetStatus Status { get; set; } = BudgetStatus.Draft;
|
||||
public decimal? Amount { get; set; }
|
||||
public string? CampaignId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateBudgetResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateBudgetCommand(CreateBudgetRequest Data) : IRequest<CreateBudgetResponse>;
|
||||
|
||||
public class CreateBudgetHandler : IRequestHandler<CreateBudgetCommand, CreateBudgetResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateBudgetHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateBudgetResponse> Handle(CreateBudgetCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Budget);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Budget
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Title = request.Data.Title,
|
||||
Description = request.Data.Description,
|
||||
BudgetDate = request.Data.BudgetDate,
|
||||
Status = request.Data.Status,
|
||||
Amount = request.Data.Amount,
|
||||
CampaignId = request.Data.CampaignId
|
||||
};
|
||||
|
||||
_context.Budget.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateBudgetResponse { Id = entity.Id, AutoNumber = entity.AutoNumber };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class CreateBudgetValidator : AbstractValidator<CreateBudgetRequest>
|
||||
{
|
||||
public CreateBudgetValidator()
|
||||
{
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Title is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CampaignId)
|
||||
.NotEmpty().WithMessage("Campaign is required");
|
||||
|
||||
RuleFor(x => x.Amount)
|
||||
.GreaterThanOrEqualTo(0).WithMessage("Amount must be zero or greater");
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue() => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateBudgetRequest>.CreateWithOptions((CreateBudgetRequest)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid) return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public record DeleteBudgetByIdRequest(string Id);
|
||||
|
||||
public record DeleteBudgetByIdCommand(DeleteBudgetByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteBudgetByIdHandler : IRequestHandler<DeleteBudgetByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteBudgetByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteBudgetByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Budget
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Budget.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class GetBudgetByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? BudgetDate { get; set; }
|
||||
public BudgetStatus Status { get; set; }
|
||||
public decimal? Amount { get; set; }
|
||||
public string? CampaignId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBudgetByIdQuery(string Id) : IRequest<GetBudgetByIdResponse?>;
|
||||
|
||||
public class GetBudgetByIdHandler : IRequestHandler<GetBudgetByIdQuery, GetBudgetByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBudgetByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetBudgetByIdResponse?> Handle(GetBudgetByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Budget
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetBudgetByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Title = x.Title,
|
||||
Description = x.Description,
|
||||
BudgetDate = x.BudgetDate,
|
||||
Status = x.Status,
|
||||
Amount = x.Amount,
|
||||
CampaignId = x.CampaignId,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class GetBudgetListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? CampaignTitle { get; set; }
|
||||
public decimal? Amount { get; set; }
|
||||
public DateTime? BudgetDate { get; set; }
|
||||
public BudgetStatus Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetBudgetListQuery() : IRequest<List<GetBudgetListResponse>>;
|
||||
|
||||
public class GetBudgetListHandler : IRequestHandler<GetBudgetListQuery, List<GetBudgetListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBudgetListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetBudgetListResponse>> Handle(GetBudgetListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Budget
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Campaign)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetBudgetListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Title = x.Title,
|
||||
CampaignTitle = x.Campaign != null ? x.Campaign.Title : string.Empty,
|
||||
Amount = x.Amount,
|
||||
BudgetDate = x.BudgetDate,
|
||||
Status = x.Status,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class BudgetLookupResponse
|
||||
{
|
||||
public List<LookupItem> Campaigns { get; set; } = new();
|
||||
public List<LookupItem> Statuses { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public int Value { get; set; }
|
||||
}
|
||||
|
||||
public record GetBudgetLookupQuery() : IRequest<BudgetLookupResponse>;
|
||||
|
||||
public class GetBudgetLookupHandler : IRequestHandler<GetBudgetLookupQuery, BudgetLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetBudgetLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<BudgetLookupResponse> Handle(GetBudgetLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new BudgetLookupResponse();
|
||||
|
||||
response.Campaigns = await _context.Campaign
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Title)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Title })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(BudgetStatus))
|
||||
.Cast<BudgetStatus>()
|
||||
.Select(x => new LookupItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.ToString()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class UpdateBudgetRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? BudgetDate { get; set; }
|
||||
public BudgetStatus Status { get; set; }
|
||||
public decimal? Amount { get; set; }
|
||||
public string? CampaignId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateBudgetResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateBudgetCommand(UpdateBudgetRequest Data) : IRequest<UpdateBudgetResponse>;
|
||||
|
||||
public class UpdateBudgetHandler : IRequestHandler<UpdateBudgetCommand, UpdateBudgetResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateBudgetHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateBudgetResponse> Handle(UpdateBudgetCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Budget
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return new UpdateBudgetResponse { Success = false };
|
||||
|
||||
entity.Title = request.Data.Title;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.BudgetDate = request.Data.BudgetDate;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.Amount = request.Data.Amount;
|
||||
entity.CampaignId = request.Data.CampaignId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateBudgetResponse { Id = entity.Id, Success = true };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Budget.Cqrs;
|
||||
|
||||
public class UpdateBudgetValidator : AbstractValidator<UpdateBudgetRequest>
|
||||
{
|
||||
public UpdateBudgetValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Title is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CampaignId)
|
||||
.NotEmpty().WithMessage("Campaign is required");
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue() => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateBudgetRequest>.CreateWithOptions((UpdateBudgetRequest)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid) return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user