Skip to content

Commit e2aeb45

Browse files
Merge pull request #34 from ScriptSage001/master
Added Cache Invalidations and minor response status codes changes
2 parents be24917 + 47f2932 commit e2aeb45

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

Shortify.NET.API/BaseApiController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ protected IActionResult HandleFailure(Result result, bool isRedirectToErrorPage
4949
{
5050
ErrorType.NotFound => StatusCodes.Status404NotFound,
5151
ErrorType.Validation => StatusCodes.Status400BadRequest,
52+
ErrorType.BadRequest => StatusCodes.Status400BadRequest,
5253
ErrorType.Conflict => StatusCodes.Status409Conflict,
5354
ErrorType.Unauthorized => StatusCodes.Status401Unauthorized,
5455
ErrorType.Gone => StatusCodes.Status410Gone,
56+
ErrorType.NoContent => StatusCodes.Status204NoContent,
5557
_ => StatusCodes.Status500InternalServerError
5658
};
5759

@@ -60,6 +62,11 @@ protected IActionResult HandleFailure(Result result, bool isRedirectToErrorPage
6062
return Redirect($"/error/{statusCode}");
6163
}
6264

65+
if (statusCode == StatusCodes.Status204NoContent)
66+
{
67+
return NoContent();
68+
}
69+
6370
return Problem(
6471
statusCode: statusCode,
6572
type: Enum.GetName(typeof(ErrorType), error.Type),

Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using Shortify.NET.Application.Abstractions.Repositories;
1+
using Shortify.NET.Application.Abstractions;
2+
using Shortify.NET.Application.Abstractions.Repositories;
3+
using Shortify.NET.Application.Shared;
24
using Shortify.NET.Common.FunctionalTypes;
35
using Shortify.NET.Common.Messaging.Abstractions;
46
using Shortify.NET.Core;
7+
using Shortify.NET.Core.Entites;
58
using Shortify.NET.Core.Errors;
69

710
namespace Shortify.NET.Application.Url.Commands.DeleteUrl;
@@ -11,10 +14,12 @@ namespace Shortify.NET.Application.Url.Commands.DeleteUrl;
1114
/// </summary>
1215
internal sealed class DeleteShortenedUrlByIdCommandHandler(
1316
IShortenedUrlRepository shortenedUrlRepository,
17+
ICachingServices cachingServices,
1418
IUnitOfWork unitOfWork)
1519
: ICommandHandler<DeleteShortenedUrlByIdCommand>
1620
{
1721
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;
22+
private readonly ICachingServices _cachingServices = cachingServices;
1823
private readonly IUnitOfWork _unitOfWork = unitOfWork;
1924

2025
/// <summary>
@@ -29,8 +34,15 @@ public async Task<Result> Handle(DeleteShortenedUrlByIdCommand command, Cancella
2934
if (url is null) return Result.Failure(DomainErrors.ShortenedUrl.ShortenedUrlNotFound);
3035

3136
_shortenedUrlRepository.Delete(url);
37+
await RemoveFromCacheAsync(url, cancellationToken);
3238
await _unitOfWork.SaveChangesAsync(cancellationToken);
3339

3440
return Result.Success();
3541
}
42+
43+
private async Task RemoveFromCacheAsync(ShortenedUrl shortenedUrl, CancellationToken cancellationToken)
44+
{
45+
var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{shortenedUrl.Code}";
46+
await _cachingServices.RemoveAsync(cacheKey, cancellationToken);
47+
}
3648
}

Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Shortify.NET.Application.Abstractions.Repositories;
1+
using Shortify.NET.Application.Abstractions;
2+
using Shortify.NET.Application.Abstractions.Repositories;
3+
using Shortify.NET.Application.Shared;
24
using Shortify.NET.Application.Shared.Models;
35
using Shortify.NET.Common.FunctionalTypes;
46
using Shortify.NET.Common.Messaging.Abstractions;
@@ -9,10 +11,14 @@ namespace Shortify.NET.Application.Url.Commands.UpdateUrl
911
{
1012
internal sealed class UpdateShortenedUrlCommandHandler(
1113
IShortenedUrlRepository shortenedUrlRepository,
14+
ICachingServices cachingServices,
1215
IUnitOfWork unitOfWork)
1316
: ICommandHandler<UpdateShortenedUrlCommand, ShortenedUrlDto>
1417
{
1518
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;
19+
20+
private readonly ICachingServices _cachingServices = cachingServices;
21+
1622
private readonly IUnitOfWork _unitOfWork = unitOfWork;
1723

1824
public async Task<Result<ShortenedUrlDto>> Handle(
@@ -27,7 +33,7 @@ public async Task<Result<ShortenedUrlDto>> Handle(
2733
_shortenedUrlRepository.Update(url);
2834
await _unitOfWork.SaveChangesAsync(cancellationToken);
2935

30-
return new ShortenedUrlDto(
36+
var response = new ShortenedUrlDto(
3137
Id: url.Id,
3238
UserId: url.UserId,
3339
OriginalUrl: url.OriginalUrl,
@@ -39,7 +45,23 @@ public async Task<Result<ShortenedUrlDto>> Handle(
3945
UpdatedOnUtc: url.UpdatedOnUtc,
4046
RowStatus: url.RowStatus
4147
);
48+
await SetCache(response, cancellationToken);
49+
50+
return response;
51+
}
52+
53+
private async Task SetCache(
54+
ShortenedUrlDto cacheItem,
55+
CancellationToken cancellationToken)
56+
{
57+
var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{cacheItem.Code}";
4258

59+
await _cachingServices
60+
.SetAsync(
61+
cacheKey,
62+
cacheItem,
63+
cancellationToken: cancellationToken,
64+
slidingExpiration: TimeSpan.FromDays(1));
4365
}
4466
}
4567
}

Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public async Task<Result<PagedList<ShortenedUrlDto>>> Handle(GetShortenedUrlsQue
3131
{
3232
return Result
3333
.Failure<PagedList<ShortenedUrlDto>>(
34-
Error.NotFound(
35-
"ShortenedUrl.NotFound",
34+
Error.NoContent(
35+
"ShortenedUrl.NotContent",
3636
"No Shortened Url is available for this User."));
3737
}
3838

Shortify.NET.Common/FunctionalTypes/Error.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public record Error(string Code, string Message, ErrorType Type)
2727
public static Error Unauthorized(string code, string message) => new(code, message, ErrorType.Unauthorized);
2828
public static Error Forbidden(string code, string message) => new(code, message, ErrorType.Forbidden);
2929
public static Error Gone(string code, string message) => new(code, message, ErrorType.Gone);
30+
public static Error NoContent(string code, string message) => new(code, message, ErrorType.NoContent);
31+
public static Error BadRequest(string code, string message) => new(code, message, ErrorType.BadRequest);
3032
}
3133

3234
/// <summary>
@@ -42,6 +44,8 @@ public enum ErrorType
4244
Unauthorized,
4345
Forbidden,
4446
Gone,
47+
NoContent,
48+
BadRequest,
4549
None
4650
}
4751
}

Shortify.NET.Core/Errors/DomainErrors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public readonly struct User
5656
/// </summary>
5757
public readonly struct UserCredentials
5858
{
59-
public static readonly Error WrongCredentials = Error.Unauthorized("User.WrongCredentials", "The specified credentials are wrong.");
59+
public static readonly Error WrongCredentials = Error.BadRequest("User.WrongCredentials", "The specified credentials are wrong.");
6060
}
6161

6262
/// <summary>

0 commit comments

Comments
 (0)