mirror of
https://github.com/ryzij/URL-Shortener.git
synced 2026-07-14 03:56:58 +00:00
Добавил patch-метод для обновления short url
This commit is contained in:
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user