Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Configuration;
- using MySql.Data.MySqlClient;
- namespace CarServiceLibrary
- {
- public class ShoppingCart
- {
- public List<CartItem> Items { get; set; }
- public decimal Total { get { return this.Items.Sum(item => item.Total); } set { } }
- public CartDetail CartDetail { get; set; }
- public ShoppingCart()
- {
- Items = new List<CartItem>();
- CartDetail = new CartDetail();
- }
- #region Items
- public void AddItem(int itemId, string itemName, int quantity, decimal price)
- {
- var item = this.Items.Where(r => r.ItemId == itemId).SingleOrDefault();
- if (item != null)
- {
- item.Quantity += 1;
- return;
- }
- else
- {
- this.Items.Add(new CartItem(itemId, itemName, quantity, price));
- }
- }
- public void RemoveItem(int itemId)
- {
- var item = this.Items.Single(r => r.ItemId == itemId);
- this.Items.Remove(item);
- }
- public void UpdateQuantity(int itemId, int quantity)
- {
- this.Items.Where(d => d.ItemId == itemId).First().Quantity = quantity;
- }
- #endregion
- public void AddCartDetails(string name, string lastname, string address, string postcode, string post, string phone)
- {
- this.CartDetail.Name = name;
- this.CartDetail.Lastname = lastname;
- this.CartDetail.Address = address;
- this.CartDetail.Postcode = postcode;
- this.CartDetail.Post = post;
- this.CartDetail.Phone = phone;
- }
- public void Checkout()
- {
- try
- {
- string connString = "Server=localhost;Port=3306;Database=ozra;Uid=root; Pwd=;";
- MySqlConnection con = new MySqlConnection(connString);
- con.Open();
- // CarDetails insert
- MySqlCommand cmd = new MySqlCommand();
- cmd.CommandText = "INSERT INTO cartdetails (name, lastname, address, postcode, post, phone) VALUES (@Name, @Lastname, @Address, @Postcode, @Post, @Phone)";
- cmd.Connection = con;
- cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = this.CartDetail.Name;
- cmd.Parameters.Add("@Lastname", MySqlDbType.VarChar).Value = this.CartDetail.Lastname;
- cmd.Parameters.Add("@Address", MySqlDbType.VarChar).Value = this.CartDetail.Address;
- cmd.Parameters.Add("@Postcode", MySqlDbType.VarChar).Value = this.CartDetail.Postcode;
- cmd.Parameters.Add("@Post", MySqlDbType.VarChar).Value = this.CartDetail.Post;
- cmd.Parameters.Add("@Phone", MySqlDbType.VarChar).Value = this.CartDetail.Phone;
- // Izvedemo sql stavek
- cmd.ExecuteNonQuery();
- long cartDetailsId = cmd.LastInsertedId;
- // Cart insert
- cmd = new MySqlCommand();
- cmd.CommandText = "INSERT INTO cart (date, cartdetails_id, totalprice) VALUES (@Date, @CartDetail_id, @Totalprice)";
- cmd.Connection = con;
- cmd.Parameters.AddWithValue("@Date", DateTime.Now);
- cmd.Parameters.Add("@CartDetail_id", MySqlDbType.VarChar).Value = cartDetailsId;
- cmd.Parameters.Add("@Totalprice", MySqlDbType.VarChar).Value = this.Total;
- // Izvedemo sql stavek
- cmd.ExecuteNonQuery();
- long cartId = cmd.LastInsertedId;
- // Cart insert
- cmd = new MySqlCommand();
- cmd.Connection = con;
- foreach (var item in this.Items)
- {
- cmd.CommandText = "INSERT INTO cartitems (quantity, part_id, cart_id, price, totalprice) VALUES (" + "'" + item.Quantity + "','" + item.ItemId + "','" + cartId + "','" + item.Price + "','" + item.Total + "')";
- // Izvedemo sql stavek
- cmd.ExecuteNonQuery();
- }
- con.Close();
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- public class CartItem
- {
- public int ItemId { get; set; }
- public string ItemName { get; set; }
- public int Quantity { get; set; }
- public decimal Price { get; set; }
- public decimal Total { get { return Price * Quantity; } set { } }
- public CartItem(int itemId, string itemName, int quantity, decimal price)
- {
- ItemId = itemId;
- ItemName = itemName;
- Price = price;
- Quantity = quantity;
- }
- }
- public class CartDetail
- {
- public string Name { get; set; }
- public string Lastname { get; set; }
- public string Address { get; set; }
- public string Postcode { get; set; }
- public string Post { get; set; }
- public string Phone { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement