Online Restaurant Website Part 32 Shopping Cart Database Designing
Hi, Dear's here we learn how to implement "Pizza Restaurant Drink" Website in Visual S
tudio using C# ASP.NET MVC. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials
In this video we are going to implement Shopping Cart Back End(Database designing) for more details click here:
watch vedioFirst we are going to open database to add shopping cart tables. so one by one table code mention below:
> CartTable :
USE [PizzaRestaurentandDrinksDb]
GO
CREATE TABLE [dbo].[CartTable](
[CartID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[UserID] [int] NOT NULL)
> CartItemDetailTable :
USE [PizzaRestaurentandDrinksDb]
GO
CREATE TABLE [dbo].[CartItemDetailTable](
[CartItemID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[CartID] [int] NOT NULL,
[StockItemID] [int] NOT NULL,
[Qty] [int] NOT NULL)
> CartDealTable:
USE [PizzaRestaurentandDrinksDb]
GO
CREATE TABLE [dbo].[CartDealTable](
[CartDealID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[CartID] [int] NOT NULL,
[StockDealID] [int] NOT NULL,
[Qty] [int] NOT NULL)
Once table is created successfully then create the ERD diagram with relationship of shopping cart.
Next we are going to update entity data model in database layer.
Next we are going to update entity data model in database layer to add CartTable , CartItemDetailTable , CartDealTable :
Next create "Order" controller :
public class OrderController : Controller
{
PizzaRestaurentandDrinksDbEntities Db = new PizzaRestaurentandDrinksDbEntities();
public ActionResult Cart_AddItem(int? itemid, int? qty, string itemtype, string return_url)
{
bool result = false;
if (string.IsNullOrEmpty(Convert.ToString(Session["UserTypeID"])))
{
result = false;
return Json(result, JsonRequestBehavior.AllowGet);
}
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
using (var transaction = Db.Database.BeginTransaction())
{
try
{
var cart = Db.CartTables.Where(u => u.UserID == userid).FirstOrDefault();
if (cart == null)
{
var create_cart = new CartTable()
{
UserID = userid
};
Db.CartTables.Add(create_cart);
Db.SaveChanges();
cart = Db.CartTables.Where(u => u.UserID == userid).FirstOrDefault();
}
if (itemtype == "item")
{
var item = Db.CartItemDetailTables.Where(i => i.StockItemID == itemid && i.CartID == cart.CartID).FirstOrDefault();
if (item != null)
{
item.Qty = item.Qty + 1;
Db.Entry(item).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
}
else
{
var create_item = new CartItemDetailTable()
{
CartID = cart.CartID,
StockItemID = Convert.ToInt32(itemid),
Qty = Convert.ToInt32(qty)
};
Db.CartItemDetailTables.Add(create_item);
Db.SaveChanges();
}
}
else
{
var deal = Db.CartDealTables.Where(d => d.StockDealID == itemid && d.CartID == cart.CartID).FirstOrDefault();
if (deal != null)
{
deal.Qty = deal.Qty + 1;
Db.Entry(deal).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
}
else
{
var create_deal = new CartDealTable()
{
CartID = cart.CartID,
Qty = Convert.ToInt32(qty),
StockDealID = Convert.ToInt32(itemid)
};
Db.CartDealTables.Add(create_deal);
Db.SaveChanges();
}
}
result = true;
transaction.Commit();
return Json(result, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
result = false;
transaction.Rollback();
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}
Next add code to AllItems View to add item to shopping cart in home controller
AllItems View Code :
// it's javascript method using ajax get method to execute Cart_AddItem action to add item to shopping cart(in database).
<script type="text/javascript">
function additem(id)
{
var url = "/Order/Cart_AddItem?itemid=" + id + "&qty=1&itemtype=item&return_url=''";
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
// we set cache: false because GET requests are often cached by browsers
// IE is particularly aggressive in that respect
cache: false,
data: { },
success: function (result)
{
if (result == true)
{
alert("Added Successfully!")
}
else
{
alert("Plz login and Try-Again!")
}
}
});
}
</script>
// add ADD Cart button to call java script method add item code
<button class="btn btn-theme btn-md" onclick="additem(@item.StockItemID)">Add To Cart</button>
so once all above steps is done then run the application...
Comments
Post a Comment