public async Task <CreateNewShortUrlCommandResponse> Handle(CreateNewShortUrlCommand request, CancellationToken cancellationToken) { var slug = request.ShortUrl.Slug; if (!string.IsNullOrEmpty(slug)) { var url = await _repository.Get(slug); if (url != null) { return(new CreateNewShortUrlCommandResponse() { IsConflict = true, ConflictMessage = $"Slug '{url.Slug}' is in use" }); } } if (string.IsNullOrEmpty(slug)) { var xeger = new Xeger("[a-z0-9]{5}", new Random()); slug = xeger.Generate(); } request.ShortUrl.Slug = slug.ToLower(); var res = await _repository.Create(request.ShortUrl); return(new CreateNewShortUrlCommandResponse() { ShortUrl = res }); }
/// <summary> /// 删除短网址 /// </summary> /// <param name="alias">短网址别名</param> public void Delete(string alias) { if (string.IsNullOrEmpty(alias)) { return; } ShortUrlEntity entity = shortUrlRepository.Get(alias); if (entity != null) { EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Delete())); shortUrlRepository.Delete(entity); EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Delete())); } }
public async Task <ApplicationResponse <List <ShortUrlResponseDto> > > List() { var currentUser = await GetCurrentUser(); var shortUrlList = _shortUrlRepository .Get(x => x.User.Company.Id == currentUser.Company.Id) .OrderByDescending(o => o.CreatedAt) .ToList(); var responseList = shortUrlList.Select(UrlShortMapper.Map).ToList(); return(new ApplicationResponse <List <ShortUrlResponseDto> >(responseList)); }
public async Task <string> Handle(GetUrlBySlugQuery request, CancellationToken cancellationToken) { var shortUrl = await _repository.Get(request.Slug); return(shortUrl == null ? $"https://dlg-sh.herokuapp.com/err?slug={request.Slug}" : shortUrl.Url); }