Ejemplo n.º 1
0
        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());
    }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        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;
            }
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 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;
            }
        }