-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ozkank
committed
Jul 2, 2024
1 parent
340801a
commit 1ec4402
Showing
15 changed files
with
315 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,79 @@ | ||
using Carter; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using UrlShortener.ApiService.Infrastructure.Database; | ||
|
||
namespace UrlShortener.ApiService.Features | ||
{ | ||
public class RedirectUrlQuery : IRequest<string> | ||
{ | ||
public string Code { get; set; } | ||
} | ||
|
||
internal sealed class RedirectUrlQueryHandler : | ||
IRequestHandler<RedirectUrlQuery, string> | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
public const int NumberOfCharsInShortLink = 7; | ||
private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
private readonly Random _random = new(); | ||
|
||
public RedirectUrlQueryHandler(ApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<string> Handle(RedirectUrlQuery request, CancellationToken cancellationToken) | ||
{ | ||
var shortenedUrl = await _context | ||
.ShortenedUrls | ||
.FirstOrDefaultAsync(s => s.Code == request.Code); | ||
|
||
if (shortenedUrl is null) | ||
{ | ||
//return Results.NotFound(); | ||
} | ||
|
||
return shortenedUrl.ShortUrl; | ||
} | ||
} | ||
|
||
|
||
public class RedirectUrlEndpoint : ICarterModule | ||
{ | ||
public void AddRoutes(IEndpointRouteBuilder app) | ||
{ | ||
app.MapGet("api/{code}", async (String code, ISender sender) => | ||
{ | ||
var query = new RedirectUrlQuery { Code = code }; | ||
var result = await sender.Send(query); | ||
|
||
return Results.Ok(result); | ||
}); | ||
} | ||
} | ||
} | ||
//using Azure.Core; | ||
//using Carter; | ||
//using FluentValidation; | ||
//using MediatR; | ||
//using Microsoft.EntityFrameworkCore; | ||
//using UrlShortener.ApiService.Infrastructure.Database; | ||
//using UrlShortener.ApiService.Shared; | ||
|
||
//namespace UrlShortener.ApiService.Features | ||
//{ | ||
// public class RedirectUrl | ||
// { | ||
// public class Query : IRequest<Result<RedirectUrlResponse>> | ||
// { | ||
// public string Code { get; set; } = string.Empty; | ||
// } | ||
|
||
// internal sealed class Handler : IRequestHandler<Query, Result<RedirectUrlResponse>> | ||
// { | ||
// private readonly ApplicationDbContext _context; | ||
// public const int NumberOfCharsInShortLink = 7; | ||
// private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
// private readonly Random _random = new(); | ||
|
||
// public Handler(ApplicationDbContext context) | ||
// { | ||
// _context = context; | ||
// } | ||
|
||
// public async Task<Result<RedirectUrlResponse>> Handle(Query request, CancellationToken cancellationToken) | ||
// { | ||
// var item = await _context | ||
// .ShortenedUrls | ||
// .Where(s => s.Code == request.Code) | ||
// .Select(x => new RedirectUrlResponse | ||
// { | ||
// LongUrl = x.LongUrl, | ||
// }) | ||
// .FirstOrDefaultAsync(cancellationToken); | ||
|
||
// if (item is null) | ||
// { | ||
// return Result.Failure<RedirectUrlResponse>(new Error( | ||
// "RedirectUrlResponse.Null", | ||
// "The longUrl with the specified shortUrl was not found")); | ||
// } | ||
|
||
// return item; | ||
// } | ||
// } | ||
|
||
// } | ||
|
||
// public class RedirectUrlEndpoint : ICarterModule | ||
// { | ||
// public void AddRoutes(IEndpointRouteBuilder app) | ||
// { | ||
// app.MapGet("api/redirect/{shortUrl}", async (string shortUrl, ISender sender) => | ||
// { | ||
// var query = new RedirectUrl.Query { Code = shortUrl }; | ||
|
||
// var result = await sender.Send(query); | ||
|
||
// if (result.IsFailure) | ||
// { | ||
// return Results.NotFound(result.Error); | ||
// } | ||
|
||
// return Results.Ok(result.Value); | ||
// }); | ||
// } | ||
// } | ||
|
||
// public class RedirectUrlResponse | ||
// { | ||
// public string LongUrl { get; set; } = string.Empty; | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace UrlShortener.ApiService.Shared | ||
{ | ||
public record Error(string Code, string Message) | ||
{ | ||
public static readonly Error None = new(string.Empty, string.Empty); | ||
|
||
public static readonly Error NullValue = new("Error.NullValue", "The specified result value is null."); | ||
|
||
public static readonly Error ConditionNotMet = new("Error.ConditionNotMet", "The specified condition was not met."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace UrlShortener.ApiService.Shared | ||
{ | ||
public class Result | ||
{ | ||
protected internal Result(bool isSuccess, Error error) | ||
{ | ||
if (isSuccess && error != Error.None) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
if (!isSuccess && error == Error.None) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
IsSuccess = isSuccess; | ||
Error = error; | ||
} | ||
|
||
public bool IsSuccess { get; } | ||
|
||
public bool IsFailure => !IsSuccess; | ||
|
||
public Error Error { get; } | ||
|
||
public static Result Success() => new(true, Error.None); | ||
|
||
public static Result<TValue> Success<TValue>(TValue value) => new(value, true, Error.None); | ||
|
||
public static Result Failure(Error error) => new(false, error); | ||
|
||
public static Result<TValue> Failure<TValue>(Error error) => new(default, false, error); | ||
|
||
public static Result Create(bool condition) => condition ? Success() : Failure(Error.ConditionNotMet); | ||
|
||
public static Result<TValue> Create<TValue>(TValue? value) => value is not null ? Success(value) : Failure<TValue>(Error.NullValue); | ||
} | ||
} |
Oops, something went wrong.