Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created April 29, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/46eb2ea35eb4e4cd4d453751cbd70f72 to your computer and use it in GitHub Desktop.
Save justinyoo/46eb2ea35eb4e4cd4d453751cbd70f72 to your computer and use it in GitHub Desktop.
Precompiled Azure Functions Revisited
public class ProductService
{
...
public async Task<ProductModel> GetAsync(string productId)
{
var id = Guid.Parse(productId);
return await this.GetAsync(id).ConfigureAwait(false);
}
public async Task<ProductModel> GetAsync(Guid productId)
{
var product = await this._context.Products
.Select(
p => new ProductModel()
{
ProductId = p.ProductId,
Name = p.Name,
Description = p.Description,
UnitPrice = p.UnitPrice
})
.SingleOrDefaultAsync(p => p.ProductId == productId)
.ConfigureAwait(false);
return product;
}
public async Task<int> SaveAsync(ProductModel model)
{
var now = DateTimeOffset.UtcNow;
var product = await this._context.Products
.SingleOrDefaultAsync(p => p.ProductId == model.ProductId)
.ConfigureAwait(false);
if (product == null)
{
product = new Product() { ProductId = Guid.NewGuid(), DateCreated = now };
}
product.Name = model.Name;
product.Description = model.Description;
product.UnitPrice = model.UnitPrice;
product.DateUpdated = now;
this._context.Products.Add(product);
var result = await this._context.SaveChangesAsync().ConfigureAwait(false);
return result;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment