Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #253 from rinkudesu/link-edit-view-model
Browse files Browse the repository at this point in the history
Use dedicated edit dto
  • Loading branch information
KowalskiPiotr98 authored Jun 4, 2023
2 parents defb2d1 + afdea4e commit eec6679
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,16 @@ public async Task<ActionResult<LinkDto>> Create([FromBody] LinkCreateDto newLink
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult> Update(Guid linkId, [FromBody] LinkDto updatedLink)
public async Task<ActionResult> 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<Link>(updatedLink);
link.Id = linkId;
link.CreatingUserId = User.GetIdAsGuid();
if (!TryValidateModel(link))
{
return BadRequest();
}
try
{
await _repository.UpdateLinkAsync(link, User.GetIdAsGuid()).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public LinkMappingProfile()
{
CreateMap<Link, LinkDto>();
CreateMap<LinkCreateDto, Link>();
CreateMap<LinkEditDto, Link>();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Data transfer object to send and receive <see cref="Link"/> objects
/// </summary>
[ExcludeFromCodeCoverage]
public class LinkEditDto
{
/// <summary>
/// URL the link is pointing to
/// </summary>
[DataType(DataType.Url)]
[MaxLength(200)]
[SuppressMessage("Design", "CA1056:URI-like properties should not be strings")]
public string LinkUrl { get; set; } = null!;
/// <summary>
/// Title of the link
/// </summary>
[MaxLength(250)]
public string Title { get; set; } = null!;
/// <summary>
/// Description of the link
/// </summary>
[MaxLength(1000)]
public string? Description { get; set; }
/// <summary>
/// Privacy configuration of the link
/// </summary>
public Link.LinkPrivacyOptions PrivacyOptions { get; set; }

/// <summary>
/// Verifies whether <see cref="LinkUrl"/> is a valid absolute url.
/// </summary>
public bool IsLinkUrlValid() => Uri.TryCreate(LinkUrl, UriKind.Absolute, out _);
}
}

0 comments on commit eec6679

Please sign in to comment.