Skip to content

Commit

Permalink
feat: shortening and recovering urls working fine.
Browse files Browse the repository at this point in the history
  • Loading branch information
armentanoc committed Mar 3, 2024
1 parent fc6af78 commit 89a06e6
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/URLShortener.Application/Interfaces/IUrlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public interface IUrlService
{
Task<Url> GetOriginalUrlAsync(string shortenedUrl);
Task<Url> ShortenUrlAsync(string originalUrl);
string GetShortenedUrlDomain();
Task<IEnumerable<Url>> GetAllAsync();
}
}
8 changes: 7 additions & 1 deletion src/URLShortener.Application/Services/UrlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public async Task<Url> GetOriginalUrlAsync(string shortenedUrl)
public async Task<Url> ShortenUrlAsync(string originalUrl)
{
string shortenedUrl = await GenerateShortenedUrl();
string slug = shortenedUrl.Split("/").Last();
DateTime expirationDate = GenerateRandomDuration();
Url newUrl = new Url(originalUrl, shortenedUrl, expirationDate);
Url newUrl = new Url(originalUrl, shortenedUrl, expirationDate, slug);
await _repository.AddAsync(newUrl);
return newUrl;
}
Expand Down Expand Up @@ -71,5 +72,10 @@ public bool UrlIsExpired(Url retrievedUrl)
return false;
throw new Exception("This URL has expired.");
}

public async Task<IEnumerable<Url>> GetAllAsync()
{
return await _repository.GetAllAsync();
}
}
}
4 changes: 3 additions & 1 deletion src/URLShortener.Domain/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ public class Url : BaseEntity
{
public string OriginalUrl { get; private set; }
public string ShortenedUrl { get; private set; }
public string Slug { get; private set; }
public DateTime ExpirationDate { get; private set; }

public Url()

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'OriginalUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ShortenedUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Slug' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'OriginalUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ShortenedUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Slug' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
// Required by EF
}

public Url(string originalUrl, string shortenedUrl, DateTime expirationDate)
public Url(string originalUrl, string shortenedUrl, DateTime expirationDate, string slug)
{
OriginalUrl = originalUrl;
ShortenedUrl = shortenedUrl;
ExpirationDate = expirationDate;
Slug = slug;
}

public Url(DateTime expirationDate)

Check warning on line 24 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'OriginalUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 24 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ShortenedUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 24 in src/URLShortener.Domain/Url.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Slug' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
.Annotation("Sqlite:Autoincrement", true),
OriginalUrl = table.Column<string>(type: "TEXT", nullable: false),
ShortenedUrl = table.Column<string>(type: "TEXT", nullable: false),
Slug = table.Column<string>(type: "TEXT", nullable: false),
ExpirationDate = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("Slug")
.IsRequired()
.HasColumnType("TEXT");

b.HasKey("Id");

b.ToTable("Url");
Expand Down
6 changes: 3 additions & 3 deletions src/URLShortener.Tests/Application/UrlServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public async Task GetOriginalUrlAsync_ShouldRetrieveOriginalUrl()
var urlService = new UrlService(urlRepositoryMock.Object, configurationMock.Object);

// Mock the behavior of GetByUrlAsync
urlRepositoryMock.Setup(repo => repo.GetByUrlAsync("short"))
.ReturnsAsync(new Url("https://exemplo.com", "short", DateTime.Now.AddDays(1)));
urlRepositoryMock.Setup(repo => repo.GetByUrlAsync("url/short"))
.ReturnsAsync(new Url("https://exemplo.com", "url/short", DateTime.Now.AddDays(1), "short"));

// Act
var response = await urlService.GetOriginalUrlAsync("short");
var response = await urlService.GetOriginalUrlAsync("url/short");
var originalUrl = response.OriginalUrl;

// Assert
Expand Down
26 changes: 19 additions & 7 deletions src/URLShortener.WebAPI/Controllers/UrlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using URLShortener.Application.Interfaces;
using URLShortener.Domain;
using URLShortener.ViewModels;
using URLShortener.WebAPI.Middlewares;

namespace URLShortener.WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
[Route("")]
public class UrlController : ControllerBase
{
private readonly ILogger<UrlController> _logger;
Expand All @@ -22,19 +21,32 @@ public UrlController(ILogger<UrlController> logger, IUrlService urlService, ICon
}

[HttpGet]
[Route("{shortenedUrl}")]
public async Task<IActionResult> Get(string shortenedUrl)
[Route("all")]
public async Task<IActionResult> GetAll()
{
var urls = await _urlService.GetAllAsync();

if (urls is not null && urls.Any())
return Ok(urls);

return NotFound();
}

[HttpGet]
[Route("{slug}")]
public async Task<IActionResult> Get([FromRoute] string slug)
{
string shortenedUrl = $"{_urlService.GetShortenedUrlDomain()}/{slug}";
Url originalUrl = await _urlService.GetOriginalUrlAsync(shortenedUrl);

if (originalUrl is Url url)
return Ok(url);
return Ok(url.OriginalUrl);

return NotFound();
}


[HttpPost]
[Route("makeUrlShort")]
public async Task<IActionResult> Add([FromBody] UrlRequest url)
{
if (url == null || string.IsNullOrEmpty(url.OriginalUrl))
Expand All @@ -45,7 +57,7 @@ public async Task<IActionResult> Add([FromBody] UrlRequest url)
if (string.IsNullOrEmpty(shortenedUrl.ShortenedUrl))
return BadRequest("Failed to create shortened URL");

return CreatedAtAction(nameof(Get), new { shortenedUrl }, shortenedUrl);
return CreatedAtAction(nameof(Get), new { slug = shortenedUrl.ShortenedUrl }, shortenedUrl);
}
}
}
Binary file modified src/URLShortener.WebAPI/URLShortenerDB.db
Binary file not shown.
Binary file removed src/URLShortener.WebAPI/URLShortenerDB.db-shm
Binary file not shown.
Binary file removed src/URLShortener.WebAPI/URLShortenerDB.db-wal
Binary file not shown.
2 changes: 1 addition & 1 deletion src/URLShortener.WebAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"URLShortenerSqlite": "Data Source=URLShortenerDB.db"
},
"AppSettings": {
"ShortenedUrlDomain": "https://localhost:5500"
"ShortenedUrlDomain": "http://localhost:5500"
},
"AllowedHosts": "*"
}

0 comments on commit 89a06e6

Please sign in to comment.