public virtual void Add(params T[] items)
        {
            using (var context = new MicrobrewitContext())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = EntityState.Added;
                }

                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                            Log.DebugFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
            }
        }
예제 #2
0
 public void Add(Fermentable fermentable)
 {
     using (var context = new MicrobrewitContext())
     {
         if (fermentable.Supplier != null)
         {
             fermentable.Supplier = null;
         }
         context.Entry(fermentable).State = EntityState.Added;
         try
         {
             context.SaveChanges();
         }
         catch (DbEntityValidationException dbEx)
         {
             foreach (var validationErrors in dbEx.EntityValidationErrors)
             {
                 foreach (var validationError in validationErrors.ValidationErrors)
                 {
                     Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName,
                                            validationError.ErrorMessage);
                     Log.DebugFormat("Property: {0} Error: {1}", validationError.PropertyName,
                                     validationError.ErrorMessage);
                 }
             }
         }
     }
 }
 public void Add(Brewery brewery)
 {
     using (var context = new MicrobrewitContext())
     {
         brewery.Origin               = null;
         brewery.CreatedDate          = DateTime.Now;
         brewery.UpdatedDate          = DateTime.Now;
         context.Entry(brewery).State = EntityState.Added;
         try
         {
             context.SaveChanges();
         }
         catch (DbEntityValidationException dbEx)
         {
             foreach (var validationErrors in dbEx.EntityValidationErrors)
             {
                 foreach (var validationError in validationErrors.ValidationErrors)
                 {
                     Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                     Log.DebugFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                 }
             }
         }
     }
 }
예제 #4
0
        protected override int?ResolveCore(FermentableDto source)
        {
            using (var context = new MicrobrewitContext())
            {
                Supplier supplier = null;
                if (source.Supplier != null)
                {
                    supplier = context.Suppliers.SingleOrDefault(s => s.SupplierId == source.Supplier.Id || s.Name.Equals(source.Supplier.Name));

                    if (supplier == null)
                    {
                        supplier = new Supplier()
                        {
                            Name = source.Supplier.Name,
                        };
                        context.Suppliers.Add(supplier);
                        context.SaveChanges();
                    }
                    return(supplier.SupplierId);
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #5
0
 public virtual void Remove(Other other)
 {
     using (var context = new MicrobrewitContext())
     {
         context.Entry(other).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
예제 #6
0
 public virtual void Update(Other other)
 {
     using (var context = new MicrobrewitContext())
     {
         context.Entry(other).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
예제 #7
0
 public virtual void Remove(BeerStyle beerStyle)
 {
     using (var context = new MicrobrewitContext())
     {
         context.Entry(beerStyle).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
예제 #8
0
 public virtual void Update(Fermentable fermentable)
 {
     using (var context = new MicrobrewitContext())
     {
         context.Entry(fermentable).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
 public virtual void Remove(params T[] items)
 {
     using (var context = new MicrobrewitContext())
     {
         foreach (T item in items)
         {
             context.Entry(item).State = EntityState.Deleted;
         }
         context.SaveChanges();
     }
 }
        public virtual void Update(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;
                }
                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;


                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    throw;
                }
            }
        }
예제 #11
0
 public void Remove(Hop hop)
 {
     using (var context = new MicrobrewitContext())
     {
         var dbHop = context.Hops.SingleOrDefault(h => h.HopId == hop.HopId);
         if (dbHop == null)
         {
             throw new DbUpdateException("Hop does not exist.");
         }
         context.Hops.Remove(dbHop);
         context.SaveChanges();
     }
 }
예제 #12
0
 public void Add(Hop hop)
 {
     using (var context = new MicrobrewitContext())
     {
         if (hop.OriginId > 0)
         {
             hop.Origin = null;
         }
         foreach (var subs in hop.Substituts)
         {
             context.Entry(subs).State = EntityState.Unchanged;
         }
         context.Hops.Add(hop);
         context.SaveChanges();
     }
 }
예제 #13
0
        public Flavour AddFlavour(string name)
        {
            using (var context = new MicrobrewitContext())
            {
                var flavourId = (context.Flavours.Max(f => (int?)f.FlavourId) ?? 0) + 1;
                var flavour   = new Flavour()
                {
                    FlavourId = flavourId,
                    Name      = name
                };
                context.Flavours.Add(flavour);
                context.SaveChanges();

                return(flavour);
            }
        }
예제 #14
0
        protected override int ResolveCore(HopDto dto)
        {
            using (var context = new MicrobrewitContext())
            {
                Origin origin = null;
                if (dto.Origin != null)
                {
                    origin = context.Origins.SingleOrDefault(o => o.OriginId == dto.Origin.Id || o.Name.Equals(dto.Origin.Name));

                    if (origin == null)
                    {
                        origin = new Origin()
                        {
                            Name = dto.Origin.Name
                        };
                        context.Origins.Add(origin);
                        context.SaveChanges();
                    }
                }
                return(origin.OriginId);
            }
        }
예제 #15
0
        public void Update(Beer beer)
        {
            using (var context = new MicrobrewitContext())
            {
                var originalBeer = context.Beers.SingleOrDefault(b => b.BeerId == beer.BeerId);
                SetChanges(context, originalBeer, beer);

                var originalAbv = context.ABVs.SingleOrDefault(a => a.AbvId == beer.ABV.AbvId);
                if (originalAbv != null)
                {
                    SetChanges(context, originalAbv, beer.ABV);
                }

                var originalSrm = context.SRMs.SingleOrDefault(s => s.SrmId == beer.SRM.SrmId);
                if (originalSrm != null)
                {
                    SetChanges(context, originalSrm, beer.SRM);
                }

                var originalIbu = context.IBUs.SingleOrDefault(i => i.IbuId == beer.IBU.IbuId);
                if (originalIbu == null)
                {
                    SetChanges(context, originalIbu, beer.IBU);
                }

                // Brewers
                foreach (var userBeer in beer.Brewers)
                {
                    var originalUserBeer = context.UserBeers.SingleOrDefault(u => u.Username.Equals(userBeer.Username) && u.BeerId == beer.BeerId);
                    if (originalUserBeer != null)
                    {
                        SetChanges(context, originalUserBeer, userBeer);
                    }
                    else
                    {
                        context.UserBeers.Add(userBeer);
                    }
                }
                // Brewery
                foreach (var breweryBeer in beer.Breweries)
                {
                    var originalBreweryBeer = context.BreweryBeers.SingleOrDefault(b => b.BreweryId == breweryBeer.BreweryId && b.BeerId == beer.BeerId);
                    if (originalBreweryBeer != null)
                    {
                        SetChanges(context, originalBreweryBeer, breweryBeer);
                    }
                    else
                    {
                        context.BreweryBeers.Add(breweryBeer);
                    }
                }
                // Recipe
                var originalRecipe = context.Recipes.SingleOrDefault(r => r.RecipeId == beer.Recipe.RecipeId);
                if (originalRecipe == null)
                {
                    context.Recipes.Add(beer.Recipe);
                }
                else
                {
                    SetChanges(context, originalRecipe, beer.Recipe);
                    // Fermentation Step
                    foreach (var fermentationStep in beer.Recipe.FermentationSteps)
                    {
                        var originalFermentationStep = context.FermentationSteps.SingleOrDefault(s =>
                                                                                                 s.StepNumber == fermentationStep.StepNumber &&
                                                                                                 s.RecipeId == fermentationStep.RecipeId);
                        if (originalFermentationStep == null)
                        {
                            context.FermentationSteps.Add(fermentationStep);
                        }
                        else
                        {
                            SetChanges(context, originalFermentationStep, fermentationStep);
                            //Fermentable
                            foreach (var fermentable in fermentationStep.Fermentables)
                            {
                                var originalFermentable = context.FermentationStepFermentables
                                                          .SingleOrDefault(f => f.StepNumber == fermentable.StepNumber && f.FermentableId == fermentable.FermentableId && f.RecipeId == fermentable.RecipeId);
                                if (originalFermentable != null)
                                {
                                    SetChanges(context, originalFermentable, fermentable);
                                }
                                else
                                {
                                    context.FermentationStepFermentables.Add(fermentable);
                                }
                            }
                            //Hop
                            foreach (var hop in fermentationStep.Hops)
                            {
                                var originalHop = context.FermentationStepHops
                                                  .SingleOrDefault(h => h.StepNumber == hop.StepNumber && h.HopId == hop.HopId && h.RecipeId == hop.RecipeId);
                                if (originalHop != null)
                                {
                                    SetChanges(context, originalHop, hop);
                                }
                                else
                                {
                                    context.FermentationStepHops.Add(hop);
                                }
                            }
                            //Other
                            foreach (var other in fermentationStep.Others)
                            {
                                var originalOther = context.FermentationStepOthers
                                                    .SingleOrDefault(o => o.StepNumber == other.StepNumber && o.OtherId == other.OtherId && o.RecipeId == other.RecipeId);
                                if (originalOther != null)
                                {
                                    SetChanges(context, originalOther, other);
                                }
                                else
                                {
                                    context.FermentationStepOthers.Add(other);
                                }
                            }
                            //Yeast
                            foreach (var yeast in fermentationStep.Yeasts)
                            {
                                var originalYeast = context.FermentationStepYeasts
                                                    .SingleOrDefault(y => y.StepNumber == yeast.StepNumber && y.YeastId == yeast.YeastId && y.RecipeId == yeast.RecipeId);
                                if (originalYeast != null)
                                {
                                    SetChanges(context, originalYeast, yeast);
                                }
                                else
                                {
                                    context.FermentationStepYeasts.Add(yeast);
                                }
                            }
                        }
                    }

                    // Boil step
                    foreach (var boilStep in beer.Recipe.BoilSteps)
                    {
                        var originalBoilStep = context.BoilSteps.SingleOrDefault(s => s.RecipeId == boilStep.RecipeId && s.StepNumber == boilStep.StepNumber);
                        if (originalBoilStep == null)
                        {
                            context.BoilSteps.Add(boilStep);
                        }
                        else
                        {
                            SetChanges(context, originalBoilStep, boilStep);
                            //Fermentable
                            foreach (var fermentable in boilStep.Fermentables)
                            {
                                var originalFermentable = context.BoilStepFermentables
                                                          .SingleOrDefault(f =>
                                                                           f.StepNumber == fermentable.StepNumber &&
                                                                           f.FermentableId == fermentable.FermentableId &&
                                                                           f.RecipeId == boilStep.RecipeId);

                                if (originalFermentable != null)
                                {
                                    SetChanges(context, originalFermentable, fermentable);
                                }
                                else
                                {
                                    context.BoilStepFermentables.Add(fermentable);
                                }
                            }
                            //Hop
                            foreach (var hopStep in boilStep.Hops)
                            {
                                var originalHopStep = context.BoilStepHops
                                                      .SingleOrDefault(h => h.StepNumber == hopStep.StepNumber && h.HopId == hopStep.HopId && h.RecipeId == hopStep.RecipeId);
                                if (originalHopStep != null)
                                {
                                    SetChanges(context, originalHopStep, hopStep);
                                }
                                else
                                {
                                    context.BoilStepHops.Add(hopStep);
                                }
                            }
                            //Other
                            foreach (var otherStep in boilStep.Others)
                            {
                                var originalOtherStep = context.BoilStepOthers
                                                        .SingleOrDefault(o => o.StepNumber == otherStep.StepNumber && o.OtherId == otherStep.OtherId && o.RecipeId == otherStep.RecipeId);
                                if (originalOtherStep != null)
                                {
                                    SetChanges(context, originalOtherStep, otherStep);
                                }
                                else
                                {
                                    context.BoilStepOthers.Add(otherStep);
                                }
                            }
                        }
                    }
                    // Updates changes to the mash steps
                    foreach (var step in beer.Recipe.MashSteps)
                    {
                        var originalStep = context.MashSteps.SingleOrDefault(s => s.StepNumber == step.StepNumber && s.RecipeId == step.RecipeId);
                        if (originalStep == null)
                        {
                            context.MashSteps.Add(step);
                        }
                        else
                        {
                            SetChanges(context, originalStep, step);
                            //Fermentable
                            foreach (var fermentableStep in step.Fermentables)
                            {
                                var originalFermentableStep = context.MashStepFermentables
                                                              .SingleOrDefault(f => f.StepNumber == fermentableStep.StepNumber && f.FermentableId == fermentableStep.FermentableId && f.RecipeId == step.RecipeId);
                                if (originalFermentableStep != null)
                                {
                                    SetChanges(context, originalFermentableStep, fermentableStep);
                                }
                                else
                                {
                                    context.MashStepFermentables.Add(fermentableStep);
                                }
                            }
                            //Hop
                            foreach (var hopStep in step.Hops)
                            {
                                var originalHopStep = context.MashStepHops
                                                      .SingleOrDefault(h => h.StepNumber == hopStep.StepNumber && h.HopId == hopStep.HopId && h.RecipeId == step.RecipeId);
                                if (originalHopStep != null)
                                {
                                    SetChanges(context, originalHopStep, hopStep);
                                }
                                else
                                {
                                    context.MashStepHops.Add(hopStep);
                                }
                            }
                            //Other
                            foreach (var otherStep in step.Others)
                            {
                                var originalOtherStep = context.MashStepOthers
                                                        .SingleOrDefault(o => o.StepNumber == otherStep.StepNumber && o.OtherId == otherStep.OtherId && o.RecipeId == step.RecipeId);
                                if (originalOtherStep != null)
                                {
                                    SetChanges(context, originalOtherStep, otherStep);
                                }
                                else
                                {
                                    context.MashStepOthers.Add(otherStep);
                                }
                            }
                        }
                    }
                }

                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    throw;
                }
            }
        }