Exemplo n.º 1
0
        public void addDiscount(DiscountComponent d)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    string sql = "INSERT INTO [dbo].[DiscountComponent] (id, percentage, duration, type, storeId, isPartOfComplex)" +
                                 " VALUES (@id,@percentage, @duration, @type, @storeId, @isPartOfComplex)";
                    int isPartOfComplex;
                    if (d.getIsPartOfComplex())
                    {
                        isPartOfComplex = 1;
                    }
                    else
                    {
                        isPartOfComplex = 0;
                    }
                    if (d is Discount)
                    {
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Discount",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        if (d is VisibleDiscount)
                        {
                            VisibleDiscount v = (VisibleDiscount)d;
                            addVisibleDiscount(v);
                        }
                        if (d is ReliantDiscount)
                        {
                            ReliantDiscount r = (ReliantDiscount)d;
                            addReliantDiscount(r);
                        }
                    }
                    if (d is DiscountComposite)
                    {
                        DiscountComposite composite = (DiscountComposite)d;
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Composite",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        foreach (DiscountComponent child in composite.getChildren())
                        {
                            string sql2 = "INSERT INTO [dbo].[DiscountComposite] (id, childid, type)" +
                                          " VALUES (@id, @childid, @type)";

                            connection.Execute(sql2, new
                            {
                                id      = d.getId(),
                                childid = child.getId(),
                                type    = composite.getType()
                            });
                        }
                    }
                    connection.Close();
                    discounts.AddFirst(d);
                }
            }

            catch (Exception e)
            {
                connection.Close();
                throw e;
            }
        }
Exemplo n.º 2
0
 public void removeDiscount(DiscountComponent d)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             connection.Execute("DELETE FROM DiscountComponent WHERE id=@id ", new { id = d.getId() });
             if (d is Discount)
             {
                 connection.Execute("DELETE FROM Discount WHERE id=@id ", new { id = d.getId() });
                 connection.Close();
             }
             else
             {
                 connection.Execute("DELETE FROM DiscountComposite WHERE id=@id ", new { id = d.getId() });
                 DiscountComposite composite = (DiscountComposite)d;
                 connection.Close();
                 foreach (DiscountComponent component in composite.getChildren())
                 {
                     removeDiscount(component);
                 }
             }
             discounts.Remove(d);
         }
     }
     catch (Exception e)
     {
         if (connection.State == ConnectionState.Open)
         {
             connection.Close();
         }
         throw e;
     }
 }