-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdersController.cs
More file actions
86 lines (72 loc) · 2.7 KB
/
OrdersController.cs
File metadata and controls
86 lines (72 loc) · 2.7 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
using eTickets.Data.Cart;
using eTickets.Data.Services;
using eTickets.Data.Static;
using eTickets.Data.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace eTickets.Controllers
{
[Authorize]
public class OrdersController : Controller
{
private readonly IMoviesService _moviesService;
private readonly ShoppingCart _shoppingCart;
private readonly IOrdersService _ordersService;
public OrdersController(IMoviesService moviesService, ShoppingCart shoppingCart, IOrdersService ordersService)
{
_moviesService = moviesService;
_shoppingCart = shoppingCart;
_ordersService = ordersService;
}
public async Task<IActionResult> Index()
{
string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
string userRole = User.FindFirstValue(ClaimTypes.Role);
var orders = await _ordersService.GetOrdersByUserIdAndRoleAsync(userId, userRole);
return View(orders);
}
public IActionResult ShoppingCart()
{
var items = _shoppingCart.GetShoppingCartItems();
_shoppingCart.ShoppingCartItems = items;
var response = new ShoppingCartVM()
{
ShoppingCart = _shoppingCart,
ShoppingCartTotal = _shoppingCart.GetShoppingCartTotal()
};
return View(response);
}
public async Task<IActionResult> AddItemToShoppingCart(int id)
{
var item = await _moviesService.GetMovieByIdAsync(id);
if (item != null)
{
_shoppingCart.AddItemToCart(item);
}
return RedirectToAction(nameof(ShoppingCart));
}
public async Task<IActionResult> RemoveItemFromShoppingCart(int id)
{
var item = await _moviesService.GetMovieByIdAsync(id);
if (item != null)
{
_shoppingCart.RemoveItemFromCart(item);
}
return RedirectToAction(nameof(ShoppingCart));
}
public async Task<IActionResult> CompleteOrder()
{
var items = _shoppingCart.GetShoppingCartItems();
string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
string userEmailAddress = User.FindFirstValue(ClaimTypes.Email);
await _ordersService.StoreOrderAsync(items, userId, userEmailAddress);
await _shoppingCart.ClearShoppingCartAsync();
return View("OrderCompleted");
}
}
}