initial commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class CreateExpenseRequest
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? ExpenseDate { get; set; } = DateTime.Today;
|
||||
public ExpenseStatus Status { get; set; } = ExpenseStatus.Draft;
|
||||
public decimal? Amount { get; set; }
|
||||
public string? CampaignId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateExpenseResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateExpenseCommand(CreateExpenseRequest Data) : IRequest<CreateExpenseResponse>;
|
||||
|
||||
public class CreateExpenseHandler : IRequestHandler<CreateExpenseCommand, CreateExpenseResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateExpenseHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateExpenseResponse> Handle(CreateExpenseCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Expense);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Expense
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Title = request.Data.Title,
|
||||
Description = request.Data.Description,
|
||||
ExpenseDate = request.Data.ExpenseDate,
|
||||
Status = request.Data.Status,
|
||||
Amount = request.Data.Amount,
|
||||
CampaignId = request.Data.CampaignId
|
||||
};
|
||||
|
||||
_context.Expense.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateExpenseResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class CreateExpenseValidator : AbstractValidator<CreateExpenseRequest>
|
||||
{
|
||||
public CreateExpenseValidator()
|
||||
{
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Expense Title is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CampaignId)
|
||||
.NotEmpty().WithMessage("Campaign is required");
|
||||
|
||||
RuleFor(x => x.Amount)
|
||||
.NotNull().WithMessage("Amount is required");
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue() => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateExpenseRequest>.CreateWithOptions((CreateExpenseRequest)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.Expense.Cqrs;
|
||||
|
||||
public record DeleteExpenseByIdRequest(string Id);
|
||||
|
||||
public record DeleteExpenseByIdCommand(DeleteExpenseByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteExpenseByIdHandler : IRequestHandler<DeleteExpenseByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteExpenseByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteExpenseByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Expense
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Expense.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class GetExpenseByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? ExpenseDate { get; set; }
|
||||
public Data.Enums.ExpenseStatus 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 GetExpenseByIdQuery(string Id) : IRequest<GetExpenseByIdResponse?>;
|
||||
|
||||
public class GetExpenseByIdHandler : IRequestHandler<GetExpenseByIdQuery, GetExpenseByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetExpenseByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetExpenseByIdResponse?> Handle(GetExpenseByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Expense
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetExpenseByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Title = x.Title,
|
||||
Description = x.Description,
|
||||
ExpenseDate = x.ExpenseDate,
|
||||
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,44 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class GetExpenseListResponse
|
||||
{
|
||||
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? ExpenseDate { get; set; }
|
||||
public Data.Enums.ExpenseStatus Status { get; set; }
|
||||
}
|
||||
|
||||
public record GetExpenseListQuery() : IRequest<List<GetExpenseListResponse>>;
|
||||
|
||||
public class GetExpenseListHandler : IRequestHandler<GetExpenseListQuery, List<GetExpenseListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetExpenseListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetExpenseListResponse>> Handle(GetExpenseListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Expense
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Campaign)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetExpenseListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Title = x.Title,
|
||||
CampaignTitle = x.Campaign != null ? x.Campaign.Title : string.Empty,
|
||||
Amount = x.Amount,
|
||||
ExpenseDate = x.ExpenseDate,
|
||||
Status = x.Status
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class ExpenseLookupResponse
|
||||
{
|
||||
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 GetExpenseLookupQuery() : IRequest<ExpenseLookupResponse>;
|
||||
|
||||
public class GetExpenseLookupHandler : IRequestHandler<GetExpenseLookupQuery, ExpenseLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetExpenseLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<ExpenseLookupResponse> Handle(GetExpenseLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new ExpenseLookupResponse();
|
||||
|
||||
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(ExpenseStatus))
|
||||
.Cast<ExpenseStatus>()
|
||||
.Select(x => new LookupItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.ToString()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class UpdateExpenseRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? ExpenseDate { get; set; }
|
||||
public ExpenseStatus 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 UpdateExpenseResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateExpenseCommand(UpdateExpenseRequest Data) : IRequest<UpdateExpenseResponse>;
|
||||
|
||||
public class UpdateExpenseHandler : IRequestHandler<UpdateExpenseCommand, UpdateExpenseResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateExpenseHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateExpenseResponse> Handle(UpdateExpenseCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Expense
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateExpenseResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Title = request.Data.Title;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.ExpenseDate = request.Data.ExpenseDate;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.Amount = request.Data.Amount;
|
||||
entity.CampaignId = request.Data.CampaignId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateExpenseResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.Expense.Cqrs;
|
||||
|
||||
public class UpdateExpenseValidator : AbstractValidator<UpdateExpenseRequest>
|
||||
{
|
||||
public UpdateExpenseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Expense 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<UpdateExpenseRequest>.CreateWithOptions((UpdateExpenseRequest)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