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