public int Save(ShortUrl shortUrl)
        {
            //insert
            shortUrl.CreateDate = DateTime.UtcNow;
            _context.ShortUrls.Add(shortUrl);
            _context.SaveChanges();

            //update path
            shortUrl.Path = ShortUrlHelper.Encode(shortUrl.Id);
            _context.ShortUrls.Update(shortUrl);
            _context.SaveChanges();

            return(shortUrl.Id);
        }
        public int Save(ShortUrl shortUrl)
        {
            _context.ShortUrls.Add(shortUrl);
            _context.SaveChanges();

            return(shortUrl.Id);
        }
Example #3
0
        public AjaxResponseData AddNew(string text)
        {
            var response = new AjaxResponseData();

            if (IsValidUrl(text))
            {
                Uri    requestUrl  = HttpContext.Current.Request.Url;
                string requesthost = string.Format("{0}{1}{2}{3}",
                                                   requestUrl.Scheme,
                                                   Uri.SchemeDelimiter,
                                                   requestUrl.Authority,
                                                   "/");
                var entities = new UrlShortenerContext();
                var data     = new ShortenedUrl();
                data.OriginalUrl = text;
                entities.ShortUrls.Add(data);
                entities.SaveChanges();
                response.Success = true;
                response.Data    = requesthost + data.Id;
            }
            else
            {
                response.Success = false;
                response.Data    = "Please enter valid URL";
            }
            return(response);
        }
Example #4
0
        public string Save(string originalUrl)
        {
            Url url = new Url()
            {
                OriginalUrl = originalUrl
            };

            _context.Urls.Add(url);
            _context.SaveChanges();

            return(ShortnerUrlHelper.Encode(url.Id));
        }
Example #5
0
        public async void GetLongShouldReturnSuccessIfExists()
        {
            var longUrl =
                "https://www.google.com/search?q=url+shortener&oq=google+u&aqs=chrome.0.69i59j69i60l3j0j69i57.1069j0j7&sourceid=chrome&ie=UTF-8";
            var shortUrl = "XJF4FR";

            //Arrange
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <UrlShortenerContext>()
                              .UseSqlite(connection)
                              .Options;

                using (var context = new UrlShortenerContext(options))
                {
                    context.Database.EnsureCreated();
                }

                using (var context = new UrlShortenerContext(options))
                {
                    context.Urls.Add(new Url()
                    {
                        created = DateTime.Now, long_url = longUrl, short_url = shortUrl, updated = DateTime.Now
                    });
                    context.SaveChanges();
                }

                using (var context = new UrlShortenerContext(options))
                {
                    //Act
                    ShortenerController controller = new ShortenerController(context);
                    string result = (await controller.GetLong(shortUrl)).Value;

                    //Assert
                    Assert.Equal(longUrl, result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #6
0
        public async Task <ActionResult <string> > GetShort(string long_url)
        {
            var shortUrl = RandomString(6);
            Url url      = new Url()
            {
                long_url = long_url, short_url = shortUrl, created = DateTime.Now, updated = DateTime.Now
            };


            if (_context.Urls.Where(u => u.long_url == long_url).Any())
            {
                return(_context.Urls.Where(u => u.long_url == long_url).FirstOrDefault().short_url);
            }

            _context.Urls.Add(url);
            _context.SaveChanges();
            return(shortUrl);
        }
        public IActionResult ToUrl(string id)
        {
            var shortUrlData = Context.ShortUrlDatas.Include("Statistic").FirstOrDefault(su => su.UniquePart.Equals(id));

            if (shortUrlData == null)
            {
                ViewData["ErrorMessage"] = $"Short URL not found";
                return(View("Index"));
                //return RedirectToAction("Index");
            }

            shortUrlData.Statistic = shortUrlData.Statistic ?? new Statistic();

            shortUrlData.Statistic.NumOfClicks++;

            Context.SaveChanges();

            return(Redirect(shortUrlData.LongUrl));
        }
Example #8
0
        public static Task <ShortUrlData> ShortUrl(HttpRequest request, UrlShortenerContext context, string longUrl)
        {
            return(Task.Run(() =>
            {
                bool isUrl = Uri.TryCreate(longUrl, UriKind.Absolute, out Uri uriResult) &&
                             (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

                if (!isUrl)
                {
                    throw new ShortUrlNotUrlException();
                }

                var existingUrl = context.ShortUrlDatas.FirstOrDefault(sud => sud.LongUrl.Equals(uriResult.AbsoluteUri));
                if (existingUrl != null)
                {
                    throw new ShortUrlExistsException();
                }

                var uniquePart = GetUniquePart(context);
                var shortUrl = GetShortUrl(request, uniquePart);

                var shortUrlData = new ShortUrlData
                {
                    LongUrl = uriResult.AbsoluteUri,
                    ShortUrl = shortUrl,
                    UniquePart = uniquePart,
                    Statistic = new Statistic()
                };

                context.ShortUrlDatas.Add(shortUrlData);

                context.SaveChanges();

                return shortUrlData;
            }));
        }
 public bool Save()
 {
     return(_context.SaveChanges() >= 0);
 }
 public void SaveChanges()
 {
     _context.SaveChanges();
 }