Exemple #1
0
        public async Task <string> ShortenAndStoreUrl(string url)
        {
            ShortenedUrl shortenedUrl;

            do
            {
                shortenedUrl = new ShortenedUrl(url);
            } while (!await _shortenedUrlStorage.TrySave(shortenedUrl));

            return(shortenedUrl.ShortUrl);
        }
Exemple #2
0
        public async Task <bool> TrySave(ShortenedUrl shortenedUrl)
        {
            try
            {
                await _collection.InsertOneAsync(shortenedUrl);

                return(true);
            }
            catch (MongoWriteException e) when(e.WriteError?.Category == ServerErrorCategory.DuplicateKey)
            {
                return(false);
            }
        }