diff --git a/Shortify.NET.API/BaseApiController.cs b/Shortify.NET.API/BaseApiController.cs index 947f462..092f04c 100644 --- a/Shortify.NET.API/BaseApiController.cs +++ b/Shortify.NET.API/BaseApiController.cs @@ -49,9 +49,11 @@ protected IActionResult HandleFailure(Result result, bool isRedirectToErrorPage { ErrorType.NotFound => StatusCodes.Status404NotFound, ErrorType.Validation => StatusCodes.Status400BadRequest, + ErrorType.BadRequest => StatusCodes.Status400BadRequest, ErrorType.Conflict => StatusCodes.Status409Conflict, ErrorType.Unauthorized => StatusCodes.Status401Unauthorized, ErrorType.Gone => StatusCodes.Status410Gone, + ErrorType.NoContent => StatusCodes.Status204NoContent, _ => StatusCodes.Status500InternalServerError }; @@ -60,6 +62,11 @@ protected IActionResult HandleFailure(Result result, bool isRedirectToErrorPage return Redirect($"/error/{statusCode}"); } + if (statusCode == StatusCodes.Status204NoContent) + { + return NoContent(); + } + return Problem( statusCode: statusCode, type: Enum.GetName(typeof(ErrorType), error.Type), diff --git a/Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs b/Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs index 6467a25..1425a38 100644 --- a/Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs +++ b/Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs @@ -1,7 +1,10 @@ -using Shortify.NET.Application.Abstractions.Repositories; +using Shortify.NET.Application.Abstractions; +using Shortify.NET.Application.Abstractions.Repositories; +using Shortify.NET.Application.Shared; using Shortify.NET.Common.FunctionalTypes; using Shortify.NET.Common.Messaging.Abstractions; using Shortify.NET.Core; +using Shortify.NET.Core.Entites; using Shortify.NET.Core.Errors; namespace Shortify.NET.Application.Url.Commands.DeleteUrl; @@ -11,10 +14,12 @@ namespace Shortify.NET.Application.Url.Commands.DeleteUrl; /// internal sealed class DeleteShortenedUrlByIdCommandHandler( IShortenedUrlRepository shortenedUrlRepository, + ICachingServices cachingServices, IUnitOfWork unitOfWork) : ICommandHandler { private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository; + private readonly ICachingServices _cachingServices = cachingServices; private readonly IUnitOfWork _unitOfWork = unitOfWork; /// @@ -29,8 +34,15 @@ public async Task Handle(DeleteShortenedUrlByIdCommand command, Cancella if (url is null) return Result.Failure(DomainErrors.ShortenedUrl.ShortenedUrlNotFound); _shortenedUrlRepository.Delete(url); + await RemoveFromCacheAsync(url, cancellationToken); await _unitOfWork.SaveChangesAsync(cancellationToken); return Result.Success(); } + + private async Task RemoveFromCacheAsync(ShortenedUrl shortenedUrl, CancellationToken cancellationToken) + { + var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{shortenedUrl.Code}"; + await _cachingServices.RemoveAsync(cacheKey, cancellationToken); + } } \ No newline at end of file diff --git a/Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs b/Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs index d1a4e5b..d756112 100644 --- a/Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs +++ b/Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs @@ -1,4 +1,6 @@ -using Shortify.NET.Application.Abstractions.Repositories; +using Shortify.NET.Application.Abstractions; +using Shortify.NET.Application.Abstractions.Repositories; +using Shortify.NET.Application.Shared; using Shortify.NET.Application.Shared.Models; using Shortify.NET.Common.FunctionalTypes; using Shortify.NET.Common.Messaging.Abstractions; @@ -9,10 +11,14 @@ namespace Shortify.NET.Application.Url.Commands.UpdateUrl { internal sealed class UpdateShortenedUrlCommandHandler( IShortenedUrlRepository shortenedUrlRepository, + ICachingServices cachingServices, IUnitOfWork unitOfWork) : ICommandHandler { private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository; + + private readonly ICachingServices _cachingServices = cachingServices; + private readonly IUnitOfWork _unitOfWork = unitOfWork; public async Task> Handle( @@ -27,7 +33,7 @@ public async Task> Handle( _shortenedUrlRepository.Update(url); await _unitOfWork.SaveChangesAsync(cancellationToken); - return new ShortenedUrlDto( + var response = new ShortenedUrlDto( Id: url.Id, UserId: url.UserId, OriginalUrl: url.OriginalUrl, @@ -39,7 +45,23 @@ public async Task> Handle( UpdatedOnUtc: url.UpdatedOnUtc, RowStatus: url.RowStatus ); + await SetCache(response, cancellationToken); + + return response; + } + + private async Task SetCache( + ShortenedUrlDto cacheItem, + CancellationToken cancellationToken) + { + var cacheKey = $"{Constant.Cache.Prefixes.OriginalUrls}{cacheItem.Code}"; + await _cachingServices + .SetAsync( + cacheKey, + cacheItem, + cancellationToken: cancellationToken, + slidingExpiration: TimeSpan.FromDays(1)); } } } \ No newline at end of file diff --git a/Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs b/Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs index bc1bec0..3c61b3e 100644 --- a/Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs +++ b/Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs @@ -31,8 +31,8 @@ public async Task>> Handle(GetShortenedUrlsQue { return Result .Failure>( - Error.NotFound( - "ShortenedUrl.NotFound", + Error.NoContent( + "ShortenedUrl.NotContent", "No Shortened Url is available for this User.")); } diff --git a/Shortify.NET.Common/FunctionalTypes/Error.cs b/Shortify.NET.Common/FunctionalTypes/Error.cs index b46a8de..4c16e8a 100644 --- a/Shortify.NET.Common/FunctionalTypes/Error.cs +++ b/Shortify.NET.Common/FunctionalTypes/Error.cs @@ -27,6 +27,8 @@ public record Error(string Code, string Message, ErrorType Type) public static Error Unauthorized(string code, string message) => new(code, message, ErrorType.Unauthorized); public static Error Forbidden(string code, string message) => new(code, message, ErrorType.Forbidden); public static Error Gone(string code, string message) => new(code, message, ErrorType.Gone); + public static Error NoContent(string code, string message) => new(code, message, ErrorType.NoContent); + public static Error BadRequest(string code, string message) => new(code, message, ErrorType.BadRequest); } /// @@ -42,6 +44,8 @@ public enum ErrorType Unauthorized, Forbidden, Gone, + NoContent, + BadRequest, None } } diff --git a/Shortify.NET.Core/Errors/DomainErrors.cs b/Shortify.NET.Core/Errors/DomainErrors.cs index 837a11c..093d9b3 100644 --- a/Shortify.NET.Core/Errors/DomainErrors.cs +++ b/Shortify.NET.Core/Errors/DomainErrors.cs @@ -56,7 +56,7 @@ public readonly struct User /// public readonly struct UserCredentials { - public static readonly Error WrongCredentials = Error.Unauthorized("User.WrongCredentials", "The specified credentials are wrong."); + public static readonly Error WrongCredentials = Error.BadRequest("User.WrongCredentials", "The specified credentials are wrong."); } ///