public static async Task Change( this FavouriteProperty favouriteProperty, ApplicationDbContext db) { var oldFavouriteProperty = await db.FavouritePropertys.FirstOrDefaultAsync( pi => pi.PropertyId.Equals(favouriteProperty.OldPropertyId) && pi.FavouriteId.Equals(favouriteProperty.OldFavouriteId)); var newFavouriteProperty = await db.FavouritePropertys.FirstOrDefaultAsync( pi => pi.PropertyId.Equals(favouriteProperty.PropertyId) && pi.FavouriteId.Equals(favouriteProperty.FavouriteId)); if (oldFavouriteProperty != null && newFavouriteProperty == null) { newFavouriteProperty = new FavouriteProperty { FavouriteId = favouriteProperty.FavouriteId, PropertyId = favouriteProperty.PropertyId }; using (var transaction = new TransactionScope( TransactionScopeAsyncFlowOption.Enabled)) { try { db.FavouritePropertys.Remove(oldFavouriteProperty); db.FavouritePropertys.Add(newFavouriteProperty); await db.SaveChangesAsync(); transaction.Complete(); } catch { transaction.Dispose(); } } } }
public async Task <ActionResult> DeleteConfirmed( int favouriteId, int propertyId) { FavouriteProperty favouriteProperty = await GetFavouriteProperty(favouriteId, propertyId); db.FavouritePropertys.Remove(favouriteProperty); await db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <ActionResult> Create( [Bind(Include = "PropertyId,FavouriteId")] FavouriteProperty favouriteProperty) { if (ModelState.IsValid) { db.FavouritePropertys.Add(favouriteProperty); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(favouriteProperty)); }
public static async Task <bool> CanChange( this FavouriteProperty favouriteProperty, ApplicationDbContext db) { var oldFP = await db.FavouritePropertys.CountAsync(fp => fp.PropertyId.Equals(favouriteProperty.OldPropertyId) && fp.FavouriteId.Equals(favouriteProperty.OldFavouriteId)); var newFP = await db.FavouritePropertys.CountAsync(fp => fp.PropertyId.Equals(favouriteProperty.PropertyId) && fp.FavouriteId.Equals(favouriteProperty.FavouriteId)); return(oldFP.Equals(1) && newFP.Equals(0)); }
// GET: Admin/FavouriteProperty/Delete/5 public async Task <ActionResult> Delete(int?favouriteId, int?propertyId) { if (favouriteId == null || propertyId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } FavouriteProperty favouriteProperty = await GetFavouriteProperty(favouriteId, propertyId); if (favouriteProperty == null) { return(HttpNotFound()); } return(View(await favouriteProperty.Convert(db))); }
public async Task <ActionResult> Edit( [Bind(Include = "PropertyId,FavouriteId,OldPropertyId,OldFavouriteId")] FavouriteProperty favouriteProperty) { if (ModelState.IsValid) { var canChange = await favouriteProperty.CanChange(db); if (canChange) { await favouriteProperty.Change(db); } return(RedirectToAction("Index")); } return(View(favouriteProperty)); }
public static async Task <FavouritePropertyModel> Convert( this FavouriteProperty favouriteProperty, ApplicationDbContext db, bool addListData = true) { var model = new FavouritePropertyModel { FavouriteId = favouriteProperty.FavouriteId, PropertyId = favouriteProperty.PropertyId, Favourites = addListData ? await db.Favourites.ToListAsync() : null, Propertys = addListData ? await db.Propertys.ToListAsync() : null, FavouriteTitle = (await db.Favourites.FirstOrDefaultAsync( f => f.Id.Equals(favouriteProperty.FavouriteId))).Title, PropertyTitle = (await db.Propertys.FirstOrDefaultAsync(p => p.Id.Equals(favouriteProperty.PropertyId))).Title }; return(model); }