public void InsertCaller()
{
DataSet ds = new DataSet();
ds.ReadXml("D:\\Sample\\Items");
BulkInsertMethod(ds.Tables[1], "Category");
BulkInsertMethod(ds.Tables[1], "Items");
}
public void BulkInsertMethod(DataTable dt, string destinationTable)
{
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(constr))
{
using (SqlBulkCopy bulkcopy =new SqlBulkCopy(sqlcon))
{
bulkcopy.DestinationTableName = destinationTable;
bulkcopy.ColumnMappings.Add("ID", "ItemID");
bulkcopy.ColumnMappings.Add("ItemName", "ItemName");
bulkcopy.ColumnMappings.Add("Price", "Price");
bulkcopy.ColumnMappings.Add("Qty", "Qty");
bulkcopy.ColumnMappings.Add("CategoryID", "CategoryID");
bulkcopy.ColumnMappings.Add("Active", "Active");
sqlcon.Open();
bulkcopy.WriteToServer(dt);
sqlcon.Close();
}
}
}
Thursday, February 1, 2018
Razor
@model Ism.ShoppingCart.Domain.ItemListViewModel
@{
ViewBag.Title = "Index";
}
<h2>Items</h2>
<br />
@using (Html.BeginForm())
{
<div class="form-horizontal">
<h4>Item</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.Label("CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
<input type="submit" id="btnsearch" value="Search" class="btn btn-default" />
</div>
</div>
</div>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Qty)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].CategoryID)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Active)
</th>
<th></th>
</tr>
@foreach (var item in Model.ItemModel) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Active)
</td>
<td>
@Html.ActionLink("AddtoCart", "AddtoCart", "Cart", new { id=item.ID },null)
</td>
</tr>
}
</table>
@{
ViewBag.Title = "Index";
}
<h2>Items</h2>
<br />
@using (Html.BeginForm())
{
<div class="form-horizontal">
<h4>Item</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.Label("CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
<input type="submit" id="btnsearch" value="Search" class="btn btn-default" />
</div>
</div>
</div>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Qty)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].CategoryID)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemModel[0].Active)
</th>
<th></th>
</tr>
@foreach (var item in Model.ItemModel) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Active)
</td>
<td>
@Html.ActionLink("AddtoCart", "AddtoCart", "Cart", new { id=item.ID },null)
</td>
</tr>
}
</table>
@model Ism.ShoppingCart.Domain.ItemModel
@{
ViewBag.Title = "Edit";
}
<h2>Add to Cart</h2>
@using (Html.BeginForm("AddtoCart","Cart"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4></h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ItemName, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Qty, htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.Qty, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Qty, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderQty, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderQty, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderQty, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Item List", "Index", "Item", null, new { @class = "btn btn-success" })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model IEnumerable<Ism.ShoppingCart.Domain.CartViewModel>
@{
ViewBag.Title = "Index";
}
<h2>My Cart Items</h2>
<br />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemID)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemName)
</th>
<th>
@Html.DisplayNameFor(model => model.Qty)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id=item.ID },new { onclick="return confirm('Are you sure you want to delete this?');"})
</td>
</tr>
}
<tfoot>
<tr>
<td>Total Value : </td>
<td>@ViewBag.Total</td>
</tr>
</tfoot>
</table>
<div class="row">
<div class="col-sm-2">
@Html.ActionLink("Continue Shopping", "Index", "Item", null, new { @class = "btn btn-success" })
</div>
<div class="col-sm-2">
@using (Html.BeginForm("CancelOrder", "Cart", FormMethod.Post))
{ @Html.AntiForgeryToken() <input type="submit" value="Cancel Order" class="btn btn-danger" />
}
<br />
</div>
<div class="col-sm-2">
@using (Html.BeginForm("ConfirmOrder", "Cart", FormMethod.Post))
{
@Html.AntiForgeryToken()
<input type="submit" value="Confirm Order" class="btn btn-success" />
}
</div>
</div>
@if (ViewBag.Message != null)
{
<script>
window.onload = function() {
alert("@ViewBag.Message");
};
</script>
}
Constructor Injection
public static class UnityConfig
{
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);
}
}
{
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);
}
}
How to Use AutoMapper
using AutoMapper;
using Ism.ShoppingCart.DataLayer;
using Ism.ShoppingCart.DataLayer.Contracts;
using Ism.ShoppingCart.Domain;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ism.ShoppingCart.DataLayer
{
public class CartDAO : ICartDAO
{
private ISM_DBEntities db = new ISM_DBEntities();
MapperConfiguration cartitemmapper = new MapperConfiguration(config => config.CreateMap<CartItemModel, CartItem>());
private IMapper icartitemmapper;
MapperConfiguration modeltocartmapper = new MapperConfiguration(config => config.CreateMap<CartModel, Cart>());
private IMapper imodeltocartmapper;
public void CreatCart(CartModel model)
{
imodeltocartmapper = modeltocartmapper.CreateMapper();
Cart cart = imodeltocartmapper.Map<CartModel, Cart>(model);
db.Carts.Add(cart);
db.SaveChanges();
}
public void InsertCartItems(CartItemModel model)
{
icartitemmapper = cartitemmapper.CreateMapper();
CartItem cartitem = icartitemmapper.Map<CartItemModel, CartItem>(model);
db.CartItems.Add(cartitem);
db.SaveChanges();
}
public void DeleteCartItem(int? Id)
{
CartItem cartItem = db.CartItems.Find(Id);
db.CartItems.Remove(cartItem);
db.SaveChanges();
}
public List<CartViewModel> GetCartItemByCartID(Guid cartID)
{
List<CartViewModel> itemList = new List<CartViewModel>();
List<CartItem> listitems = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in listitems)
{
CartViewModel obj = new CartViewModel();
obj.CartID = item.CartID;
obj.ID = item.ID;
obj.ItemID = item.ItemID;
obj.ItemName = item.Item.ItemName;
obj.Price = item.Item.Price;
obj.Qty = item.Qty;
itemList.Add(obj);
}
return itemList;
}
public void CancelOrder(Guid cartID)
{
List<CartItem> cartItem = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in cartItem)
{
db.CartItems.Remove(item);
db.SaveChanges();
}
Cart cart = db.Carts.Find(cartID);
db.Carts.Remove(cart);
db.SaveChanges();
}
public void ConfirmOrder(Guid cartID)
{
Cart cart = db.Carts.Find(cartID);
cart.Status="1";
using (var context = new ISM_DBEntities())
{
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
db.Entry(cart).State = EntityState.Modified;
db.SaveChanges();
List<CartItem> cartItem = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in cartItem)
{
Item itemobj = db.Items.Where(x => x.ID == item.ItemID).FirstOrDefault();
itemobj.Qty = itemobj.Qty - item.Qty;
db.Entry(itemobj).State = EntityState.Modified;
db.SaveChanges();
}
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
}
}
}
using Ism.ShoppingCart.DataLayer.Contracts;
using Ism.ShoppingCart.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
namespace Ism.ShoppingCart.DataLayer
{
public class ItemDAO : IItemDAO
{
private ISM_DBEntities db = new ISM_DBEntities();
MapperConfiguration mapper = new MapperConfiguration(config => config.CreateMap<Item, ItemModel>());
private IMapper imapper;
MapperConfiguration categorymapper = new MapperConfiguration(config => config.CreateMap<Category, CategoryModel>());
private IMapper icategorymapper;
public List<ItemModel> GetAllItems(int? id)
{
imapper = mapper.CreateMapper();
List<ItemModel> lst = new List<ItemModel>();
if (id != null)
{
lst = imapper.Map<List<Item>, List<ItemModel>>(db.Items.Where(x => x.CategoryID == id).ToList());
}
else
{
lst = imapper.Map<List<Item>, List<ItemModel>>(db.Items.ToList());
}
return lst;
}
public ItemModel GetItemByID(int? Id)
{
imapper = mapper.CreateMapper();
ItemModel item = imapper.Map<Item, ItemModel>(db.Items.Find(Id));
return item;
}
public List<CategoryModel> GetCategories()
{
icategorymapper = categorymapper.CreateMapper();
return icategorymapper.Map<List<Category>, List<CategoryModel>>(db.Categories.ToList());
}
}
}
using Ism.ShoppingCart.DataLayer;
using Ism.ShoppingCart.DataLayer.Contracts;
using Ism.ShoppingCart.Domain;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ism.ShoppingCart.DataLayer
{
public class CartDAO : ICartDAO
{
private ISM_DBEntities db = new ISM_DBEntities();
MapperConfiguration cartitemmapper = new MapperConfiguration(config => config.CreateMap<CartItemModel, CartItem>());
private IMapper icartitemmapper;
MapperConfiguration modeltocartmapper = new MapperConfiguration(config => config.CreateMap<CartModel, Cart>());
private IMapper imodeltocartmapper;
public void CreatCart(CartModel model)
{
imodeltocartmapper = modeltocartmapper.CreateMapper();
Cart cart = imodeltocartmapper.Map<CartModel, Cart>(model);
db.Carts.Add(cart);
db.SaveChanges();
}
public void InsertCartItems(CartItemModel model)
{
icartitemmapper = cartitemmapper.CreateMapper();
CartItem cartitem = icartitemmapper.Map<CartItemModel, CartItem>(model);
db.CartItems.Add(cartitem);
db.SaveChanges();
}
public void DeleteCartItem(int? Id)
{
CartItem cartItem = db.CartItems.Find(Id);
db.CartItems.Remove(cartItem);
db.SaveChanges();
}
public List<CartViewModel> GetCartItemByCartID(Guid cartID)
{
List<CartViewModel> itemList = new List<CartViewModel>();
List<CartItem> listitems = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in listitems)
{
CartViewModel obj = new CartViewModel();
obj.CartID = item.CartID;
obj.ID = item.ID;
obj.ItemID = item.ItemID;
obj.ItemName = item.Item.ItemName;
obj.Price = item.Item.Price;
obj.Qty = item.Qty;
itemList.Add(obj);
}
return itemList;
}
public void CancelOrder(Guid cartID)
{
List<CartItem> cartItem = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in cartItem)
{
db.CartItems.Remove(item);
db.SaveChanges();
}
Cart cart = db.Carts.Find(cartID);
db.Carts.Remove(cart);
db.SaveChanges();
}
public void ConfirmOrder(Guid cartID)
{
Cart cart = db.Carts.Find(cartID);
cart.Status="1";
using (var context = new ISM_DBEntities())
{
using (DbContextTransaction transaction = context.Database.BeginTransaction())
{
try
{
db.Entry(cart).State = EntityState.Modified;
db.SaveChanges();
List<CartItem> cartItem = db.CartItems.Where(x => x.CartID == cartID).ToList();
foreach (var item in cartItem)
{
Item itemobj = db.Items.Where(x => x.ID == item.ItemID).FirstOrDefault();
itemobj.Qty = itemobj.Qty - item.Qty;
db.Entry(itemobj).State = EntityState.Modified;
db.SaveChanges();
}
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
}
}
}
using Ism.ShoppingCart.DataLayer.Contracts;
using Ism.ShoppingCart.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
namespace Ism.ShoppingCart.DataLayer
{
public class ItemDAO : IItemDAO
{
private ISM_DBEntities db = new ISM_DBEntities();
MapperConfiguration mapper = new MapperConfiguration(config => config.CreateMap<Item, ItemModel>());
private IMapper imapper;
MapperConfiguration categorymapper = new MapperConfiguration(config => config.CreateMap<Category, CategoryModel>());
private IMapper icategorymapper;
public List<ItemModel> GetAllItems(int? id)
{
imapper = mapper.CreateMapper();
List<ItemModel> lst = new List<ItemModel>();
if (id != null)
{
lst = imapper.Map<List<Item>, List<ItemModel>>(db.Items.Where(x => x.CategoryID == id).ToList());
}
else
{
lst = imapper.Map<List<Item>, List<ItemModel>>(db.Items.ToList());
}
return lst;
}
public ItemModel GetItemByID(int? Id)
{
imapper = mapper.CreateMapper();
ItemModel item = imapper.Map<Item, ItemModel>(db.Items.Find(Id));
return item;
}
public List<CategoryModel> GetCategories()
{
icategorymapper = categorymapper.CreateMapper();
return icategorymapper.Map<List<Category>, List<CategoryModel>>(db.Categories.ToList());
}
}
}
Subscribe to:
Comments (Atom)
BulkInsert
public void InsertCaller() { DataSet ds = new DataSet(); ds.ReadXml("D:\\Sample\\Items"); ...
-
@model Ism.ShoppingCart.Domain.ItemListViewModel @{ ViewBag.Title = "Index"; } <h2>Items</h2> <br /> ...
-
public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContaine...
-
Dependency Injection (DI) is a software design pattern that we can develop loosely couple software component. DI also enables us to better m...