Exemple #1
0
        public RecipeWithIngredients GetRecipeByIdWithIngredients(int recipeId)
        {
            RecipeWithIngredients rec    = new RecipeWithIngredients();
            List <Offers>         offers = new List <Offers>();

            rec.GetOrSetRecipe      = GetRecipeById(recipeId);
            rec.GetOrSetIngredients = GetIngredientsByRecipeId(recipeId);

            foreach (Ingredient ingredient in rec.GetOrSetIngredients)
            {
                offers.AddRange(GetOfferByIngredientId(ingredient.GetOrSetId));
            }

            rec.GetOrSetOffers        = offers;
            rec.GetOrSetIngredientIns = GetIngredientInsByRecipeId(recipeId);
            return(rec);
        }
Exemple #2
0
        public RecipeWithIngredients GetRecipesByIdWithIngredients(int recipeId)
        {
            DBController        dbc = new DBController();
            WebOperationContext ctx = WebOperationContext.Current;

            try {
                RecipeWithIngredients tempRec = dbc.GetRecipeByIdWithIngredients(recipeId);
                if (tempRec != null)
                {
                    return(tempRec);
                }
            } catch (NpgsqlException e) {
                Console.WriteLine((Program.sqlDebugMessages) ? "GetRecipesByIdWithIngredients: " + e.BaseMessage.ToString() : "");
                ctx.OutgoingResponse.StatusCode        = System.Net.HttpStatusCode.Conflict;
                ctx.OutgoingResponse.StatusDescription = e.BaseMessage;
                return(null);
            } finally {
                dbc.Close();
            }
            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NoContent;
            return(null);
        }
Exemple #3
0
        public Dictionary <Retailer, ExtraData> getExtraData(int recipeId, int radius, double latitude, double longitude)
        {
            decimal NormalPrice      = 0;
            decimal storeNormalPrice = 0;
            decimal price            = 0;
            decimal savingStorePrice;
            decimal savingStorePercent;
            decimal savingGeneralPrice;
            decimal savingGeneralPercent;

            DBController dbc = new DBController();

            Dictionary <Retailer, ExtraData> retailerDictionary = new Dictionary <Retailer, ExtraData>();

            RecipeWithIngredients recipeWithIngredients = dbc.GetRecipeByIdWithIngredients(recipeId);

            List <Retailer> retailers = GetStoresWithinRadius(dbc.GetAllRetailers(), radius, latitude, longitude);

            //finds all retailers within the radius which has an offer on anything on the recipe.
            for (int i = retailers.Count() - 1; i > -1; i--)
            {
                bool found = false;

                foreach (Offers offer in recipeWithIngredients.GetOrSetOffers)
                {
                    if (retailers[i].GetOrSetId == offer.GetOrSetRetailerId)
                    {
                        found = true;
                    }
                }

                if (!found)
                {
                    retailers.RemoveAt(i);
                }
            }

            //foreach retailer, get the offered price, the normal price from the store, and the normal price from average.
            foreach (Retailer retailer in retailers)
            {
                foreach (Ingredient ingredient in recipeWithIngredients.GetOrSetIngredients)
                {
                    foreach (Offers offer in recipeWithIngredients.GetOrSetOffers)
                    {
                        if (offer.GetOrSetIngredientId == ingredient.GetOrSetId)
                        {
                            price += offer.GetOrSetOnSalePrice;

                            if (storeNormalPrice != -1)
                            {
                                if (offer.GetOrSetNormalPrice.HasValue)
                                {
                                    storeNormalPrice += (decimal)offer.GetOrSetNormalPrice;
                                }
                                else
                                {
                                    storeNormalPrice = -1;
                                }
                            }
                        }
                        else
                        {
                            price            += ingredient.GetOrSetPrice;
                            storeNormalPrice += ingredient.GetOrSetPrice;
                        }

                        NormalPrice += ingredient.GetOrSetPrice;
                    }
                }

                //The saving prices and percentages are calculated
                savingGeneralPrice = NormalPrice - price;
                savingStorePrice   = storeNormalPrice - price;

                savingGeneralPercent = savingGeneralPrice / price * 100;
                savingStorePercent   = savingStorePrice / price * 100;

                //Important, this is the order in which the dictionary is ordered

                retailerDictionary.Add(retailer, new ExtraData(price, savingStorePrice, savingGeneralPrice, savingStorePercent, savingGeneralPercent, retailer.GetOrSetId));
                price = 0;
            }

            retailerDictionary = retailerDictionary.OrderBy(x => x.Value.Price).ToDictionary(x => x.Key, x => x.Value);

            dbc.Close();
            return(retailerDictionary);
        }