Добавил patch-метод для обновления short url

This commit is contained in:
2026-05-14 12:56:25 +03:00
parent aa910cc8a9
commit eedbe9aaae
2 changed files with 38 additions and 0 deletions
@@ -54,6 +54,27 @@ namespace URL_Shortener.Controllers
return CreatedAtRoute(nameof(GetShortUrlByIdAsync), new { id = shortUrl.Id }, shortUrl); return CreatedAtRoute(nameof(GetShortUrlByIdAsync), new { id = shortUrl.Id }, shortUrl);
} }
[HttpPatch("{id:int}")]
public async Task<IActionResult> 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}")] [HttpDelete("{id:int}")]
public async Task<IActionResult> DeleteShortUrlByIdAsync(int id) public async Task<IActionResult> DeleteShortUrlByIdAsync(int id)
{ {
+17
View File
@@ -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;
}
}