From afdea4e9c957baef297b0440eb918d26afad6849 Mon Sep 17 00:00:00 2001 From: Piotr Kowalski Date: Sun, 4 Jun 2023 10:41:22 +0200 Subject: [PATCH] Use dedicated edit dto --- .../Controllers/V1/LinksController.cs | 9 ++-- .../DataTransferObjects/LinkMappingProfile.cs | 1 + .../DataTransferObjects/V1/LinkEditDto.cs | 41 +++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/V1/LinkEditDto.cs diff --git a/Rinkudesu.Services.Links/Rinkudesu.Services.Links/Controllers/V1/LinksController.cs b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/Controllers/V1/LinksController.cs index 3ad0744..e2fd17d 100644 --- a/Rinkudesu.Services.Links/Rinkudesu.Services.Links/Controllers/V1/LinksController.cs +++ b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/Controllers/V1/LinksController.cs @@ -161,17 +161,16 @@ public async Task> Create([FromBody] LinkCreateDto newLink [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status200OK)] - public async Task Update(Guid linkId, [FromBody] LinkDto updatedLink) + public async Task Update(Guid linkId, [FromBody] LinkEditDto updatedLink) { + if (!ModelState.IsValid) + return BadRequest(); + using var scope = _logger.BeginScope("Updating a link with id {linkId} with {newLink}", linkId, updatedLink); var link = _mapper.Map(updatedLink); link.Id = linkId; link.CreatingUserId = User.GetIdAsGuid(); - if (!TryValidateModel(link)) - { - return BadRequest(); - } try { await _repository.UpdateLinkAsync(link, User.GetIdAsGuid()).ConfigureAwait(false); diff --git a/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/LinkMappingProfile.cs b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/LinkMappingProfile.cs index 63ba870..983efaa 100644 --- a/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/LinkMappingProfile.cs +++ b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/LinkMappingProfile.cs @@ -13,6 +13,7 @@ public LinkMappingProfile() { CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/V1/LinkEditDto.cs b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/V1/LinkEditDto.cs new file mode 100644 index 0000000..4db2f51 --- /dev/null +++ b/Rinkudesu.Services.Links/Rinkudesu.Services.Links/DataTransferObjects/V1/LinkEditDto.cs @@ -0,0 +1,41 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using Rinkudesu.Services.Links.Models; + +namespace Rinkudesu.Services.Links.DataTransferObjects.V1 +{ + /// + /// Data transfer object to send and receive objects + /// + [ExcludeFromCodeCoverage] + public class LinkEditDto + { + /// + /// URL the link is pointing to + /// + [DataType(DataType.Url)] + [MaxLength(200)] + [SuppressMessage("Design", "CA1056:URI-like properties should not be strings")] + public string LinkUrl { get; set; } = null!; + /// + /// Title of the link + /// + [MaxLength(250)] + public string Title { get; set; } = null!; + /// + /// Description of the link + /// + [MaxLength(1000)] + public string? Description { get; set; } + /// + /// Privacy configuration of the link + /// + public Link.LinkPrivacyOptions PrivacyOptions { get; set; } + + /// + /// Verifies whether is a valid absolute url. + /// + public bool IsLinkUrlValid() => Uri.TryCreate(LinkUrl, UriKind.Absolute, out _); + } +}