/// <summary> /// Creates a new product with default values /// </summary> /// <returns></returns> internal ProductEntity Create() { ProductEntity product = new ProductEntity { ReleaseDate = DateTime.Now }; return product; }
private bool WriteData(ProductEntity product) { // Insert-or-replace an entity StorageCredentials creds = new StorageCredentials(accountName, accountKey); CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); CloudTableClient tableClient = account.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("products"); // Create a retrieve operation that takes a product entity TableOperation retrieveOperation = TableOperation.Retrieve<ProductEntity>(product.PartitionKey, product.RowKey); TableResult retrieveResult = table.Execute(retrieveOperation); ProductEntity updateEntity = (ProductEntity) retrieveResult.Result; if (updateEntity != null) { updateEntity.Description = product.Description; updateEntity.Price = product.Price; TableOperation insertOrReplaceOperation = TableOperation.Replace(updateEntity); table.Execute(insertOrReplaceOperation); } else { TableOperation insertOperation = TableOperation.Insert(product); table.Execute(insertOperation); } return true; }
/// <summary> /// Saves a new product. /// </summary> /// <param name="product"></param> /// <returns></returns> internal ProductEntity Save(ProductEntity product) { // Read in the existing products var products = this.RetrieveTable(); // Assign a new Id var maxId = products.Max(p => p.ProductId); product.ProductId = maxId + 1; WriteData(product); return product; }