source: KernelRecordsMVC.Web/Controllers/WishlistController.cs@ 595d2e5

main
Last change on this file since 595d2e5 was d89d25b, checked in by mmilevski <markomilevski3@…>, 4 weeks ago

Added few implementaions, minor UI changes.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1using KernelRecordsMVC.Infrastructure.Data;
2using KernelRecordsMVC.Models;
3using Microsoft.AspNetCore.Mvc;
4using Microsoft.EntityFrameworkCore;
5
6namespace KernelRecordsMVC.Web.Controllers;
7
8public class WishlistController : Controller
9{
10 private readonly KernelRecordsContext _context;
11
12 public WishlistController(KernelRecordsContext context)
13 {
14 _context = context;
15 }
16
17 // ==========================================
18 // WISHLIST
19 // ==========================================
20
21 [HttpGet]
22 public IActionResult Index()
23 {
24 var userId = GetUserId();
25
26 if (userId == null)
27 {
28 return RedirectToAction(
29 "Login",
30 "Account");
31 }
32
33 var wishlist = _context.Wishlists
34 .Include(w => w.WishlistProducts)
35 .ThenInclude(wp => wp.Product)
36 .ThenInclude(p => p.Release)
37 .FirstOrDefault(w => w.UserId == userId.Value);
38
39 if (wishlist == null)
40 {
41 return View(new Wishlist());
42 }
43
44 return View(wishlist);
45 }
46
47
48 // ==========================================
49 // ADD TO WISHLIST
50 // ==========================================
51
52 [HttpPost]
53 [ValidateAntiForgeryToken]
54 public IActionResult Add(long productId)
55 {
56 var sessionUserId =
57 HttpContext.Session.GetInt32("UserId");
58
59 if (!sessionUserId.HasValue)
60 {
61 return RedirectToAction(
62 "Login",
63 "Account");
64 }
65
66 var userId =
67 (long)sessionUserId.Value;
68
69
70 var product =
71 _context.Products
72 .FirstOrDefault(p =>
73 p.ProductId == productId);
74
75 if (product == null)
76 return NotFound();
77
78
79 var wishlist =
80 _context.Wishlists
81 .FirstOrDefault(w =>
82 w.UserId == userId);
83
84
85 // Create wishlist if user doesn't have one yet.
86 if (wishlist == null)
87 {
88 wishlist = new Wishlist
89 {
90 WishlistId =
91 GetNextWishlistId(),
92
93 UserId =
94 userId
95 };
96
97
98 _context.Wishlists.Add(
99 wishlist);
100
101 _context.SaveChanges();
102 }
103
104
105 // Prevent duplicate product.
106 var alreadyExists =
107 _context.WishlistProducts
108 .Any(wp =>
109 wp.WishlistId ==
110 wishlist.WishlistId &&
111 wp.ProductId ==
112 productId);
113
114
115 if (!alreadyExists)
116 {
117 var wishlistProduct =
118 new WishlistProduct
119 {
120 WishlistId =
121 wishlist.WishlistId,
122
123 ProductId =
124 productId
125 };
126
127
128 _context.WishlistProducts.Add(
129 wishlistProduct);
130
131 _context.SaveChanges();
132 }
133
134
135 TempData["Success"] =
136 "Product added to your wishlist.";
137
138
139 return RedirectToAction(
140 nameof(Index));
141 }
142
143
144 // ==========================================
145 // REMOVE FROM WISHLIST
146 // ==========================================
147
148 [HttpPost]
149 [ValidateAntiForgeryToken]
150 public IActionResult Remove(long productId)
151 {
152 var userId = GetUserId();
153
154 if (userId == null)
155 {
156 return RedirectToAction(
157 "Login",
158 "Account");
159 }
160
161 var item = _context.WishlistProducts
162 .Include(x => x.Wishlist)
163 .FirstOrDefault(x =>
164 x.ProductId == productId &&
165 x.Wishlist.UserId == userId.Value);
166
167 if (item != null)
168 {
169 _context.WishlistProducts.Remove(item);
170 _context.SaveChanges();
171
172 TempData["Success"] =
173 "Product removed from your wishlist.";
174 }
175
176 return RedirectToAction(nameof(Index));
177 }
178
179
180 // ==========================================
181 // MOVE WISHLIST PRODUCT TO CART
182 // ==========================================
183
184 [HttpPost]
185 [ValidateAntiForgeryToken]
186 public IActionResult AddToCart(long productId)
187 {
188 var userId = GetUserId();
189
190 if (userId == null)
191 {
192 return RedirectToAction(
193 "Login",
194 "Account");
195 }
196
197 var product = _context.Products
198 .Include(p => p.Release)
199 .FirstOrDefault(p => p.ProductId == productId);
200
201 if (product == null)
202 return NotFound();
203
204 if (product.Stock <= 0)
205 {
206 TempData["Error"] =
207 "This product is currently out of stock.";
208
209 return RedirectToAction(nameof(Index));
210 }
211
212 return RedirectToAction(
213 "AddProduct",
214 "Order",
215 new { id = productId });
216 }
217
218
219 // ==========================================
220 // USER ID
221 // ==========================================
222
223 private long? GetUserId()
224 {
225 var userId = HttpContext.Session.GetInt32("UserId");
226
227 if (userId.HasValue)
228 return userId.Value;
229
230 return null;
231 }
232
233
234 // ==========================================
235 // WISHLIST ID
236 // ==========================================
237
238 private long GetNextWishlistId()
239 {
240 var maxId = _context.Wishlists
241 .Select(x => (long?)x.WishlistId)
242 .Max();
243
244 return (maxId ?? 0) + 1;
245 }
246}
Note: See TracBrowser for help on using the repository browser.