-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCartController.cs
More file actions
167 lines (145 loc) · 6.18 KB
/
CartController.cs
File metadata and controls
167 lines (145 loc) · 6.18 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Catalog.Models;
using SimplCommerce.Module.Core.Extensions;
using SimplCommerce.Module.Core.Services;
using SimplCommerce.Module.Pricing.Services;
using SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels;
using SimplCommerce.Module.ShoppingCart.Models;
using SimplCommerce.Module.ShoppingCart.Services;
namespace SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.Controllers
{
[Area("ShoppingCart")]
[ApiExplorerSettings(IgnoreApi = true)]
public class CartController : Controller
{
private readonly IRepository<CartItem> _cartItemRepository;
private readonly ICartService _cartService;
private readonly IMediaService _mediaService;
private readonly IWorkContext _workContext;
private readonly ICurrencyService _currencyService;
private readonly IStringLocalizer _localizer;
public CartController(
IRepository<CartItem> cartItemRepository,
ICartService cartService,
IMediaService mediaService,
IWorkContext workContext,
ICurrencyService currencyService,
IStringLocalizerFactory stringLocalizerFactory)
{
_cartItemRepository = cartItemRepository;
_cartService = cartService;
_mediaService = mediaService;
_workContext = workContext;
_currencyService = currencyService;
_localizer = stringLocalizerFactory.Create(null);
}
[HttpPost("cart/add-item")]
public async Task<IActionResult> AddToCart([FromBody] AddToCartModel model)
{
var currentUser = await _workContext.GetCurrentUser();
var result = await _cartService.AddToCart(currentUser.Id, model.ProductId, model.Quantity);
if (result.Success)
{
return RedirectToAction("AddToCartResult", new { productId = model.ProductId, categoryName = model.CategoryName });
}
else
{
return Ok(result);
}
}
[HttpGet("cart/add-item-result")]
public async Task<IActionResult> AddToCartResult(long productId,string categoryName)
{
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetCartDetails(currentUser.Id);
var model = new AddToCartResultVm(_currencyService)
{
CartItemCount = cart.Items.Count,
CartAmount = cart.SubTotal
};
var addedProduct = cart.Items.First(x => x.ProductId == productId);
model.ProductName = addedProduct.ProductName;
model.ProductImage = addedProduct.ProductImage;
model.ProductPrice = addedProduct.ProductPrice;
model.CalculatedProductPrice = addedProduct.CalculatedProductPrice;
model.Quantity = addedProduct.Quantity;
model.CategoryName = categoryName;
return PartialView(model);
}
[HttpGet("cart")]
public IActionResult Index()
{
return View();
}
[HttpGet("cart/list")]
public async Task<IActionResult> List()
{
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetCartDetails(currentUser.Id);
if(cart == null)
{
cart = new CartVm(_currencyService);
}
return Json(cart);
}
[HttpPost("cart/update-item-quantity")]
public async Task<IActionResult> UpdateQuantity([FromBody] CartQuantityUpdate model)
{
if(model.Quantity <= 0)
{
return Ok(new { Error = true, Message = _localizer["The quantity must be larger than zero"].Value });
}
var currentUser = await _workContext.GetCurrentUser();
var cartItem = _cartItemRepository.Query().Include(x => x.Product).FirstOrDefault(x => x.Id == model.CartItemId && x.CustomerId == currentUser.Id);
if (cartItem == null)
{
return NotFound();
}
if(model.Quantity > cartItem.Quantity) // always allow user to descrease the quality
{
if (cartItem.Product.StockTrackingIsEnabled && cartItem.Product.StockQuantity < model.Quantity)
{
return Ok(new { Error = true, Message = _localizer["There are only {0} items available for {1}.", cartItem.Product.StockQuantity, cartItem.Product.Name].Value });
}
}
cartItem.Quantity = model.Quantity;
_cartItemRepository.SaveChanges();
return await List();
}
[HttpPost("cart/apply-coupon")]
public async Task<IActionResult> ApplyCoupon([FromBody] ApplyCouponForm model)
{
var currentUser = await _workContext.GetCurrentUser();
var validationResult = await _cartService.ApplyCoupon(currentUser.Id, model.CouponCode);
if (validationResult.Succeeded)
{
var cartVm = await _cartService.GetCartDetails(currentUser.Id);
cartVm.Discount = validationResult.DiscountAmount;
return Json(cartVm);
}
return Json(validationResult);
}
[HttpPost("cart/remove-item")]
public async Task<IActionResult> Remove([FromBody] long itemId, string returnUrl)
{
var currentUser = await _workContext.GetCurrentUser();
var cartItem = _cartItemRepository.Query().FirstOrDefault(x => x.Id == itemId && x.CustomerId == currentUser.Id);
if (cartItem == null)
{
return NotFound();
}
_cartItemRepository.Remove(cartItem);
_cartItemRepository.SaveChanges();
if (!string.IsNullOrWhiteSpace(returnUrl))
{
return LocalRedirect(returnUrl);
}
return await List();
}
}
}