Skip to content

Commit 65f444d

Browse files
Add GroupName to a SwiftLink
1 parent 3ceec34 commit 65f444d

File tree

11 files changed

+293
-11
lines changed

11 files changed

+293
-11
lines changed

src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface ICacheProvider
2929
/// Removes the object associated with the given key.
3030
/// </summary>
3131
/// <param name="key">An object identifying the requested entry.</param>
32-
Task Remove(string key);
32+
Task<bool> Remove(string key);
3333

3434
/// <summary>
3535
/// Gets the item associated with this key if present.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace SwiftLink.Application.UseCases.Links.Commands;
44

55
public record GenerateShortCodeCommand : IRequest<Result<LinksDto>>
66
{
7+
public string GroupName { get; set; }
78
public string Url { get; set; }
89
public string Description { get; set; }
910
public string Title { get; set; }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public async Task<Result<LinksDto>> Handle(GenerateShortCodeCommand request,
3333
Password = request.Password?.Hash(request.Url),
3434
Title = request.Title,
3535
Tags = request.Tags?.ToList(),
36+
GroupName = request.GroupName
3637
};
3738

3839
linkTable.Add(link);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SwiftLink.Application.UseCases.Links.Commands;
55

66
public class GenerateShortCodeValidator : AbstractValidator<GenerateShortCodeCommand>
77
{
8-
public readonly IApplicationDbContext _dbContext;
8+
private readonly IApplicationDbContext _dbContext;
99

1010
public GenerateShortCodeValidator(IApplicationDbContext dbContext)
1111
{

src/SwiftLink.Application/UseCases/Links/Commands/UpdateLink/UpdateLinkCommandHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public async Task<Result<bool>> Handle(UpdateLinkCommand request, CancellationTo
2929
if (dbResult.IsFailure)
3030
return Result.Failure<bool>(CommonMessages.Database.InsertFailed);
3131

32+
await _cache.Remove(link.ShortCode);
3233
await _cache.Set(link.ShortCode, JsonSerializer.Serialize(link), link.ExpirationDate);
3334

3435
return Result.Success(true);

src/SwiftLink.Domain/Entities/Link.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ public class Link : IEntity
1515
public string Description { get; set; }
1616
public DateTime ExpirationDate { get; set; }
1717
public bool IsBanned { get; set; }
18-
public bool IsDisabled { get; set; }
18+
public bool IsDisabled { get; private set; }
1919
public string Password { get; set; }
20+
public string GroupName { get; set; }
2021
public List<Tags> Tags { get; set; }
2122
public ICollection<LinkVisit> LinkVisits { get; set; }
22-
23-
public void Enable()
23+
24+
public void Enable()
2425
=> IsDisabled = false;
25-
26+
2627
public void Disable()
2728
=> IsDisabled = true;
2829
}

src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ public class RedisCacheService(IDistributedCache cache, IOptions<AppSettings> op
1515

1616
private readonly AppSettings _options = options.Value;
1717

18-
public Task Remove(string key)
19-
=> _cache.RemoveAsync(key);
18+
public async Task<bool> Remove(string key)
19+
{
20+
var removeCacheCircuitBreaker = _policyRegistry.Get<AsyncCircuitBreakerPolicy<bool>>(nameof(RedisCashServiceResiliencyKey.RemoveCircuitBreaker));
21+
return removeCacheCircuitBreaker.CircuitState is not CircuitState.Open &&
22+
await removeCacheCircuitBreaker.ExecuteAsync(async () =>
23+
{
24+
try
25+
{
26+
await _cache.RemoveAsync(key);
27+
}
28+
catch (RedisConnectionException)
29+
{
30+
return false;
31+
}
32+
return true;
33+
});
34+
}
2035

2136
public async Task<bool> Set(string key, string value)
2237
=> await Set(key, value, DateTime.Now.AddDays(_options.DefaultExpirationTimeInDays));
@@ -67,5 +82,6 @@ public async Task<string> Get(string key)
6782
public enum RedisCashServiceResiliencyKey
6883
{
6984
SetCircuitBreaker,
70-
GetCircuitBreaker
85+
GetCircuitBreaker,
86+
RemoveCircuitBreaker
7187
}

src/SwiftLink.Infrastructure/Persistence/Migrations/20240213102938_AddGroupNameInLink.Designer.cs

Lines changed: 225 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace SwiftLink.Infrastructure.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class AddGroupNameInLink : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.AddColumn<string>(
14+
name: "GroupName",
15+
schema: "BASE",
16+
table: "Link",
17+
type: "varchar(200)",
18+
unicode: false,
19+
maxLength: 200,
20+
nullable: true);
21+
}
22+
23+
/// <inheritdoc />
24+
protected override void Down(MigrationBuilder migrationBuilder)
25+
{
26+
migrationBuilder.DropColumn(
27+
name: "GroupName",
28+
schema: "BASE",
29+
table: "Link");
30+
}
31+
}
32+
}

src/SwiftLink.Infrastructure/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4040
b.Property<DateTime>("ExpirationDate")
4141
.HasColumnType("datetime2");
4242

43+
b.Property<string>("GroupName")
44+
.HasMaxLength(200)
45+
.IsUnicode(false)
46+
.HasColumnType("varchar(200)");
47+
4348
b.Property<bool>("IsBanned")
4449
.HasColumnType("bit");
4550

0 commit comments

Comments
 (0)