Beispiel #1
0
        public async Task <ShortenUrlResponse> ShortenURL(UrlInfo input)
        {
            int Id = await SaveToDbAndGetKey(input);

            input.Key     = Id + 1000000;
            input.UrlHash = await _helper.Encode(input.Key);

            input.Title = await _helper.GetTitle(input.Url);

            //TODO research about date time
            input.CreatedAt = DateTime.Now;
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(new ShortenUrlResponse
                {
                    Message = "Unable to save to Db"
                });
            }
            var response = UrlMapper.MapUrlInfoToShortenUrlResponse(input);

            return(response);
        }
Beispiel #2
0
        public async Task ShortenUrl_Success()
        {
            DeleteDatabase();
            using (var _dbContext = dbContext)
            {
                var TestData = GetTestData();
                foreach (var Data in TestData)
                {
                    _dbContext.Urls.Add(Data);
                }
                _dbContext.SaveChanges();
                _service = new ShortenerService(_dbContext, _config, _clientFactory, _helper);

                var input = GetShortenUrlInput();
                _helper.Encode(Arg.Any <int>()).Returns("hfRd");
                _helper.GetTitle(Arg.Any <string>()).Returns("Gmail");

                var resp = await _service.ShortenURL(input);

                Assert.IsNotNull(resp);
                Assert.IsNull(resp.Message);
                Assert.IsNotNull(resp.Url);
                Assert.AreEqual(resp.Url.OrignalUrl, input.Url);
            }
        }