|
using System;
using System.Collections.Generic;
using System.Collections.Specialized; //needed for IOrderedDictionary
using System.Web;
using NHibernate.MapClasses;
using NHibernate;
using Routing.Module;
using NHibernate.Mapping;
/// <summary>
/// Summary description for BooksDB
/// </summary>
///
namespace MVC.Models
{
public class BooksForSaleDB : IBooksForSale
{
public BooksForSaleDB()
{
}
//-------------------cart manipulation methods-----------------------------------
public List<CartMember> GetShoppingCart()
{
if (HttpContext.Current.Session["cart"] != null)
return (List<CartMember>)HttpContext.Current.Session["cart"];
else
return null;
}
public void add_item_to_cart(int id)
{
CartMember CartEntry = new CartMember();
Product SingleProduct = ShowByID(id);
CartEntry.MemberId = id;
CartEntry.TitleCart = SingleProduct.Title;
CartEntry.UnitPrice = SingleProduct.Price;
CartEntry.TotalUnitPrice = CartEntry.UnitPrice;
CartEntry.Quantity = 1;
if (HttpContext.Current.Session["cart"] != null)
{
int doublepos = FindDoubleEntry(id);
if (doublepos == -1)
((List<CartMember>)HttpContext.Current.Session["cart"]).Add(CartEntry);
else
{
CartEntry.TotalUnitPrice =
((List<CartMember>)HttpContext.Current.Session["cart"])
[doublepos].TotalUnitPrice + CartEntry.UnitPrice;
CartEntry.Quantity =
((List<CartMember>)HttpContext.Current.Session["cart"])
[doublepos].Quantity + 1;
((List<CartMember>)HttpContext.Current.Session["cart"]).RemoveAt(doublepos);
((List<CartMember>)HttpContext.Current.Session["cart"]).Add(CartEntry);
}
}
else
{
List<CartMember> TempList = new List<CartMember>();
TempList.Add(CartEntry);
HttpContext.Current.Session.Add("cart", TempList);
}
}
public void EmptyCart()
{
if (HttpContext.Current.Session["cart"] != null)
{
HttpContext.Current.Session["cart"] = null;
}
}
//helper methods not in the interface
private int FindDoubleEntry(int WhatId)
{
int pos = -1;
foreach (CartMember SingleEntry in (List<CartMember>)HttpContext.Current.Session["cart"])
{
pos++;
if ((SingleEntry.MemberId == WhatId)) return pos;
}
return -1;
}
//-----------------------------------------------------------------------------
public Product ShowByID(int id)
{
ISession session = RoutingClass.NHibernateCurrentSession;
Product product;
using (session.BeginTransaction())
{
product = session.Get<Product>(id);
session.Transaction.Commit();
}
return product;
}
public IList<Product> ShowAllForSale()
{
ISession session = RoutingClass.NHibernateCurrentSession;
IList<Product> ListOfProducts;
using (session.BeginTransaction())
{
IQuery query = session.CreateQuery("from Product where Date_Available < getdate()");
ListOfProducts = query.List<Product>();
session.Transaction.Commit();
}
return ListOfProducts;
}
public int CreateOrder(IOrderedDictionary InputParameters,
ref List<CartMember> CartSession)
{
ISession session = RoutingClass.NHibernateCurrentSession;
ITransaction transaction = session.BeginTransaction();
Order order = new Order();
order.Name = (string)InputParameters["Name"];
order.Email = (string)InputParameters["Email"];
order.Address = (string)InputParameters["Address"];
order.Pay_Type = (string)InputParameters["Pay_Type"];
foreach (CartMember SingleEntry in CartSession)
{
LineItem lineitem = new LineItem();
lineitem.Product_Id = SingleEntry.MemberId; //order_id skipped
lineitem.Quantity = SingleEntry.Quantity;
lineitem.Unit_Price = SingleEntry.UnitPrice;
order.AddLineItem(lineitem); //takes care of associations
}
try
{
session.Save(order);
transaction.Commit();
return 1;
}
catch (TransactionException) { return 0; }
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
After defending his
PhD thesis in 2005 (computational nuclear physics) at Vanderbilt in Nashville, the author decided to pursue a career in software development. As a long time open source advocate, he started with writing web applications using Linux-Apache-MySql-P (LAMP) framework. After that experience, he decided to embrace Microsoft technologies.
Currently working as a web developer in .NET platform in the online payments company.