{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IItemBL, ItemBL>();
container.RegisterType<ICartBL, CartBL>();
container.RegisterType<IItemDAO, ItemDAO>();
container.RegisterType<ICartDAO, CartDAO>();
container.RegisterType<AccountController>(new InjectionConstructor());
//container.RegisterType<RolesAdminController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());
//container.RegisterType<UsersAdminController>(new InjectionConstructor());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Ism.ShoppingCart.DataLayer;
using Ism.ShoppingCart.Domain;
using Ism.ShoppingCart.BussinessLogic.Contracts;
namespace ISMShoppingCart.Controllers
{
public class CartController : Controller
{
private IItemBL iItemBL;
private ICartBL iCartBL;
public CartController(IItemBL iItemBL, ICartBL iCartBL)
{
this.iItemBL = iItemBL;
this.iCartBL = iCartBL;
}
// GET: Cart
public ActionResult Index()
{
HttpCookie getCookie = HttpContext.Request.Cookies["CartID"];
if (getCookie != null)
{
string cartId = getCookie.Value;
Guid cId = new Guid(cartId);
return View(this.iCartBL.GetCartItemByCartID(cId));
}
return View(this.iCartBL.GetCartItemByCartID(new Guid()));
}
public ActionResult AddtoCart(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ItemModel itemModel = this.iItemBL.GetItemByID(id);
if (itemModel == null)
{
return HttpNotFound();
}
return View(itemModel);
}
// POST: Item/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult AddtoCart([Bind(Include = "ID,ItemName,Price,Qty,OrderQty,CategoryID,Active")] ItemModel itemModel)
{
if (ModelState.IsValid)
{
if (itemModel.OrderQty <= itemModel.Qty)
{
HttpCookie getCookie = HttpContext.Request.Cookies["CartID"];
if (getCookie == null)
{
Guid cartId = Guid.NewGuid();
this.iCartBL.CreatCart(new CartModel
{
CartID = cartId,
CreatedDate = System.DateTime.Now,
Status = "0"
});
this.iCartBL.InsertCartItems(new CartItemModel
{
CartID = cartId,
ItemID = itemModel.ID,
Qty = itemModel.OrderQty,
});
HttpCookie Cookie = new HttpCookie("CartID", cartId.ToString());
HttpContext.Response.Cookies.Add(Cookie);
}
else
{
string cartId = getCookie.Value;
Guid cId = new Guid(cartId);
this.iCartBL.InsertCartItems(new CartItemModel
{
CartID = cId,
ItemID = itemModel.ID,
Qty = itemModel.OrderQty,
});
}
return RedirectToAction("Index");
}
}
return View(itemModel);
}
// GET: Cart/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
this.iCartBL.DeleteCartItem(id);
return RedirectToAction("Index");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CancelOrder()
{
HttpCookie getCookie = HttpContext.Request.Cookies["CartID"];
if (getCookie != null)
{
string cartId = getCookie.Value;
Guid cId = new Guid(cartId);
this.iCartBL.CancelOrder(cId);
Response.Cookies["CartID"].Expires = DateTime.Now.AddDays(-1);
}
return RedirectToAction("Index", "Item");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ConfirmOrder()
{
HttpCookie getCookie = HttpContext.Request.Cookies["CartID"];
if (getCookie != null)
{
string cartId = getCookie.Value;
Guid cId = new Guid(cartId);
this.iCartBL.ConfirmOrder(cId);
Response.Cookies["CartID"].Expires = DateTime.Now.AddDays(-1);
ViewBag.Message = "Your Order Successfully Saved";
}
return View("Index");
}
}
}
public class ItemController : Controller
{
private IItemBL iItemBL;
public ItemController(IItemBL iItemBL, ICartBL iCartBL)
{
this.iItemBL = iItemBL;
}
[HandleError]
public ActionResult Index(int? id)
{
ItemListViewModel model = new ItemListViewModel();
ViewBag.CategoryID = new SelectList(this.iItemBL.GetCategories(), "ID", "CategoryName");
model.ItemModel = this.iItemBL.GetAllItems(id);
return View(model);
}
[HttpPost]
[HandleError]
public ActionResult Index(ItemListViewModel itemModel)
{
int? catId = itemModel.CategoryID != null ? itemModel.CategoryID : null;
ItemListViewModel model = new ItemListViewModel();
ViewBag.CategoryID = new SelectList(this.iItemBL.GetCategories(), "ID", "CategoryName");
model.ItemModel = this.iItemBL.GetAllItems(catId);
return View(model);
}
}
No comments:
Post a Comment