| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonDomains.Models.OrderCarts; |
| | | 4 | | using OzonRepositories.Data; |
| | | 5 | | using Services.CacheServices.Cache.CartCache; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Controllers |
| | | 8 | | { |
| | | 9 | | [Area("Studio2")] |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | public class CartController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly OrderCartRepository _orderCartRepository; |
| | | 14 | | private readonly CartCache _cartCache; |
| | | 15 | | |
| | 0 | 16 | | public CartController(OrderCartRepository orderCartRepository, |
| | 0 | 17 | | CartCache cartCache) |
| | | 18 | | { |
| | 0 | 19 | | _orderCartRepository = orderCartRepository; |
| | 0 | 20 | | _cartCache = cartCache; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task<IActionResult> Index(int page = 1) |
| | | 24 | | { |
| | 0 | 25 | | var pagedCarts = await _cartCache.Get(page); |
| | 0 | 26 | | var nextPageData = await _cartCache.Get(page + 1); |
| | | 27 | | |
| | 0 | 28 | | ViewBag.CurrentPage = page; |
| | 0 | 29 | | ViewBag.HasNextPage = nextPageData.Count == _cartCache.GetIncrementCount(); |
| | | 30 | | |
| | 0 | 31 | | int totalItems = await _cartCache.GetTotalCount(); |
| | 0 | 32 | | int itemsPerPage = _cartCache.GetIncrementCount(); |
| | 0 | 33 | | ViewBag.TotalPages = (int)Math.Ceiling((double)totalItems / itemsPerPage); |
| | | 34 | | |
| | 0 | 35 | | return View(pagedCarts); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | |
| | | 39 | | |
| | | 40 | | |
| | | 41 | | [HttpPost] |
| | | 42 | | public async Task<IActionResult> UpdateCache() |
| | | 43 | | { |
| | 0 | 44 | | await _cartCache.Update(); |
| | 0 | 45 | | return RedirectToAction("Index"); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task<IActionResult> Details(int id) |
| | | 49 | | { |
| | 0 | 50 | | var cart = await _orderCartRepository.GetAsync(id); |
| | 0 | 51 | | if (cart == null) return NotFound(); |
| | | 52 | | |
| | 0 | 53 | | return View(cart); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public IActionResult Create() |
| | | 57 | | { |
| | 0 | 58 | | return View(new OrderCart()); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [HttpPost] |
| | | 62 | | [ValidateAntiForgeryToken] |
| | | 63 | | public async Task<IActionResult> Create(OrderCart cart) |
| | | 64 | | { |
| | 0 | 65 | | if (!ModelState.IsValid) |
| | 0 | 66 | | return View(cart); |
| | | 67 | | |
| | 0 | 68 | | await _orderCartRepository.Add(cart); |
| | 0 | 69 | | return RedirectToAction(nameof(Index)); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<IActionResult> Edit(int id) |
| | | 73 | | { |
| | 0 | 74 | | var cart = await _orderCartRepository.GetAsync(id); |
| | 0 | 75 | | if (cart == null) return NotFound(); |
| | | 76 | | |
| | 0 | 77 | | return View(cart); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | [HttpPost] |
| | | 81 | | [ValidateAntiForgeryToken] |
| | | 82 | | public async Task<IActionResult> Edit(int id, OrderCart cart) |
| | | 83 | | { |
| | 0 | 84 | | if (id != cart.Id) return NotFound(); |
| | | 85 | | |
| | 0 | 86 | | if (!ModelState.IsValid) |
| | 0 | 87 | | return View(cart); |
| | | 88 | | |
| | 0 | 89 | | await _orderCartRepository.Update(cart); |
| | 0 | 90 | | return RedirectToAction(nameof(Index)); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | public async Task<IActionResult> Delete(int id) |
| | | 94 | | { |
| | 0 | 95 | | var cart = await _orderCartRepository.GetAsync(id); |
| | 0 | 96 | | if (cart == null) return NotFound(); |
| | | 97 | | |
| | 0 | 98 | | return View(cart); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | [HttpPost, ActionName("Delete")] |
| | | 102 | | [ValidateAntiForgeryToken] |
| | | 103 | | public async Task<IActionResult> DeleteConfirmed(int id) |
| | | 104 | | { |
| | 0 | 105 | | await _orderCartRepository.DeleteAsync(id); |
| | 0 | 106 | | await _cartCache.Update(); |
| | 0 | 107 | | return RedirectToAction(nameof(Index)); |
| | 0 | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |