public static Shortcut CreateShortcut(ref IMapper mapper, ShortcutWriteDto writeDto) { Shortcut shortcut = mapper.Map <Shortcut>(writeDto); CreateShortendUrl(ref writeDto, ref shortcut); return(shortcut); }
private static void CreateShortendUrl(ref ShortcutWriteDto writeDto, ref Shortcut shortcut) { // Custom URL if (!string.IsNullOrEmpty(writeDto.DesiredUrl)) { shortcut.CustomUrl = true; shortcut.ShortendUrl = writeDto.DesiredUrl; } else { shortcut.ShortendUrl = Utilities.UrlShortner.Make(shortcut.OriginalUrl); } }
public async Task <ShortcutReadDto> Shorten(ShortcutWriteDto writeDto) { if (!string.IsNullOrEmpty(writeDto.DesiredUrl)) { if (await _repo.HasShortendUrl(writeDto.DesiredUrl)) { return(null); } } var shortcut = await _repo.Get(writeDto.OriginalUrl).ConfigureAwait(false); if (shortcut != null) { return(_mapper.Map <ShortcutReadDto>(shortcut)); } shortcut = ShortcutFactory.CreateShortcut(ref _mapper, writeDto); await _repo.Add(shortcut).ConfigureAwait(false); return(_mapper.Map <ShortcutReadDto>(shortcut)); }
public async Task <IActionResult> Add([FromBody] ShortcutWriteDto writeDto) { var dto = await _service.Shorten(writeDto); return(dto == null?BadRequest() : Ok(dto) as IActionResult); }