65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
|
||
using TechnoWorld.Services;
|
||
|
||
namespace TechnoWorld.Controllers;
|
||
|
||
public class CartController : Controller
|
||
{
|
||
private readonly CartService _cart;
|
||
|
||
public CartController(CartService cart)
|
||
{
|
||
_cart = cart;
|
||
}
|
||
|
||
[HttpGet]
|
||
public IActionResult Index()
|
||
{
|
||
ViewData["Title"] = "Корзина";
|
||
var items = _cart.GetItems();
|
||
ViewData["Total"] = _cart.GetTotal();
|
||
return View(items);
|
||
}
|
||
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public IActionResult Add(int productId)
|
||
{
|
||
_cart.Add(productId);
|
||
return RedirectToAction(nameof(Index));
|
||
}
|
||
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public IActionResult Update(int productId, int quantity)
|
||
{
|
||
_cart.SetQuantity(productId, quantity);
|
||
return RedirectToAction(nameof(Index));
|
||
}
|
||
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public IActionResult Remove(int productId)
|
||
{
|
||
_cart.Remove(productId);
|
||
return RedirectToAction(nameof(Index));
|
||
}
|
||
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public IActionResult Clear()
|
||
{
|
||
_cart.Clear();
|
||
return RedirectToAction(nameof(Index));
|
||
}
|
||
|
||
[HttpPost]
|
||
[ValidateAntiForgeryToken]
|
||
public IActionResult Checkout()
|
||
{
|
||
_cart.Clear();
|
||
TempData["OrderSuccess"] = "Заказ оформлен! Мы свяжемся с вами для подтверждения.";
|
||
return RedirectToAction(nameof(Index));
|
||
}
|
||
} |