Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Shortify.NET.API/BaseApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,10 +14,12 @@ namespace Shortify.NET.Application.Url.Commands.DeleteUrl;
/// </summary>
internal sealed class DeleteShortenedUrlByIdCommandHandler(
IShortenedUrlRepository shortenedUrlRepository,
ICachingServices cachingServices,
IUnitOfWork unitOfWork)
: ICommandHandler<DeleteShortenedUrlByIdCommand>
{
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;
private readonly ICachingServices _cachingServices = cachingServices;
private readonly IUnitOfWork _unitOfWork = unitOfWork;

/// <summary>
Expand All @@ -29,8 +34,15 @@ public async Task<Result> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,10 +11,14 @@ namespace Shortify.NET.Application.Url.Commands.UpdateUrl
{
internal sealed class UpdateShortenedUrlCommandHandler(
IShortenedUrlRepository shortenedUrlRepository,
ICachingServices cachingServices,
IUnitOfWork unitOfWork)
: ICommandHandler<UpdateShortenedUrlCommand, ShortenedUrlDto>
{
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;

private readonly ICachingServices _cachingServices = cachingServices;

private readonly IUnitOfWork _unitOfWork = unitOfWork;

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

return new ShortenedUrlDto(
var response = new ShortenedUrlDto(
Id: url.Id,
UserId: url.UserId,
OriginalUrl: url.OriginalUrl,
Expand All @@ -39,7 +45,23 @@ public async Task<Result<ShortenedUrlDto>> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public async Task<Result<PagedList<ShortenedUrlDto>>> Handle(GetShortenedUrlsQue
{
return Result
.Failure<PagedList<ShortenedUrlDto>>(
Error.NotFound(
"ShortenedUrl.NotFound",
Error.NoContent(
"ShortenedUrl.NotContent",
"No Shortened Url is available for this User."));
}

Expand Down
4 changes: 4 additions & 0 deletions Shortify.NET.Common/FunctionalTypes/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand All @@ -42,6 +44,8 @@ public enum ErrorType
Unauthorized,
Forbidden,
Gone,
NoContent,
BadRequest,
None
}
}
2 changes: 1 addition & 1 deletion Shortify.NET.Core/Errors/DomainErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public readonly struct User
/// </summary>
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.");
}

/// <summary>
Expand Down
Loading