示例#1
0
        public override async Task <ShortUrlDto> Handle(CreateShortUrlCommand request, CancellationToken cancellationToken = default)
        {
            ValidateRequest(request);

            if (request.IsRouter)
            {
                CurrentUser = AnonymousUser.Current();

                // Only check url dublication for anonymous user
                var existUrl = await _shortUrlRepository.GetUserShortUrlByLongUrl(request.LongUrl, CurrentUser.UserId, cancellationToken);

                if (Check.IsNotNull(existUrl))
                {
                    return(ShortUrlDto.MapFromEntity(existUrl, _globalSettings.RouterDomain));
                }
            }

            //Generate shortUrl
            var shortUrl = new ShortUrl(request.LongUrl)
            {
                CreatedBy = CurrentUser.UserId,
                UpdatedBy = CurrentUser.UserId
            };

            var slug = await CreateSlug(request.SlugKey);

            shortUrl.SetSlug(slug);

            //Save to database
            await _shortUrlRepository.InsertOrUpdate(shortUrl, cancellationToken);

            //Created Event
            _mediator.Publish(new ShortUrlCreated {
                ShortUrl = shortUrl
            }, cancellationToken).Forget();

            return(ShortUrlDto.MapFromEntity(shortUrl, _globalSettings.RouterDomain));;
        }