public async Task <int> CreateProductAsync(Product entity) { // Set default values entity.Likes = 0; entity.Stocks = 0; entity.Available = true; DbContext.AddEntity(entity); return(await DbContext.SaveChangesAsync()); }
/// <summary> /// Reconciles the stored entity graph extending as far as described by the given extent with the one given by entity. /// It makes a number of load requests from the store, but all modifications are merely scheduled in the context. /// This overload takes a prefetched attached entity that can allow the skipping of the load request in case of a /// trivial extent. /// </summary> /// <param name="db">The context.</param> /// <param name="attachedEntity">The prefetched graph to reconcile.</param> /// <param name="templateEntity">The detached graph to reconcile with.</param> /// <param name="extent">The extent of the subgraph to reconcile.</param> /// <returns>The attached entity.</returns> internal static async Task <E> ReconcileAsync <E>(this DbContext db, E attachedEntity, E templateEntity, Action <ExtentBuilder <E> > extent) where E : class { // FIXME: We want to be able to deal with unset foreign keys, perhaps this helps: // https://stackoverflow.com/questions/4384081/read-foreign-key-metadata-programatically-with-entity-framework-4 var builder = new ExtentBuilder <E>(); extent?.Invoke(builder); if (attachedEntity == null || builder.reconcilers.Count != 0) { // In case of removals, the templateEntity is null, and otherwise // attachedEntity is often null as it's not always preloaded by // the caller. var entityToTakeTheKeyFrom = templateEntity ?? attachedEntity; attachedEntity = builder.reconcilers .Aggregate(db.GetEntity(entityToTakeTheKeyFrom), (q, r) => r.AugmentInclude(q)) .FirstOrDefault(); } var isNewEntity = attachedEntity == null || (db.Entry(attachedEntity).State == EntityState.Deleted && templateEntity != null); if (isNewEntity) { attachedEntity = db.AddEntity(templateEntity); } foreach (var reconciler in builder.reconcilers) { await reconciler.ReconcileAsync(db, attachedEntity, templateEntity); } if (!isNewEntity && templateEntity != null) { db.UpdateEntity(attachedEntity, templateEntity); } foreach (var property in builder.properties) { property.Modify(db, attachedEntity, templateEntity); } return(attachedEntity); }
public async Task PlaceOrderAsync(OrderHeader header, IEnumerable <OrderDetail> details) { using (var txn = await DbContext.Database.BeginTransactionAsync()) { try { // Set default values for order header header.OrderDate = DateTime.Now; header.Total = 0m; header.Total = details.Sum(item => item.Total); DbContext.AddEntity(header); await DbContext.SaveChangesAsync(); foreach (var item in details) { item.OrderHeaderID = header.OrderHeaderID; DbContext.AddEntity(item); } await DbContext.SaveChangesAsync(); foreach (var detail in details) { var product = await DbContext .GetProductAsync(new Product(detail.ProductID)); // Update stocks for product product.Stocks -= 1; } await DbContext.SaveChangesAsync(); txn.Commit(); } catch (Exception ex) { txn.Rollback(); throw ex; } } }
public async Task <int> LikeProductAsync(Product entity) { var productLike = await DbContext .GetProductLikeByProductIDAndCreationUserAsync(entity.ProductID, entity.LastUpdateUser); if (productLike == null) { DbContext.AddEntity(new ProductLike { ProductID = entity.ProductID, CreationUser = entity.LastUpdateUser }); entity.Likes += 1; DbContext.UpdateEntity(entity); return(await DbContext.SaveChangesAsync()); } return(0); }
public async Task <int> UpdateProductPriceAsync(Product entity) { using (var txn = await DbContext.Database.BeginTransactionAsync()) { try { // Set changes entity.Price = entity.Price; DbContext.UpdateEntity(entity); await DbContext.SaveChangesAsync(); // Add product price to history DbContext.AddEntity(new ProductPriceHistory { ProductID = entity.ProductID, Price = entity.Price, StartDate = DateTime.Now, CreationUser = entity.LastUpdateUser }); var affectedRows = await DbContext.SaveChangesAsync(); txn.Commit(); return(affectedRows); } catch (Exception ex) { txn.Rollback(); throw ex; } } }