Example #1
0
        public void Should_Be_Able_to_Short_An_URL_to_Charcter()
        {
            Shortner shortner = new Shortner();
            var      token    = shortner.Encode(125);

            token.Should().Be("4p");
        }
Example #2
0
        public void Should_Be_Able_to_Decode_the_token_to_Number()
        {
            Shortner shortner = new Shortner();
            var      token    = shortner.Decode("4p");

            token.Should().Be(125);
        }
Example #3
0
        public async Task <IActionResult> PutShortner(int id, Shortner shortner)
        {
            if (id != shortner.ID)
            {
                return(BadRequest());
            }

            _context.Entry(shortner).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ShortnerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public void CanInsertshortnerIntoDatabasee()
        {
            using var context = new ApplicationDbContext();
            var shortner = new Shortner();

            context.Shortner.Add(shortner);
            Assert.AreEqual(EntityState.Added, context.Entry(shortner).State);
        }
Example #5
0
        public async Task <ActionResult <Shortner> > PostShortner(Shortner shortner)
        {
            Uri        urlcheck;
            WebRequest req;

            //bool UrlTest = true;
            if (shortner.oUrl.Contains("http://") || shortner.oUrl.Contains("https://"))
            {
                urlcheck = new Uri(shortner.oUrl);
            }
            else
            {
                urlcheck      = new Uri("http://" + shortner.oUrl);
                shortner.oUrl = "http://" + shortner.oUrl;
            }

            req = WebRequest.Create(urlcheck);
            try
            {
                var res = req.GetResponse();
            }
            catch (Exception)
            {
                return(NotFound());
            }



            var val = _context.UrlShortner.Where(l => l.oUrl == shortner.oUrl).FirstOrDefaultAsync();

            if (val.Result == null)
            {
                var rowid = _context.UrlShortner.OrderByDescending(p => p.ID).FirstOrDefault();
                if (rowid == null)
                {
                    shortner.simUrl = "https://localhost:85/api/shortners/" + 1;
                    _context.UrlShortner.Add(shortner);
                    await _context.SaveChangesAsync();

                    return(CreatedAtAction("GetShortner", new { id = shortner.ID }, shortner));
                }
                int shrt = rowid.ID + 1;
                shortner.simUrl = "https://localhost:85/api/shortners/" + shrt;
                _context.UrlShortner.Add(shortner);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetShortner", new { id = shortner.ID }, shortner));
            }
            else
            {
                var smurl = await _context.UrlShortner.FindAsync(val.Result.ID);

                return(smurl);
            }
        }
Example #6
0
            public async Task <string> Handle(CreateShortnerCommand request, CancellationToken cancellationToken)
            {
                var shortenedUrl = await this._urlManager.ShortenUrl(request.LongUrl, request.Ip);

                var shortner = new Shortner
                {
                    CreationDate = DateTime.Now,
                    LongUrl      = request.LongUrl,
                    ShortenedUrl = shortenedUrl,
                    Ip           = request.Ip,
                    NumOfClicks  = 0
                };

                _context.Shortner.Add(shortner);
                await _context.SaveChangesAsync();

                return(shortner.ShortenedUrl);
            }
Example #7
0
        public async Task <ActionResult <Shortner> > PostShortner(Shortner shortner)
        {
            var env = Environment.GetEnvironmentVariable("applicationUrl");
            //var baseUrl = string.Format("{ 0}://{1}{2}", Request.Uri.Scheme, Request.Uri.Authority, Url.Content("~"));
            //HttpRequest Request = HttpContext.Request;
            //string baseUrl = (Url.Request.RequestUri.GetComponents(
            //        UriComponents.SchemeAndServer, UriFormat.Unescaped).TrimEnd('/')
            //     + HttpContext.Current.Request.ApplicationPath).TrimEnd('/');

            //test part
            Uri        urlcheck;
            WebRequest req;

            //bool UrlTest = true;
            if (shortner.oUrl.Contains("http://") || shortner.oUrl.Contains("https://"))
            {
                urlcheck = new Uri(shortner.oUrl);
            }
            else
            {
                urlcheck      = new Uri("http://" + shortner.oUrl);
                shortner.oUrl = "http://" + shortner.oUrl;
            }

            req = WebRequest.Create(urlcheck);
            try
            {
                var res = req.GetResponse();
            }
            catch (Exception)
            {
                return(NotFound());
            }



            var val = _context.UrlShortner.Where(l => l.oUrl == shortner.oUrl).FirstOrDefaultAsync();

            if (val.Result == null)
            {
                var rowid = _context.UrlShortner.OrderByDescending(p => p.ID).FirstOrDefault();
                if (rowid == null)
                {
                    shortner.simUrl = "http://simly.azurewebsites.net/api/shortners/" + 1;
                    _context.UrlShortner.Add(shortner);
                    await _context.SaveChangesAsync();

                    return(CreatedAtAction("GetShortner", new { id = shortner.ID }, shortner));
                }
                int shrt = rowid.ID + 1;
                shortner.simUrl = "http://simly.azurewebsites.net/api/shortners/" + shrt;
                _context.UrlShortner.Add(shortner);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetShortner", new { id = shortner.ID }, shortner));
            }
            else
            {
                var smurl = await _context.UrlShortner.FindAsync(val.Result.ID);

                return(smurl);
            }
        }