Advertisement
Guest User

raca

a guest
Apr 28th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using System.Configuration;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace CarServiceLibrary
  11. {
  12.     public class ShoppingCart
  13.     {
  14.         public List<CartItem> Items { get; set; }
  15.  
  16.         public decimal Total { get { return this.Items.Sum(item => item.Total); } set { } }
  17.  
  18.         public CartDetail CartDetail { get; set; }
  19.  
  20.  
  21.         public ShoppingCart()
  22.         {
  23.             Items = new List<CartItem>();
  24.             CartDetail = new CartDetail();
  25.         }
  26.  
  27.         #region Items
  28.         public void AddItem(int itemId, string itemName, int quantity, decimal price)
  29.         {
  30.  
  31.             var item = this.Items.Where(r => r.ItemId == itemId).SingleOrDefault();
  32.  
  33.             if (item != null)
  34.             {
  35.                 item.Quantity += 1;
  36.                 return;
  37.             }
  38.             else
  39.             {
  40.                 this.Items.Add(new CartItem(itemId, itemName, quantity, price));
  41.             }
  42.         }
  43.  
  44.         public void RemoveItem(int itemId)
  45.         {
  46.             var item = this.Items.Single(r => r.ItemId == itemId);
  47.             this.Items.Remove(item);
  48.         }
  49.  
  50.         public void UpdateQuantity(int itemId, int quantity)
  51.         {
  52.             this.Items.Where(d => d.ItemId == itemId).First().Quantity = quantity;
  53.         }
  54.         #endregion
  55.  
  56.         public void AddCartDetails(string name, string lastname, string address, string postcode, string post, string phone)
  57.         {
  58.             this.CartDetail.Name = name;
  59.             this.CartDetail.Lastname = lastname;
  60.             this.CartDetail.Address = address;
  61.             this.CartDetail.Postcode = postcode;
  62.             this.CartDetail.Post = post;
  63.             this.CartDetail.Phone = phone;
  64.         }
  65.  
  66.         public void Checkout()
  67.         {
  68.             try
  69.             {
  70.                 string connString = "Server=localhost;Port=3306;Database=ozra;Uid=root; Pwd=;";
  71.  
  72.                 MySqlConnection con = new MySqlConnection(connString);
  73.                 con.Open();
  74.  
  75.                 // CarDetails insert
  76.                 MySqlCommand cmd = new MySqlCommand();
  77.                 cmd.CommandText = "INSERT INTO cartdetails (name, lastname, address, postcode, post, phone)  VALUES (@Name, @Lastname, @Address, @Postcode, @Post, @Phone)";
  78.                 cmd.Connection = con;
  79.  
  80.                 cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = this.CartDetail.Name;
  81.                 cmd.Parameters.Add("@Lastname", MySqlDbType.VarChar).Value = this.CartDetail.Lastname;
  82.                 cmd.Parameters.Add("@Address", MySqlDbType.VarChar).Value = this.CartDetail.Address;
  83.                 cmd.Parameters.Add("@Postcode", MySqlDbType.VarChar).Value = this.CartDetail.Postcode;
  84.                 cmd.Parameters.Add("@Post", MySqlDbType.VarChar).Value = this.CartDetail.Post;
  85.                 cmd.Parameters.Add("@Phone", MySqlDbType.VarChar).Value = this.CartDetail.Phone;
  86.  
  87.                 // Izvedemo sql stavek
  88.                 cmd.ExecuteNonQuery();
  89.  
  90.                 long cartDetailsId = cmd.LastInsertedId;
  91.  
  92.                 // Cart insert
  93.                 cmd = new MySqlCommand();
  94.  
  95.                 cmd.CommandText = "INSERT INTO cart (date, cartdetails_id, totalprice)  VALUES (@Date, @CartDetail_id, @Totalprice)";
  96.                 cmd.Connection = con;
  97.  
  98.                 cmd.Parameters.AddWithValue("@Date", DateTime.Now);
  99.                 cmd.Parameters.Add("@CartDetail_id", MySqlDbType.VarChar).Value = cartDetailsId;
  100.                 cmd.Parameters.Add("@Totalprice", MySqlDbType.VarChar).Value = this.Total;
  101.  
  102.                 // Izvedemo sql stavek
  103.                 cmd.ExecuteNonQuery();
  104.  
  105.                 long cartId = cmd.LastInsertedId;
  106.  
  107.                 // Cart insert
  108.                 cmd = new MySqlCommand();
  109.  
  110.                 cmd.Connection = con;
  111.  
  112.                 foreach (var item in this.Items)
  113.                 {
  114.                     cmd.CommandText = "INSERT INTO cartitems (quantity, part_id, cart_id, price, totalprice)  VALUES (" + "'" + item.Quantity + "','" + item.ItemId + "','" + cartId + "','" + item.Price + "','" + item.Total + "')";
  115.  
  116.                     // Izvedemo sql stavek
  117.                     cmd.ExecuteNonQuery();
  118.                 }
  119.  
  120.                 con.Close();
  121.             }
  122.             catch (Exception)
  123.             {
  124.                 throw;
  125.             }
  126.         }
  127.  
  128.  
  129.     }
  130.  
  131.     public class CartItem
  132.     {
  133.         public int ItemId { get; set; }
  134.         public string ItemName { get; set; }
  135.         public int Quantity { get; set; }
  136.         public decimal Price { get; set; }
  137.         public decimal Total { get { return Price * Quantity; } set { } }
  138.  
  139.         public CartItem(int itemId, string itemName, int quantity, decimal price)
  140.         {
  141.             ItemId = itemId;
  142.             ItemName = itemName;
  143.             Price = price;
  144.             Quantity = quantity;
  145.         }
  146.     }
  147.  
  148.     public class CartDetail
  149.     {
  150.         public string Name { get; set; }
  151.         public string Lastname { get; set; }
  152.         public string Address { get; set; }
  153.         public string Postcode { get; set; }
  154.         public string Post { get; set; }
  155.         public string Phone { get; set; }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement