Retructured solution
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
namespace LiteCharms.Features.Leads.Commands;
|
||||
|
||||
public class CreateLeadCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
public Guid? CustomerId { get; set; }
|
||||
|
||||
public string? Source { get; set; }
|
||||
|
||||
public string? ClickId { get; set; }
|
||||
|
||||
public string? WebClickId { get; set; }
|
||||
|
||||
public string? AppClickId { get; set; }
|
||||
|
||||
public long? CampaignId { get; set; }
|
||||
|
||||
public long? AdGroupId { get; set; }
|
||||
|
||||
public long? AdName { get; set; }
|
||||
|
||||
public long? TargetId { get; set; }
|
||||
|
||||
public long? FeedItemId { get; set; }
|
||||
|
||||
public string? ClickLocation { get; set; }
|
||||
|
||||
public string? AttribusionHash { get; set; }
|
||||
|
||||
private CreateLeadCommand(Guid? customerId, string source, string clickId, string webClickId, string appClickId, long? campaignId, long? adGroupId, long? adName, long? targetId, long? feedItemId, string? clickLocation, string? attribusionHash)
|
||||
{
|
||||
CustomerId = customerId;
|
||||
Source = source;
|
||||
ClickId = clickId;
|
||||
WebClickId = webClickId;
|
||||
AppClickId = appClickId;
|
||||
CampaignId = campaignId;
|
||||
AdGroupId = adGroupId;
|
||||
AdName = adName;
|
||||
TargetId = targetId;
|
||||
FeedItemId = feedItemId;
|
||||
ClickLocation = clickLocation;
|
||||
AttribusionHash = attribusionHash;
|
||||
}
|
||||
|
||||
public static CreateLeadCommand Create(Guid? customerId, string source, string clickId, string webClickId, string appClickId, long? campaignId, long? adGroupId, long? adName, long? targetId, long? feedItemId, string? clickLocation, string? attribusionHash)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(source))
|
||||
throw new ArgumentNullException("Lead source is required to create a lead.", nameof(source));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(clickId) || string.IsNullOrWhiteSpace(appClickId) || string.IsNullOrWhiteSpace(webClickId))
|
||||
throw new ArgumentException("ClickId, App ClickId and Web ClickId are required to create a lead.");
|
||||
|
||||
return new(customerId, source, clickId, webClickId, appClickId, campaignId, adGroupId, adName, targetId, feedItemId, clickLocation, attribusionHash);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Utilities.Hash.Commands;
|
||||
|
||||
namespace LiteCharms.Features.Leads.Commands.Handlers;
|
||||
|
||||
public class CreateLeadCommandHandler(IDbContextFactory<ShopDbContext> contextFactory, ISender mediator) : IRequestHandler<CreateLeadCommand, Result<Guid>>
|
||||
{
|
||||
public async ValueTask<Result<Guid>> Handle(CreateLeadCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var hashCommand = ComputeHashCommand.Create($"{request.ClickId}{request.AppClickId}{request.WebClickId}");
|
||||
var hashResult = await mediator.Send(hashCommand, cancellationToken);
|
||||
|
||||
if(hashResult.IsFailed)
|
||||
return Result.Fail<Guid>(new Error($"Failed to compute hash for lead -> Google ClickId: {request.ClickId}, App ClickId: {request.AppClickId}, Web ClickId: {request.WebClickId}")
|
||||
.CausedBy(hashResult.Errors));
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var newLead = context.Leads.Add(new Entities.Lead
|
||||
{
|
||||
WebClickId = request.WebClickId,
|
||||
AppClickId = request.AppClickId,
|
||||
Source = request.Source,
|
||||
ClickId = request.ClickId,
|
||||
AdGroupId = request.AdGroupId,
|
||||
AdName = request.AdName,
|
||||
CampaignId = request.CampaignId,
|
||||
ClickLocation = request.ClickLocation,
|
||||
CustomerId = request.CustomerId,
|
||||
FeedItemId = request.FeedItemId,
|
||||
Status = Models.LeadStatus.New,
|
||||
TargetId = request.TargetId,
|
||||
AttributionHash = hashResult.Value
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(newLead.Entity.Id)
|
||||
: Result.Fail<Guid>(new Error($"Failed to create lead -> Google ClickId: {request.ClickId}, App ClickId: {request.AppClickId}, Web ClickId: {request.WebClickId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
|
||||
namespace LiteCharms.Features.Leads.Commands.Handlers;
|
||||
|
||||
public class UpdateLeadCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateLeadCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(UpdateLeadCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var lead = await context.Leads.FirstOrDefaultAsync(l => l.Id == request.LeadId, cancellationToken);
|
||||
|
||||
if (lead is null)
|
||||
return Result.Fail(new Error($"Lead with ID {request.LeadId} not found."));
|
||||
|
||||
lead.Status = request.Status;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to update the lead {request.LeadId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using LiteCharms.Features.Shop;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Leads.Commands;
|
||||
|
||||
public class UpdateLeadCommand : IRequest<Result>
|
||||
{
|
||||
public Guid LeadId { get; set; }
|
||||
|
||||
public LeadStatus Status { get; set; }
|
||||
|
||||
private UpdateLeadCommand(Guid leadId, LeadStatus status)
|
||||
{
|
||||
LeadId = leadId;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public static UpdateLeadCommand Create(Guid leadId, LeadStatus status)
|
||||
{
|
||||
if (leadId == Guid.Empty)
|
||||
throw new ArgumentException("Lead ID cannot be empty.", nameof(leadId));
|
||||
|
||||
if (!Enum.IsDefined(typeof(LeadStatus), status))
|
||||
throw new ArgumentException("Invalid lead status.", nameof(status));
|
||||
|
||||
return new(leadId, status);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user