示例#1
0
        public async Task <ShortLink> CreateShortLinkAsync(ShortLink shortLink)
        {
            await _context.ShortLinks.AddAsync(shortLink);

            await _context.SaveChangesAsync();

            return(shortLink);
        }
示例#2
0
        public async Task <ShortLink> CreateShortLinkAsync(string fullLink)
        {
            var shortLink = new ShortLink
            {
                FullLink = fullLink
            };

            for (var length = 2; length < 100; length++)
            {
                var slug = SlugGenerator.Generate(length);
                if (!await _context.ShortLinks.AnyAsync(s => s.Slug == slug))
                {
                    shortLink.Slug = slug;
                    break;
                }
            }

            await _context.ShortLinks.AddAsync(shortLink);

            await _context.SaveChangesAsync();

            return(shortLink);
        }