예제 #1
0
        public void CanShortenUrl()
        {
            var userUrl = new UserDto()
            {
                Id = 1000,
                OriginalUrl = "http://google.com",
                AccessCount = 0,
                LastAccessedOn = DateTime.Now
            };

            var dbUserUrl = new Shorty.Data.UserUrl()
            {
                Id = 1000,

            };

            var repo = new Mock<IRepository>();
            repo.Setup(r => r.SaveUrl(It.IsAny<UserUrl>()))
                .Returns(() => dbUserUrl);

            var converter = new BaseConverter();
            shortener = new ShortService(converter, repo.Object);

            var result = shortener.ShortenUrl(userUrl);

            Assert.AreEqual("Ge", result.Url);
        }
예제 #2
0
        /// <summary>
        /// Shortens Url 
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        public UserDto ShortenUrl(UserDto userDto)
        {
            // Mapper.CreateMap<UserUrl, UserDto>().ReverseMap();
            // map the url to the datalayer.
            var url = Mapper.Map<UserUrl>(userDto);
            // save the incoming url into db
            var result =  _repository.SaveUrl(url);
            // shorten it
            userDto.Url = _shorteningAlgorithm.Encode(result.Id);
            // save back
            url.Id = result.Id;
            _repository.SaveUrl(url);

            return userDto;
        }
예제 #3
0
        public ActionResult Shorten(string url)
        {
            var userUrl = new UserDto()
            {
                OriginalUrl = url,
                CreatedOn = DateTime.Now,
                UserId = User.Identity.Name,
            };

            var result = _shortService.ShortenUrl(userUrl);

            // get current domain
            var baseurl = GetBaseUrl();
            result.Url = baseurl + "1" + result.Url;

            return Json(result);
        }