public async Task <string> ShortenAndStoreUrl(string url) { ShortenedUrl shortenedUrl; do { shortenedUrl = new ShortenedUrl(url); } while (!await _shortenedUrlStorage.TrySave(shortenedUrl)); return(shortenedUrl.ShortUrl); }
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); } }