private void saveOffers(List <Offer> offers)
        {
            // Variables that needs to be calculated
            decimal?kroner_savings;
            decimal?percent_saving_retailer;

            // Go through all eTilbudsAvisen offers and create equivalent dbOffers from the data, recognized by the Database
            for (int i = 0; i < offers.Count - 1; i++)
            {
                Offers dbOffers = new Offers();

                // If an offer can not be mapped to a retailer, skip the offer
                if (RetailerIdMap.ContainsKey(offers[i].store_id))
                {
                    //Convert values of offers to dbOffers
                    dbOffers.GetOrSetRetailerId  = RetailerIdMap[offers[i].store_id];
                    dbOffers.GetOrSetOfferFrom   = XmlConvert.ToDateTime(offers[i].run_from, XmlDateTimeSerializationMode.Utc);
                    dbOffers.GetOrSetOfferTo     = XmlConvert.ToDateTime(offers[i].run_till, XmlDateTimeSerializationMode.Utc);
                    dbOffers.GetOrSetOnSalePrice = (decimal)offers[i].pricing.price;

                    //Some items does not have values; directly, or because they can't be calculated: give default value of -1.0
                    if (String.IsNullOrEmpty(offers[i].pricing.pre_price.ToString()))
                    {
                        dbOffers.GetOrSetNormalPrice              = defaultValue;
                        dbOffers.GetOrSetKrSaving                 = defaultValue;
                        dbOffers.GetOrSetPercentageSavingGeneral  = defaultValue;
                        dbOffers.GetOrSetPercentageSavingRetailer = defaultValue;

                        //If variables have values, calculate and/or convert them to an appropriate format.
                    }
                    else
                    {
                        dbOffers.GetOrSetNormalPrice = (decimal)offers[i].pricing.pre_price;
                        kroner_savings            = dbOffers.GetOrSetNormalPrice - dbOffers.GetOrSetOnSalePrice;
                        dbOffers.GetOrSetKrSaving = kroner_savings;
                        percent_saving_retailer   = (dbOffers.GetOrSetOnSalePrice / dbOffers.GetOrSetNormalPrice) * 100;
                        dbOffers.GetOrSetPercentageSavingRetailer = percent_saving_retailer;

                        //TODO: once the database has been set up and each offer has been matched with ingredient, do this.
                        dbOffers.GetOrSetPercentageSavingGeneral = (decimal) - 1.0;
                    }

                    //TODO: ingredientId?? - See TODO above
                    dbOffers.GetOrSetIngredientId = 1;

                    //Save to Database
                    DBController dbc = new DBController();
                    dbc.AddOffers(dbOffers);
                    dbc.Close();
                }
            }
        }
Beispiel #2
0
        public void AddOffers(Offers off)
        {
            DBController dbc = new DBController();

            try {
                dbc.AddOffers(off);
            } catch (NpgsqlException e) {
                Console.WriteLine((Program.sqlDebugMessages) ? "AddOffers: " + e.BaseMessage.ToString() : "");
                WebOperationContext ctx = WebOperationContext.Current;
                ctx.OutgoingResponse.StatusCode        = System.Net.HttpStatusCode.Conflict;
                ctx.OutgoingResponse.StatusDescription = e.BaseMessage;
            } finally {
                dbc.Close();
            }
        }
Beispiel #3
0
        public static void dbMassInsert()
        {
            DBController dbc = new DBController();

            dbc.AddAccount(ModelDebug.GetTestAccount());
            dbc.AddIngredient(ModelDebug.GetTestIngredient());
            dbc.AddRetailer(ModelDebug.GetTestRetailer());
            dbc.AddRecipe(ModelDebug.GetTestRecipe());
            dbc.AddComment(ModelDebug.GetTestComment());

            dbc.AddFavorises(ModelDebug.GetTestFavorises());
            dbc.AddHasEaten(ModelDebug.GetTestHasEaten());
            dbc.AddIngredientIn(ModelDebug.GetTestIngredientIn());
            dbc.AddOffers(ModelDebug.GetTestOffers());
            dbc.AddPictures(ModelDebug.GetTestPictures());

            dbc.Close();
        }