PDA

View Full Version : گفتگو: سبد خرید



minaforotan
دوشنبه 12 خرداد 1393, 13:58 عصر
سلام دوستان
سبد خرید بخش مهمی تو هر فروشگاهه . و تو سایت asp.net یکسری آموزش ها داده شده که به نظر من جالب و کافیه
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8

اما متاسفانه من و خیلی های دیگه اونقدر زبانمون خوب نیست که بتونیم مطلب رو به طور کامل درک کنیم
حالا میخوام همین قسمت سبد خرید رو بر مبنای مطالب سایت asp.net در اینجا جوری بیان کنیم که کمک بزرگی به من و تمام کسانی که مثل من هستن بشه
برای همین از همه اساتید و کسایی که به طور حرفه ای کار کردن خواهش میکنم علم خودشون رو ازمون دریغ نکنن و کمکی در این زمینه به ما بکنند

مثال در مورد فروشگاهی است که آلبوم های موسیقی رو میفروشه
کلاس آلبوم به این صورته


namespace MvcMusicStore.Models
{
public class Album
{
public int AlbumId { get; set;
}
public int GenreId { get; set; }
public int ArtistId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string AlbumArtUrl { get; set; }
public Genre Genre { get; set; }
public Artist Artist { get; set; }
}
}



و هر آلبوم نوعی داره مثلا پاپ،راک،جاز و...
و هر نوع میتونه واسه چندین آلبوم باشه


namespace MvcMusicStore.Models
{
public partial class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
}



وهمچنین هر آلبوم مال یک هنرمند هستش و هر هنرمند هم میتونه چندین آلبوم داشته باشه


namespace MvcMusicStore.Models
{
public class Artist
{
public int ArtistId { get; set; }
public string Name { get; set; }
}
}

minaforotan
دوشنبه 12 خرداد 1393, 14:04 عصر
حالا برای سبد خرید کلاس های زیرو میسازیم


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

namespace MvcMusicStore.Models
{
public partial class ShoppingCart
{
MusicStoreEntities storeDB = new MusicStoreEntities();
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;
}


// Helper method to simplify shopping cart calls
public static ShoppingCart GetCart(Controller controller)
{
return GetCart(controller.HttpContext);
}


public void AddToCart(Album album)
{
// Get the matching cart and album instances
var cartItem = storeDB.Carts.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.AlbumId == album.AlbumId);

if (cartItem == null)
{
// Create a new cart item if no cart item exists
cartItem = new Cart
{
AlbumId = album.AlbumId,
CartId = ShoppingCartId,
Count = 1,
DateCreated = DateTime.Now
};
storeDB.Carts.Add(cartItem);
}
else
{
// If the item does exist in the cart,
// then add one to the quantity
cartItem.Count++;
}
// Save changes
storeDB.SaveChanges();
}



public int RemoveFromCart(int id)
{
// Get the cart
var cartItem = storeDB.Carts.Single(
cart => cart.CartId == ShoppingCartId
&& cart.RecordId == id);

int itemCount = 0;

if (cartItem != null)
{
if (cartItem.Count > 1)
{
cartItem.Count--;
itemCount = cartItem.Count;
}
else
{
storeDB.Carts.Remove(cartItem);
}
// Save changes
storeDB.SaveChanges();
}
return itemCount;
}



public void EmptyCart()
{
var cartItems = storeDB.Carts.Where(
cart => cart.CartId == ShoppingCartId);

foreach (var cartItem in cartItems)
{
storeDB.Carts.Remove(cartItem);
}
// Save changes
storeDB.SaveChanges();
}



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



public int GetCount()
{
// Get the count of each item in the cart and sum them up
int? count = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count).Sum();
// Return 0 if all entries are null
return count ?? 0;
}


public decimal GetTotal()
{
// Multiply album price by count of that album to get
// the current price for each of those albums in the cart
// sum all album price totals to get the cart total
decimal? total = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count *
cartItems.Album.Price).Sum();

return total ?? decimal.Zero;
}



public int CreateOrder(Order order)
{
decimal orderTotal = 0;

var cartItems = GetCartItems();
// Iterate over the items in the cart,
// adding the order details for each
foreach (var item in cartItems)
{
var orderDetail = new OrderDetail
{
AlbumId = item.AlbumId,
OrderId = order.OrderId,
UnitPrice = item.Album.Price,
Quantity = item.Count
};
// Set the order total of the shopping cart
orderTotal += (item.Count * item.Album.Price);

storeDB.OrderDetails.Add(orderDetail);

}
// Set the order's total to the orderTotal count
order.Total = orderTotal;

// Save the order
storeDB.SaveChanges();
// Empty the shopping cart
EmptyCart();
// Return the OrderId as the confirmation number
return order.OrderId;
}



// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity. Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}




// When a user has logged in, migrate their shopping cart to
// be associated with their username
public void MigrateCart(string userName)
{
var shoppingCart = storeDB.Carts.Where(
c => c.CartId == ShoppingCartId);

foreach (Cart item in shoppingCart)
{
item.CartId = userName;
}
storeDB.SaveChanges();
}
}
}

حالا از شما دوستان میخوام که یکی یکی در مورد قسمت های مختلف این کلاس صحبت کنیم و ببینیم کارش چیه و آیا بهتر از این هم میشه یا نه:تشویق: