homework/Controllers/CartController.cs

65 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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));
}
}