Added shared projects

This commit is contained in:
Khwezi Mngoma
2026-05-03 16:10:27 +02:00
parent 66c08dc54b
commit 1b997013bb
81 changed files with 4507 additions and 1 deletions
@@ -0,0 +1,64 @@
namespace LiteCharms.Features.Customers.Commands;
public class CreateCustomerCommand : IRequest<Result<Guid>>
{
public string? Company { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string? Tax { get; set; }
public string Email { get; set; }
public string? Discord { get; set; }
public string? Slack { get; set; }
public string? LinkedIn { get; set; }
public string? Whatsapp { get; set; }
public string? Website { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? Region { get; set; }
public string? Country { get; set; }
public string? PostalCode { get; set; }
private CreateCustomerCommand(string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode)
{
Name = name;
LastName = lastName;
Company = company;
Tax = tax;
Email = email;
Discord = discord;
Slack = slack;
LinkedIn = linkedIn;
Whatsapp = whatsapp;
Website = website;
Phone = phone;
Address = address;
City = city;
Region = region;
Country = country;
PostalCode = postalCode;
}
public static CreateCustomerCommand Create(string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode)
{
if (string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(lastName) && string.IsNullOrWhiteSpace(email))
throw new ArgumentException("At the following fields must be provided: Name, LastName, Email");
return new(name, lastName, company, tax, email, discord, slack, linkedIn, whatsapp, website, phone, address, city, region, country, postalCode);
}
}
@@ -0,0 +1,48 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.Customers.Commands.Handlers;
public class CreateCustomerCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateCustomerCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var customerEmail = request.Email.ToLower().Trim();
if (await context.Customers.AnyAsync(c => c.Email == customerEmail, cancellationToken))
return Result.Fail<Guid>(new Error($"A customer with the email {customerEmail} already exists"));
var newCustomer = context.Customers.Add(new Entities.Customer
{
Company = request.Company,
Name = request.Name,
LastName = request.LastName,
Tax = request.Tax,
Email = customerEmail,
Discord = request.Discord,
Slack = request.Slack,
LinkedIn = request.LinkedIn,
Whatsapp = request.Whatsapp,
Website = request.Website,
Phone = request.Phone,
Address = request.Address,
City = request.City,
Region = request.Region,
Country = request.Country,
PostalCode = request.PostalCode,
Active = true,
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(newCustomer.Entity.Id)
: Result.Fail<Guid>(new Error($"Failed to create customer {customerEmail}"));
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,38 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.Customers.Commands.Handlers;
public class RefundCustomerCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<RefundCustomerCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(RefundCustomerCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if(!await context.Orders.AnyAsync(o => o.Id == request.OrderId, cancellationToken))
return Result.Fail<Guid>(new Error($"Order with Id: {request.OrderId} does not exist"));
if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
return Result.Fail<Guid>(new Error($"Customer with Id: {request.CustomerId} does not exist"));
if(!await context.Orders.AnyAsync(o => o.Id == request.OrderId && o.CustomerId == request.CustomerId, cancellationToken))
return Result.Fail<Guid>(new Error($"Order with Id: {request.OrderId} does not belong to Customer with Id: {request.CustomerId}"));
var refund = context.OrderRefunds.Add(new Entities.OrderRefund
{
OrderId = request.OrderId,
Reason = request.Reason,
Amount = request.Amount
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(refund.Entity.Id)
: Result.Fail<Guid>(new Error($"Failed to create refund for OrderId: {request.OrderId}"));
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,44 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.Customers.Commands.Handlers;
public class UpdateCustomerCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateCustomerCommand, Result>
{
public async ValueTask<Result> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken);
if (customer is null)
return Result.Fail(new Error($"Customer with ID {request.CustomerId} not found."));
customer.Name = request.Name;
customer.LastName = request.LastName;
customer.Email = request.Email;
customer.Company = request.Company;
customer.Address = request.Address;
customer.City = request.City;
customer.Region = request.Region;
customer.Country = request.Country;
customer.PostalCode = request.PostalCode;
customer.Phone = request.Phone;
customer.Tax = request.Tax;
customer.City = request.City;
customer.Discord = request.Discord;
customer.Slack = request.Slack;
customer.LinkedIn = request.LinkedIn;
customer.Whatsapp = request.Whatsapp;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to update the customer {request.CustomerId}."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,38 @@
namespace LiteCharms.Features.Customers.Commands;
public class RefundCustomerCommand : IRequest<Result<Guid>>
{
public Guid OrderId { get; set; }
public Guid CustomerId { get; set; }
public string? Reason { get; set; }
public decimal Amount { get; set; }
private RefundCustomerCommand(Guid orderId, Guid customerId, string? reason, decimal amount)
{
OrderId = orderId;
CustomerId = customerId;
Reason = reason;
Amount = amount;
CustomerId = customerId;
}
public static RefundCustomerCommand Create(Guid orderId, Guid customerId, string? reason, decimal amount)
{
if (orderId == Guid.Empty)
throw new ArgumentException("OrderId is required", nameof(orderId));
if (customerId == Guid.Empty)
throw new ArgumentException("CustomerId is required", nameof(customerId));
if (amount <= 0)
throw new ArgumentException("Amount must be greater than zero", nameof(amount));
if (string.IsNullOrWhiteSpace(reason))
throw new ArgumentException("Reason is required", nameof(reason));
return new(orderId, customerId, reason, amount);
}
}
@@ -0,0 +1,70 @@
namespace LiteCharms.Features.Customers.Commands;
public class UpdateCustomerCommand : IRequest<Result>
{
public Guid CustomerId { get; set; }
public string? Company { get; set; }
public string? Name { get; set; }
public string? LastName { get; set; }
public string? Tax { get; set; }
public string? Email { get; set; }
public string? Discord { get; set; }
public string? Slack { get; set; }
public string? LinkedIn { get; set; }
public string? Whatsapp { get; set; }
public string? Website { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? Region { get; set; }
public string? Country { get; set; }
public string? PostalCode { get; set; }
private UpdateCustomerCommand(Guid customerId, string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode)
{
CustomerId = customerId;
Name = name;
LastName = lastName;
Company = company;
Tax = tax;
Email = email;
Discord = discord;
Slack = slack;
LinkedIn = linkedIn;
Whatsapp = whatsapp;
Website = website;
Phone = phone;
Address = address;
City = city;
Region = region;
Country = country;
PostalCode = postalCode;
}
public static UpdateCustomerCommand Create(Guid customerId, string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode)
{
if (customerId == Guid.Empty)
throw new ArgumentException("Customer ID is required.", nameof(customerId));
if (string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(lastName) && string.IsNullOrWhiteSpace(email))
throw new ArgumentException("At the following fields must be provided: Name, LastName, Email");
return new(customerId, name, lastName, company, tax, email, discord, slack, linkedIn, whatsapp, website, phone, address, city, region, country, postalCode);
}
}