public async Task <T> GetByIdAsync(string id) { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var jay = ResolvePartitionKey(id); //var jayDoc = await cosmosDbClient.ReadDocumentAsync(id); var document = await cosmosDbClient.ReadDocumentAsync(id, new RequestOptions { PartitionKey = ResolvePartitionKey(id) }); return(JsonConvert.DeserializeObject <T>(document.ToString())); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { throw new EntityNotFoundException(); } throw; } }
public async Task <T[]> GetAllAsync() { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var documents = await cosmosDbClient.ReadDocumentsAsync <T>(); return(documents.ToArray()); }
public async Task <T> GetByIdAsync(string id) { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var document = await cosmosDbClient.ReadDocumentAsync(id, new RequestOptions { PartitionKey = this.ResolvePartitionKey(id), }); return(JsonConvert.DeserializeObject <T>(document.ToString())); } catch (DocumentClientException) { throw; } }
public async Task <List <T> > GetAll() { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var documents = await cosmosDbClient.ReadDocumentCollectionAsync(); return(JsonConvert.DeserializeObject <List <T> >(documents.ToString())); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { throw new Exception(); } throw; } }
public async Task <T> GetByIdAsync(Guid id) { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var document = await cosmosDbClient.ReadDocumentAsync(id.ToString()); var dataObject = JsonConvert.DeserializeObject <TDocument>(document.ToString()); var aggregate = new T(); aggregate.Load(dataObject); return(aggregate); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { throw new EntityNotFoundException(); } throw; } }
protected CosmosDbRepository(ICosmosDbClientFactory cosmosDbClientFactory) { if (cosmosDbClientFactory == null) { throw new ArgumentException("cosmosDbClientFactory"); } if (string.IsNullOrWhiteSpace(CollectionName)) { throw new ArgumentException("collectionName"); } container = cosmosDbClientFactory.GetClient(CollectionName).GetContainer(cosmosDbClientFactory.Database, CollectionName); }
public virtual IList <T> GetAll(Expression <Func <T, bool> > predicate) { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); return(cosmosDbClient.ReadDocumentBySql <T>(predicate).ToList()); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { throw new EntityNotFoundException(); } throw; } }
public async Task <User> GetUserByUsernameAsync(string userName) { try { var cosmosDbClient = _cosmosDbClientFactory.GetClient(CollectionName); var query = cosmosDbClient.Client. CreateDocumentQuery <UserDocument>(cosmosDbClient.GetCollectionUri(), new SqlQuerySpec() { QueryText = "SELECT * FROM Users U WHERE U.Username = @username", Parameters = new SqlParameterCollection() { new SqlParameter("@username", userName) } }); var results = await query.AsDocumentQuery() .ExecuteNextAsync <UserDocument>(); if (results.Count > 0) { var dataObject = results.FirstOrDefault(); var aggregate = new User(); aggregate.Load(dataObject); return(aggregate); } else { return(null); } } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { throw new EntityNotFoundException(); } throw; } }