/// <summary>
        /// Создание сжатой ссылки
        /// </summary>
        /// <param name="userUrl"><seealso cref="UserUrl"/></param>
        /// <param name="userGuid">гуид пользователя</param>
        /// <returns></returns>
        public async Task <string> AddUserUrlAsync(UserUrl userUrl, Guid userGuid)
        {
            using (var context = new UrlShortenerContext())
            {
                if (userUrl == null || userGuid == Guid.Empty)
                {
                    throw new NullReferenceException();
                }

                //есть такой пользователь в Бд?
                var user = await context.Users.FirstOrDefaultAsync(x => x.UserGuid == userGuid);

                var userId = user?.Id;
                if (userId == null)
                {
                    //добавляем в БД пользователя
                    var result = context.Users.Add(new User()
                    {
                        UserGuid = userGuid
                    });
                    userId = result.Id;
                }

                userUrl.UserId = (long)userId;

                var newUrl = context.UserUrls.Add(userUrl);
                await context.SaveChangesAsync();

                return(newUrl.CompactUrl);
            }
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("Id,FullName")] Url url)
        {
            if (ModelState.IsValid)
            {
                url.FullName = UrlHelpers.AppendProtocol(url.FullName);
                var innerUrl = await _context.Urls.FirstOrDefaultAsync(dbUrl => dbUrl.FullName == url.FullName);

                if (innerUrl == null)
                {
                    url.ShortName = UrlHelpers.ShortenUrl(url.FullName);
                    _context.Add(url);
                    await _context.SaveChangesAsync();
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(url));
        }
Exemple #3
0
        public async Task <string> CreateShortenedUrl(string originalUrl)
        {
            if (!originalUrl.StartsWith("http://") && !originalUrl.StartsWith("https://"))
            {
                originalUrl = "https://" + originalUrl;
            }

            var newUrlInfo = new UrlInfo
            {
                Original = originalUrl
            };

            _urlShortenerContext.UrlInfos.Add(newUrlInfo);
            await _urlShortenerContext.SaveChangesAsync();

            var key = GenerateShortenedUrlKey(newUrlInfo.Id);

            newUrlInfo.Shortened = key.ToLower();
            await _urlShortenerContext.SaveChangesAsync();

            return(newUrlInfo.Shortened);
        }
        /// <summary>
        /// Инкремент кол-во переходов по ссылке
        /// </summary>
        /// <param name="compactUrl">ключ компактной ссылки</param>
        /// <returns></returns>
        public async Task IncrementNumberOfViewAsync(string compactUrl)
        {
            using (var context = new UrlShortenerContext())
            {
                var userUrl = await context.UserUrls.FirstOrDefaultAsync(x => x.CompactUrl == compactUrl);

                if (userUrl != null)
                {
                    userUrl.NumberOfViews += 1;
                    await context.SaveChangesAsync();
                }
            }
        }