diff --git a/URL-Shortener/Controllers/ShortUrlController.cs b/URL-Shortener/Controllers/ShortUrlController.cs index 36c6853..ad58ccc 100644 --- a/URL-Shortener/Controllers/ShortUrlController.cs +++ b/URL-Shortener/Controllers/ShortUrlController.cs @@ -54,6 +54,27 @@ namespace URL_Shortener.Controllers return CreatedAtRoute(nameof(GetShortUrlByIdAsync), new { id = shortUrl.Id }, shortUrl); } + [HttpPatch("{id:int}")] + public async Task UpdateShortUrlAsync(int id, UpdateShortUrlDto dto) + { + var shortUrl = await _db.ShortUrls.FindAsync(id); + if (shortUrl == null) + return NotFound("Short URL not found"); + + if (!string.IsNullOrEmpty(dto.OriginalUrl)) + shortUrl.OriginalUrl = dto.OriginalUrl; + if (dto.ExpirationDateTime.HasValue) + shortUrl.ExpirationDateTime = dto.ExpirationDateTime; + if (dto.ClickLimit.HasValue) + shortUrl.ClickLimit = dto.ClickLimit.Value; + if (dto.ResetExpirationDateTime) + shortUrl.ExpirationDateTime = null; + + await _db.SaveChangesAsync(); + + return Ok(shortUrl); + } + [HttpDelete("{id:int}")] public async Task DeleteShortUrlByIdAsync(int id) { diff --git a/URL-Shortener/DTO/UpdateShortUrlDto.cs b/URL-Shortener/DTO/UpdateShortUrlDto.cs new file mode 100644 index 0000000..e5322a2 --- /dev/null +++ b/URL-Shortener/DTO/UpdateShortUrlDto.cs @@ -0,0 +1,17 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace URL_Shortener.DTO +{ + public class UpdateShortUrlDto + { + public string? OriginalUrl { get; set; } + + public DateTime? ExpirationDateTime { get; set; } = null; + + public int? ClickLimit { get; set; } = null; + + [DefaultValue(false)] + public bool ResetExpirationDateTime { get; set; } = false; + } +}