Repository navigation
Expand file tree
/
Copy pathCartRepository.cs
More file actions
87 lines (73 loc) · 2.75 KB
/
Copy pathCartRepository.cs
File metadata and controls
87 lines (73 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using PaystackSampleWebApp.Models.DataModel;
using PaystackSampleWebApp.Models.Interfaces;
using PaystackSampleWebApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PaystackSampleWebApp.Models.Repository
{
public class CartRepository : BaseRepository<ShoppingCartEntities>, ICart
{
public bool AddToCart(Cart cart)
{
var existingItem = DataContext.Carts.Where(x => x.ProductId == cart.ProductId);
if (existingItem.Any())
{
existingItem.First().Count += 1;
DataContext.SaveChanges();
return true;
}
else
{
DataContext.Carts.Add(cart);
return DataContext.SaveChanges() > 0;
}
}
public Cart GetCartItem(long itemId)
{
return DataContext.Carts.FirstOrDefault(x => x.RecordId == itemId);
}
public IEnumerable<Cart> GetCartItems()
{
return DataContext.Carts;
}
public string GetCartId(HttpContextBase context)
{
if (context.Request.Cookies["CartId"] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
HttpCookie aCookie = new HttpCookie("CartId");
aCookie.Value = context.User.Identity.Name;
aCookie.Expires = DateTime.Now.AddDays(4);
context.Response.Cookies.Add(aCookie);
}
else
{
Guid tempCartId = Guid.NewGuid();
HttpCookie aCookie = new HttpCookie("CartId");
aCookie.Value = tempCartId.ToString();
aCookie.Expires = DateTime.Now.AddDays(4);
context.Response.Cookies.Add(aCookie);
}
}
var fCookie = context.Request.Cookies["CartId"];
return context.Request.Cookies["CartId"].Value.ToString();
}
public IEnumerable<CartViewModel> GetUserCartItems(string cartId)
{
return from ct in DataContext.Carts join p in DataContext.Products
on ct.ProductId equals p.PrpductId
where ct.CartId==cartId
select new CartViewModel
{
price = p.Price,
productId = ct.ProductId,
productImage = p.ProductImage,
productName =p.ProductName,
quantity =ct.Count
};
}
}
}