public virtual async Task RemoveAsync(Origin origin) { using (var context = new MicrobrewitContext()) { context.Entry(origin).State = EntityState.Deleted; try { await context.SaveChangesAsync(); } catch (Exception e) { Log.Debug(e); throw; } } }
public virtual async Task <int> UpdateAsync(Brewery brewery) { using (var context = new MicrobrewitContext()) { brewery.UpdatedDate = DateTime.Now; var originalBrewery = context.Breweries.Include(b => b.Members).SingleOrDefault(b => b.BreweryId == brewery.BreweryId); if (originalBrewery == null) { return(-1); } brewery.CreatedDate = originalBrewery.CreatedDate; SetChanges(context, originalBrewery, brewery); foreach (var member in brewery.Members) { var existingMember = originalBrewery.Members.Any(m => m.MemberUsername.Equals(member.MemberUsername)); if (existingMember) { var originalMember = originalBrewery.Members.SingleOrDefault(m => m.MemberUsername.Equals(member.MemberUsername)); SetChanges(context, originalMember, member); } else { context.BreweryMembers.Add(member); } } foreach (var brewerySocial in brewery.Socials) { var existingBrewerySocial = context.BrewerySocials.SingleOrDefault( s => s.BreweryId == brewerySocial.BreweryId && s.SocialId == brewerySocial.SocialId); if (existingBrewerySocial != null) { SetChanges(context, existingBrewerySocial, brewerySocial); } else { context.BrewerySocials.Add(brewerySocial); } } brewery.Origin = null; return(await context.SaveChangesAsync()); } }
public virtual async Task <int> UpdateAsync(params T[] items) { using (var context = new MicrobrewitContext()) { foreach (T item in items) { context.Entry(item).State = EntityState.Modified; } try { return(await context.SaveChangesAsync()); } catch (Exception e) { throw; } } }
public virtual async Task RemoveAsync(params T[] items) { using (var context = new MicrobrewitContext()) { foreach (T item in items) { context.Entry(item).State = EntityState.Deleted; } try { await context.SaveChangesAsync(); } catch (Exception e) { Log.Debug(e); throw; } } }
public async Task <bool> ConfirmUserBeerAsync(string username, NotificationDto notificationDto) { using (var context = new MicrobrewitContext()) { var userBeer = await context.UserBeers.SingleOrDefaultAsync(b => b.BeerId == notificationDto.Id && b.Username == username); if (userBeer == null) { return(false); } if (notificationDto.Value) { userBeer.Confirmed = true; } else { context.UserBeers.Remove(userBeer); } await context.SaveChangesAsync(); return(true); } }
public async Task <int> UpdateAsync(Beer newBeer) { using (var context = new MicrobrewitContext()) { var dbBeer = context.Beers .Include(b => b.ABV) .Include(b => b.SRM) .Include(b => b.IBU) .Include(b => b.Brewers) .Include(b => b.Breweries) .SingleOrDefault(b => b.BeerId == newBeer.BeerId); newBeer.BeerStyle = null; newBeer.CreatedDate = dbBeer.CreatedDate; newBeer.UpdatedDate = DateTime.Now; context.Entry(dbBeer).CurrentValues.SetValues(newBeer); if (newBeer.ABV != null) { var dbAbv = context.ABVs.SingleOrDefault(a => a.AbvId == newBeer.ABV.AbvId); if (dbAbv != null) { context.Entry(dbBeer.ABV).CurrentValues.SetValues(newBeer.ABV); } else { dbBeer.ABV = newBeer.ABV; } } if (newBeer.SRM != null) { var dbSrm = context.SRMs.SingleOrDefault(s => s.SrmId == newBeer.SRM.SrmId); if (dbSrm != null) { context.Entry(dbBeer.SRM).CurrentValues.SetValues(newBeer.SRM); } else { dbBeer.SRM = newBeer.SRM; } } if (newBeer.IBU != null) { var dbIbu = context.IBUs.SingleOrDefault(i => i.IbuId == newBeer.IBU.IbuId); if (dbIbu != null) { context.Entry(dbBeer.IBU).CurrentValues.SetValues(newBeer.IBU); } else { dbBeer.IBU = newBeer.IBU; } } // Brewers foreach (var brewers in dbBeer.Brewers.ToList()) { if (newBeer.Brewers.All(b => b.Username != brewers.Username)) { context.UserBeers.Remove(brewers); } } foreach (var newUserBeer in newBeer.Brewers) { var dbUserBeer = context.UserBeers.SingleOrDefault(u => u.Username.Equals(newUserBeer.Username) && u.BeerId == newBeer.BeerId); if (dbUserBeer != null) { context.Entry(dbUserBeer).CurrentValues.SetValues(newUserBeer); } else { context.UserBeers.Add(newUserBeer); } } // Brewery foreach (var dbBreweryBeers in dbBeer.Breweries.ToList()) { if (newBeer.Breweries.All(b => b.BreweryId != dbBreweryBeers.BreweryId)) { context.BreweryBeers.Remove(dbBreweryBeers); } } foreach (var newBreweryBeer in newBeer.Breweries) { var dbBreweryBeer = context.BreweryBeers.SingleOrDefault(b => b.BreweryId == newBreweryBeer.BreweryId && b.BeerId == newBeer.BeerId); if (dbBreweryBeer != null) { context.Entry(dbBreweryBeer).CurrentValues.SetValues(newBreweryBeer); } else { context.BreweryBeers.Add(newBreweryBeer); } } // Recipe if (newBeer.Recipe != null) { var dbRecipe = context.Recipes .Include(r => r.FermentationSteps) .Include(r => r.MashSteps) .Include(r => r.BoilSteps) // .Include(r => r.SpargeStep) .SingleOrDefault(r => r.RecipeId == newBeer.Recipe.RecipeId); if (dbRecipe != null) { context.Entry(dbRecipe).CurrentValues.SetValues(newBeer.Recipe); //Sparge step //if (newBeer.Recipe.SpargeStep != null) //{ // var newSpargeStep = newBeer.Recipe.SpargeStep; // var dbSpargeStep = context.SpargeSteps.SingleOrDefault(i => i.StepNumber == newSpargeStep.StepNumber && i.RecipeId == newSpargeStep.RecipeId); // if (dbSpargeStep != null) // { // context.Entry(dbSpargeStep).CurrentValues.SetValues(newSpargeStep); // } // else // { // dbRecipe.SpargeStep = newSpargeStep; // } //} // Fermentation Step foreach (var dbFermentationStep in dbRecipe.FermentationSteps) { if ( newBeer.Recipe.FermentationSteps.All( f => f.StepNumber != dbFermentationStep.StepNumber)) { context.FermentationSteps.Remove(dbFermentationStep); } } foreach (var newFermentationStep in newBeer.Recipe.FermentationSteps) { var dbFermentationStep = context.FermentationSteps.SingleOrDefault(s => s.StepNumber == newFermentationStep.StepNumber && s.RecipeId == newFermentationStep.RecipeId); if (dbFermentationStep != null) { context.Entry(dbFermentationStep).CurrentValues.SetValues(newFermentationStep); //Fermentable if (dbFermentationStep.Fermentables != null) { foreach (var dbFermentable in dbFermentationStep.Fermentables) { if ( newFermentationStep.Fermentables.All( f => f.FermentableId != dbFermentable.FermentableId)) { context.FermentationStepFermentables.Remove(dbFermentable); } } } if (newFermentationStep.Fermentables != null) { foreach (var newFermentable in newFermentationStep.Fermentables) { var dbFermentable = context.FermentationStepFermentables .SingleOrDefault( f => f.StepNumber == newFermentable.StepNumber && f.FermentableId == newFermentable.FermentableId && f.RecipeId == newFermentable.RecipeId); if (dbFermentable != null) { context.Entry(dbFermentable).CurrentValues.SetValues(newFermentable); } else { context.FermentationStepFermentables.Add(newFermentable); } } } //Hop if (dbFermentationStep.Hops != null) { foreach (var dbFermentationStepHop in dbFermentationStep.Hops) { if (newFermentationStep.Hops.All(h => h.HopId != dbFermentationStepHop.HopId)) { context.FermentationStepHops.Remove(dbFermentationStepHop); } } } if (newFermentationStep.Hops != null) { foreach (var newHop in newFermentationStep.Hops) { var dbHop = context.FermentationStepHops .SingleOrDefault( h => h.StepNumber == newHop.StepNumber && h.HopId == newHop.HopId && h.RecipeId == newHop.RecipeId); if (dbHop != null) { context.Entry(dbHop).CurrentValues.SetValues(newHop); } else { context.FermentationStepHops.Add(newHop); } } } //Other if (dbFermentationStep.Others != null) { foreach (var dbFermentationStepOther in dbFermentationStep.Others) { if ( newFermentationStep.Others.All( o => o.OtherId != dbFermentationStepOther.OtherId)) { context.FermentationStepOthers.Remove(dbFermentationStepOther); } } } if (newFermentationStep.Others != null) { foreach (var newOther in newFermentationStep.Others) { var dbOther = context.FermentationStepOthers .SingleOrDefault( o => o.StepNumber == newOther.StepNumber && o.OtherId == newOther.OtherId && o.RecipeId == newOther.RecipeId); if (dbOther != null) { context.Entry(dbOther).CurrentValues.SetValues(newOther); } else { context.FermentationStepOthers.Add(newOther); } } } //Yeast if (dbFermentationStep.Yeasts != null) { foreach (var dbFermentationStepYeast in dbFermentationStep.Yeasts) { if ( newFermentationStep.Yeasts.All( y => y.YeastId != dbFermentationStepYeast.YeastId)) { context.FermentationStepYeasts.Remove(dbFermentationStepYeast); } } } if (newFermentationStep.Yeasts != null) { foreach (var newFermentationStepYeast in newFermentationStep.Yeasts) { var dbYeast = context.FermentationStepYeasts .SingleOrDefault( y => y.StepNumber == newFermentationStepYeast.StepNumber && y.YeastId == newFermentationStepYeast.YeastId && y.RecipeId == newFermentationStepYeast.RecipeId); if (dbYeast != null) { context.Entry(dbYeast).CurrentValues.SetValues(newFermentationStepYeast); } else { context.FermentationStepYeasts.Add(newFermentationStepYeast); } } } } else { dbRecipe.FermentationSteps.Add(newFermentationStep); } } // Boil step foreach (var dbBoilStep in dbRecipe.BoilSteps) { if (newBeer.Recipe.BoilSteps.All(f => f.StepNumber != dbBoilStep.StepNumber)) { context.BoilSteps.Remove(dbBoilStep); } } foreach (var newBoilStep in newBeer.Recipe.BoilSteps) { var dbBoilStep = context.BoilSteps.SingleOrDefault(s => s.StepNumber == newBoilStep.StepNumber && s.RecipeId == newBoilStep.RecipeId); if (dbBoilStep != null) { context.Entry(dbBoilStep).CurrentValues.SetValues(newBoilStep); //Fermentable if (dbBoilStep.Fermentables != null) { foreach (var dbFermentable in dbBoilStep.Fermentables) { if ( newBoilStep.Fermentables.All( f => f.FermentableId != dbFermentable.FermentableId)) { context.BoilStepFermentables.Remove(dbFermentable); } } } if (newBoilStep.Fermentables != null) { foreach (var newFermentable in newBoilStep.Fermentables) { var dbFermentable = context.BoilStepFermentables .SingleOrDefault( f => f.StepNumber == newFermentable.StepNumber && f.FermentableId == newFermentable.FermentableId && f.RecipeId == newFermentable.RecipeId); if (dbFermentable != null) { context.Entry(dbFermentable).CurrentValues.SetValues(newFermentable); } else { context.BoilStepFermentables.Add(newFermentable); } } } //Hop if (dbBoilStep.Hops != null) { foreach (var dbBoilStepHop in dbBoilStep.Hops) { if (newBoilStep.Hops.All(h => h.HopId != dbBoilStepHop.HopId)) { context.BoilStepHops.Remove(dbBoilStepHop); } } } if (newBoilStep.Hops != null) { foreach (var newHop in newBoilStep.Hops) { var dbHop = context.BoilStepHops .SingleOrDefault( h => h.StepNumber == newHop.StepNumber && h.HopId == newHop.HopId && h.RecipeId == newHop.RecipeId); if (dbHop != null) { context.Entry(dbHop).CurrentValues.SetValues(newHop); } else { context.BoilStepHops.Add(newHop); } } } //Other if (dbBoilStep.Others != null) { foreach (var dbBoilStepOther in dbBoilStep.Others) { if ( newBoilStep.Others.All( o => o.OtherId != dbBoilStepOther.OtherId)) { context.BoilStepOthers.Remove(dbBoilStepOther); } } } if (newBoilStep.Others != null) { foreach (var newOther in newBoilStep.Others) { var dbOther = context.BoilStepOthers .SingleOrDefault( o => o.StepNumber == newOther.StepNumber && o.OtherId == newOther.OtherId && o.RecipeId == newOther.RecipeId); if (dbOther != null) { context.Entry(dbOther).CurrentValues.SetValues(newOther); } else { context.BoilStepOthers.Add(newOther); } } } } else { dbRecipe.BoilSteps.Add(newBoilStep); } } // Updates changes to the mash steps foreach (var dbMashStep in dbRecipe.MashSteps) { if ( newBeer.Recipe.MashSteps.All( f => f.StepNumber != dbMashStep.StepNumber)) { context.MashSteps.Remove(dbMashStep); } } foreach (var newMashStep in newBeer.Recipe.MashSteps) { var dbMashStep = context.MashSteps.SingleOrDefault(s => s.StepNumber == newMashStep.StepNumber && s.RecipeId == newMashStep.RecipeId); if (dbMashStep != null) { context.Entry(dbMashStep).CurrentValues.SetValues(newMashStep); //Fermentable if (dbMashStep.Fermentables != null) { foreach (var dbFermentable in dbMashStep.Fermentables) { if ( newMashStep.Fermentables.All( f => f.FermentableId != dbFermentable.FermentableId)) { context.MashStepFermentables.Remove(dbFermentable); } } } if (newMashStep.Fermentables != null) { foreach (var newFermentable in newMashStep.Fermentables) { var dbFermentable = context.MashStepFermentables .SingleOrDefault( f => f.StepNumber == newFermentable.StepNumber && f.FermentableId == newFermentable.FermentableId && f.RecipeId == newFermentable.RecipeId); if (dbFermentable != null) { context.Entry(dbFermentable).CurrentValues.SetValues(newFermentable); } else { context.MashStepFermentables.Add(newFermentable); } } } //Hop if (dbMashStep.Hops != null) { foreach (var dbMashStepHop in dbMashStep.Hops) { if (newMashStep.Hops.All(h => h.HopId != dbMashStepHop.HopId)) { context.MashStepHops.Remove(dbMashStepHop); } } } if (newMashStep.Hops != null) { foreach (var newHop in newMashStep.Hops) { var dbHop = context.MashStepHops .SingleOrDefault( h => h.StepNumber == newHop.StepNumber && h.HopId == newHop.HopId && h.RecipeId == newHop.RecipeId); if (dbHop != null) { context.Entry(dbHop).CurrentValues.SetValues(newHop); } else { context.MashStepHops.Add(newHop); } } } //Other if (dbMashStep.Others != null) { foreach (var dbMashStepOther in dbMashStep.Others) { if ( newMashStep.Others.All( o => o.OtherId != dbMashStepOther.OtherId)) { context.MashStepOthers.Remove(dbMashStepOther); } } } if (newMashStep.Others != null) { foreach (var newOther in newMashStep.Others) { var dbOther = context.MashStepOthers .SingleOrDefault( o => o.StepNumber == newOther.StepNumber && o.OtherId == newOther.OtherId && o.RecipeId == newOther.RecipeId); if (dbOther != null) { context.Entry(dbOther).CurrentValues.SetValues(newOther); } else { context.MashStepOthers.Add(newOther); } } } } else { dbRecipe.MashSteps.Add(newMashStep); } } } else { context.Recipes.Add(newBeer.Recipe); } } try { return(await context.SaveChangesAsync()); } catch (Exception e) { Console.WriteLine(e); throw; } } }