Exemplo n.º 1
0
        /// <summary>
        /// Method to create the database collection associated with the specified model type using
        /// the current configuration settings.
        /// </summary>
        /// <returns></returns>
        public static async Task <bool> CreateCollectionAsync <T>(IDocumentDBConfiguration configuration)
        {
            string collectionName = ModelUtility.GetPluralizedModelName <T>();

            using (var client = NewDocumentClient(configuration))
            {
                string             collectionsFeed = GetCollectionsFeed(configuration);
                DocumentCollection collection      = client.CreateDocumentCollectionQuery(collectionsFeed)
                                                     .Where(c => c.Id == collectionName)
                                                     .AsEnumerable()
                                                     .FirstOrDefault();
                if (collection == null)
                {
                    collection = await client.CreateDocumentCollectionAsync(
                        collectionsFeed,
                        new DocumentCollection { Id = collectionName });

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <T> GetAsync(string id)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                var result = await connection.QueryAsync <T>($"select * from {ModelUtility.GetPluralizedModelName<T>()} where id = @Id", new { Id = id });

                return(result.FirstOrDefault());
            }
        }
Exemplo n.º 3
0
        public async Task <bool> DeleteAsync(string id)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                var resultCount = await connection.ExecuteAsync($"delete {ModelUtility.GetPluralizedModelName<T>()} where id = @Id", new { Id = id });

                return(resultCount == 1);
            }
        }
Exemplo n.º 4
0
        public async Task <T> UpsertAsync(T model)
        {
            string command = String.Empty;

            using (var connection = new SqlConnection(ConnectionString))
            {
                var ids = await connection.QueryAsync <string>("select top 1 id from DataPoints where id = @Id", model);

                if (ids == null || ids.Count() != 1)
                {
                    command = String.Format(
                        "insert into {0} ({1}) values({2})",
                        ModelUtility.GetPluralizedModelName <T>(),
                        DapperUtility.GetPropertyNames <T>(),
                        DapperUtility.GetPropertyNames <T>(true));
                }
                else
                {
                    command = String.Format(
                        "update {0} set {1} where id = @Id",
                        ModelUtility.GetPluralizedModelName <T>(),
                        DapperUtility.GetPropertiesAssignmentSqlString <T>());
                }

                var upsertResult = await connection.ExecuteAsync(command, model);

                if (upsertResult == 1)
                {
                    connection.Close();
                    return(model);
                }
                else
                {
                    connection.Close();
                    return(null);
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Get a string for a document feed based on configuration setting for the database name and the
 /// type of the model in the collection
 /// </summary>
 /// <returns></returns>
 internal static string GetDocumentFeed <T>(IDocumentDBConfiguration configuration) where T : IModel
 {
     return($"{GetCollectionsFeed(configuration)}colls/{ModelUtility.GetPluralizedModelName<T>()}");
 }