NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _Electronix.Models
{
public class ShoppingCart
{
NorthwindEntities db = new NorthwindEntities();
private string ShoppingCartId { get; set; }

public const string CartSessionKey = "CartId";

public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}

public static ShoppingCart GetCart(Controller controller)
{
return GetCart(controller.HttpContext);
}

public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!String.IsNullOrEmpty(context.User.Identity.Name))
{
context.Session[CartSessionKey] = context.User.Identity.Name;
}
else
{
Guid tempCartID = Guid.NewGuid();
context.Session[CartSessionKey] = tempCartID.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}

public void AddToCart(Product product)
{
var cartItem = db.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId && c.ProductId == product.ProductID);
if (cartItem == null)
{
cartItem = new Cart
{
ProductId = product.ProductID,
CartId = ShoppingCartId,
Count = 1,
DateCreated = DateTime.Now
};
db.Carts.Add(cartItem);
}
else
{
cartItem.Count++;
}
db.SaveChanges();
}

public int RemoveItem(int id)
{
var cartItem = db.Carts.Single(cart => cart.CartId == ShoppingCartId && cart.RecordId == id);
int itemCount = 0;
if (cartItem != null)
{
if (cartItem.Count > 1)
{
cartItem.Count--;
itemCount = (int)cartItem.Count;
}
else
{
db.Carts.Remove(cartItem);
}
db.SaveChanges();
}
return itemCount;
}

public void EmptyCart()
{
var cartItems = db.Carts.Where(cart => cart.CartId == ShoppingCartId);
foreach (var item in cartItems)
{
db.Carts.Remove(item);
}
db.SaveChanges();
}

public List<Cart> GetCartItems()
{
return db.Carts.Where(c => c.CartId == ShoppingCartId).ToList();
}

public int GetCount()
{
int? count = (from cartItems in db.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count).Sum();
return count ?? 0;
}

public decimal GetTotal()
{
decimal? total = (from cartItems in db.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count * cartItems.Product.UnitPrice).Sum();
return total ?? decimal.Zero;
}

public int CreateOrder(Order order)
{
decimal orderTotal = 0;
var cartItems = GetCartItems();
foreach (var item in cartItems)
{
var orderDetail = new OrderDetail
{
OrderID=order.OrderID,
ProductID=(int)item.ProductId,
Quantity = (short)item.Count,
UnitPrice = (int)item.Product.UnitPrice
};
orderTotal += ((int)item.Count * (decimal)item.Product.UnitPrice);
db.OrderDetails.Add(orderDetail);
}

order.Total = orderTotal;
db.SaveChanges();
EmptyCart();
return order.OrderID;
}

public void MigrateCart(string userName)
{
var shoppinCart = db.Carts.Where(c => c.CartId == ShoppingCartId);
foreach (Cart item in shoppinCart)
{
item.CartId = userName;
}
db.SaveChanges();
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.