Skip to content

Commit 3e43a5a

Browse files
Add Fields and Migration,
Add Handler and Command and validation.
1 parent 9975da2 commit 3e43a5a

19 files changed

+556
-17
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
global using Microsoft.EntityFrameworkCore;
1+
global using MediatR;
2+
global using Microsoft.EntityFrameworkCore;
23
global using SwiftLink.Domain.Entities;
3-
global using SwiftLink.Shared;
4+
global using SwiftLink.Shared;

src/SwiftLink.Application/UseCases/Links/Commands/GenerateShortCode/GenerateShortCodeCommand.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
using MediatR;
2-
using SwiftLink.Application.Dtos;
1+
using SwiftLink.Application.Dtos;
32

43
namespace SwiftLink.Application.UseCases.Links.Commands;
54

65
public record GenerateShortCodeCommand : IRequest<Result<LinksDto>>
76
{
87
public string Url { get; set; }
98
public string Description { get; set; }
10-
public string? Title { get; set; }
9+
public string Title { get; set; }
1110
public DateTime? ExpirationDate { get; set; }
1211
public string Password { get; set; }
1312
}
1413

15-
public record GenerateShortCodeDto
14+
internal record GenerateShortCodeDto
1615
{
1716
public string Url { get; set; }
1817
public string Description { get; set; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace SwiftLink.Application.UseCases.Subscribers.Commands;
2+
3+
public record AddSubscriberCommand(string Email,string Name) : IRequest<Result<Guid>>;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using SwiftLink.Application.Common;
2+
using SwiftLink.Application.Common.Interfaces;
3+
4+
namespace SwiftLink.Application.UseCases.Subscribers.Commands;
5+
public class AddSubscriberCommandHandler(IApplicationDbContext dbContext)
6+
: IRequestHandler<AddSubscriberCommand, Result<Guid>>
7+
{
8+
private readonly IApplicationDbContext _dbContext = dbContext;
9+
10+
public async Task<Result<Guid>> Handle(AddSubscriberCommand request, CancellationToken cancellationToken)
11+
{
12+
Subscriber subscriber = new()
13+
{
14+
Email = request.Email,
15+
IsActive = true,
16+
Name = request.Name,
17+
Token = Guid.NewGuid()
18+
};
19+
_dbContext.Set<Subscriber>().Add(subscriber);
20+
var result = await _dbContext.SaveChangesAsync(cancellationToken);
21+
if (result.IsFailure)
22+
return Result.Failure<Guid>(CommonMessages.Database.InsertFailed);
23+
24+
return Result.Success(subscriber.Token);
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using FluentValidation;
2+
using SwiftLink.Application.Common.Interfaces;
3+
4+
namespace SwiftLink.Application.UseCases.Subscribers.Commands;
5+
6+
public class AddSubscriberCommandValidator : AbstractValidator<AddSubscriberCommand>
7+
{
8+
public readonly IApplicationDbContext _dbContext;
9+
10+
public AddSubscriberCommandValidator(IApplicationDbContext dbContext)
11+
{
12+
_dbContext = dbContext;
13+
14+
RuleFor(x => x.Name)
15+
.NotNull().WithMessage(SubscriberMessage.NameMustBeSent.Message);
16+
17+
RuleFor(x => x.Email)
18+
.NotNull().WithMessage(SubscriberMessage.EmailMustBeSent.Message)
19+
.Must(BeAValidEmail).WithMessage(SubscriberMessage.EmailFormatIsIncorrect.Message)
20+
.MustAsync(BeAUniqueEmail).WithMessage(SubscriberMessage.EmailMustBeUnique.Message);
21+
}
22+
23+
private bool BeAValidEmail(string email)
24+
=> EmailFormatChecker.EmailRegex().IsMatch(email);
25+
26+
private Task<bool> BeAUniqueEmail(string email, CancellationToken cancellationToken)
27+
=> _dbContext.Set<Subscriber>().AnyAsync(x => x.Email == email, cancellationToken);
28+
}

src/SwiftLink.Application/UseCases/Subscribers/SubscriberMessage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ internal record SubscriberMessage
44
{
55
public static readonly Error SubscriberMustBeSent = Error.Validation("TokenNotSended", "Token must be sent!");
66
public static readonly Error InvalidToken = Error.NotFound("UnAuthorized", "Invalid User!");
7+
8+
public static readonly Error NameMustBeSent = Error.Validation("Validation", "Name must be sent.");
9+
public static readonly Error EmailMustBeSent = Error.Validation("Validation", "Email must be sent.");
10+
public static readonly Error EmailFormatIsIncorrect = Error.Validation("Validation", "Email must be in correct format.");
11+
public static readonly Error EmailMustBeUnique = Error.Validation("Validation", "Email is exist in our system.");
712
}

src/SwiftLink.Domain/Entities/Subscriber.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public class Subscriber : IEntity
99
public int Id { get; set; }
1010
public Guid Token { get; set; }
1111
public string Name { get; set; }
12+
public string Email { get; set; }
1213
public bool IsActive { get; set; }
1314
}

src/SwiftLink.Infrastructure/Persistence/Config/LinkConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ public void Configure(EntityTypeBuilder<Link> builder)
4040
builder.Property(x => x.IsDisabled)
4141
.HasDefaultValue(false);
4242
}
43-
}
43+
}

src/SwiftLink.Infrastructure/Persistence/Config/LinkVisitConfig.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public class LinkVisitConfig : IEntityTypeConfiguration<LinkVisit>
66
{
77
public void Configure(EntityTypeBuilder<LinkVisit> builder)
88
{
9-
builder.ToTable(t =>
10-
t.HasComment("analytics, providing insights into the number of users who clicked on a shortened link."));
9+
builder.ToTable(t => t.HasComment("analytics, providing insights into the number of users who clicked on a shortened link."));
1110

1211
builder.HasKey(t => t.Id)
1312
.HasName("PK_Base_LinkVisit");
@@ -19,4 +18,4 @@ public void Configure(EntityTypeBuilder<LinkVisit> builder)
1918
builder.Property(x => x.Date)
2019
.IsRequired();
2120
}
22-
}
21+
}

src/SwiftLink.Infrastructure/Persistence/Config/SubscriberConfig.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ public class SubscriberConfig : IEntityTypeConfiguration<Subscriber>
66
{
77
public void Configure(EntityTypeBuilder<Subscriber> builder)
88
{
9-
builder.ToTable(
10-
t => t.HasComment("Only these subscribers are allowed to insert a URL to obtain a shorter one."));
9+
builder.ToTable(t => t.HasComment("Only these subscribers are allowed to insert a URL to obtain a shorter one."));
1110

1211
builder.HasKey(t => t.Id)
1312
.HasName("PK_Base_Subscriber");
@@ -16,10 +15,17 @@ public void Configure(EntityTypeBuilder<Subscriber> builder)
1615
.IsRequired()
1716
.HasMaxLength(50);
1817

18+
builder.Property(x => x.Email)
19+
.IsRequired()
20+
.HasMaxLength(75);
21+
22+
builder.HasIndex(x => x.Email)
23+
.IsUnique();
24+
1925
builder.Property(x => x.Token)
2026
.IsRequired();
2127

2228
builder.Property(x => x.IsActive)
2329
.IsRequired();
2430
}
25-
}
31+
}

0 commit comments

Comments
 (0)