Exemple #1
0
        public async Task <Link> AddRandomUrl(Link url)
        {
            string hash;

            while (true)
            {
                hash = await hashService.GenerateHash(url.LongURL);

                var isExist = await IsShortUrlExist(hash);

                if (!isExist)
                {
                    break;
                }
            }
            url.ShortURL = hash;
            await context.Links.AddAsync(url);

            await context.SaveChangesAsync();

            return(url);
        }
        public async Task <ShortenedUrl> AddUrl(string url, string userId)
        {
            var entityEntry = await _databaseContext.AddAsync(
                new ShortenedUrl()
            {
                Created = DateTimeOffset.Now,
                Url     = url,
                UserId  = userId
            });

            var result = (await _databaseContext.SaveChangesAsync());

            if (result != 1)
            {
                return(null);
            }

            return(entityEntry.Entity);
        }