private async Task <string> CreateShortUrl(ShortUrlCreateRequest shortUrlCreateRequest, string key)
        {
            var shortUrlModel = new ShortUrlModel
            {
                Key        = key,
                CreatedUtc = DateTime.UtcNow,
                CreatorId  = shortUrlCreateRequest.UserId,
                ExpiresUtc = DateTime.UtcNow.AddDays(ApplicationVariable.ShortUrlExpireDays),
                UpdateDate = DateTime.UtcNow,
                Url        = shortUrlCreateRequest.LongUrl
            };
            await _shortUrlRepository.Insert(shortUrlModel);

            return(shortUrlModel.Key);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建短网址
        /// </summary>
        /// <param name="url">需要处理的Url</param>
        /// <returns>Url别名</returns>
        public string Create(string url)
        {
            bool urlExists;

            IUrlShortner     urlShortner = null;
            ShortUrlSettings settings    = shortUrlSettingsManager.Get();

            if (settings.IsEnableOtherShortner)
            {
                urlShortner = DIContainer.Resolve <IUrlShortner>();
            }

            ShortUrlEntity entity = ShortUrlEntity.New(GetUrlAlias(url, out urlExists));

            entity.Url = url;

            //判断Url是否存在
            if (urlExists)
            {
                return(entity.Alias);
            }

            string primaryKey = string.Empty;

            EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Create()));

            if (settings.IsEnableOtherShortner && urlShortner != null)
            {
                entity.OtherShortUrl = urlShortner.Shortner(url);
            }

            primaryKey = shortUrlRepository.Insert(entity).ToString();

            EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Create()));

            return(primaryKey);
        }