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 @@ -12,7 +12,7 @@
public abstract class BaseApiController(IApiService apiService)
: ControllerBase
{
protected readonly IApiService _apiService = apiService;

Check warning on line 15 in Shortify.NET.API/BaseApiController.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Inconsistent Naming

Name '_apiService' does not match rule 'Instance fields (not private)'. Suggested name is 'ApiService'.

Check warning on line 15 in Shortify.NET.API/BaseApiController.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Inconsistent Naming

Name '_apiService' does not match rule 'Instance fields (not private)'. Suggested name is 'ApiService'.

/// <summary>
/// Handles Failure Scenarios to Generate
Expand Down Expand Up @@ -49,9 +49,11 @@
{
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 @@
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,11 +14,13 @@
/// </summary>
internal sealed class DeleteShortenedUrlByIdCommandHandler(
IShortenedUrlRepository shortenedUrlRepository,
ICachingServices cachingServices,
IUnitOfWork unitOfWork)
: ICommandHandler<DeleteShortenedUrlByIdCommand>
{
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;

Check notice on line 21 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 21 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter
private readonly ICachingServices _cachingServices = cachingServices;

Check notice on line 22 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 22 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter
private readonly IUnitOfWork _unitOfWork = unitOfWork;

Check notice on line 23 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 23 in Shortify.NET.Application/Url/Commands/DeleteUrl/DeleteShortenedUrlByIdCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

/// <summary>
/// Handles the command to delete a shortened URL by its identifier.
Expand All @@ -29,8 +34,15 @@
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,11 +11,15 @@
{
internal sealed class UpdateShortenedUrlCommandHandler(
IShortenedUrlRepository shortenedUrlRepository,
ICachingServices cachingServices,
IUnitOfWork unitOfWork)
: ICommandHandler<UpdateShortenedUrlCommand, ShortenedUrlDto>
{
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;

Check notice on line 18 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 18 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

private readonly ICachingServices _cachingServices = cachingServices;

Check notice on line 20 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 20 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

private readonly IUnitOfWork _unitOfWork = unitOfWork;

Check notice on line 22 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 22 in Shortify.NET.Application/Url/Commands/UpdateUrl/UpdateShortenedUrlCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

public async Task<Result<ShortenedUrlDto>> Handle(
UpdateShortenedUrlCommand command,
Expand All @@ -27,7 +33,7 @@
_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 @@
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 @@ -9,7 +9,7 @@
internal sealed class GetShortenedUrlsQueryHandler(IShortenedUrlRepository shortenedUrlRepository)
: IQueryHandler<GetShortenedUrlsQuery, PagedList<ShortenedUrlDto>>
{
private readonly IShortenedUrlRepository _shortenedUrlRepository = shortenedUrlRepository;

Check notice on line 12 in Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

Check notice on line 12 in Shortify.NET.Application/Url/Queries/GetAllShortenedUrls/GetShortenedUrlsQueryHandler.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Replace with primary constructor parameter

Replace with primary constructor parameter

public async Task<Result<PagedList<ShortenedUrlDto>>> Handle(GetShortenedUrlsQuery query, CancellationToken cancellationToken)
{
Expand All @@ -31,8 +31,8 @@
{
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 @@ -20,13 +20,15 @@
public static readonly Error UnauthorizedRequest = new("Error.Unauthorized", "An Unauthorized Error occurred.", ErrorType.Unauthorized);

public static Error Failure(string code, string message) => new(code, message, ErrorType.Failure);
public static Error Unexpected(string code, string message) => new(code, message, ErrorType.Unexpected);

Check notice on line 23 in Shortify.NET.Common/FunctionalTypes/Error.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Type member is never used: Non-private accessibility

Method 'Unexpected' is never used

Check notice on line 23 in Shortify.NET.Common/FunctionalTypes/Error.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Type member is never used: Non-private accessibility

Method 'Unexpected' is never used
public static Error Validation(string code, string message) => new(code, message, ErrorType.Validation);
public static Error Conflict(string code, string message) => new(code, message, ErrorType.Conflict);
public static Error NotFound(string code, string message) => new(code, message, ErrorType.NotFound);
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);

Check notice on line 28 in Shortify.NET.Common/FunctionalTypes/Error.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Type member is never used: Non-private accessibility

Method 'Forbidden' is never used

Check notice on line 28 in Shortify.NET.Common/FunctionalTypes/Error.cs

View workflow job for this annotation

GitHub Actions / Qodana Community for .NET

Type member is never used: Non-private accessibility

Method 'Forbidden' is never used
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 @@
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