source: KernelRecordsMVC.Web/Controllers/AdminController.cs@ 08aefc6

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

Initial commit

  • Property mode set to 100644
File size: 8.4 KB
Line 
1using Microsoft.AspNetCore.Mvc;
2using Microsoft.EntityFrameworkCore;
3using KernelRecordsMVC.Data;
4using KernelRecordsMVC.Models;
5using KernelRecordsMVC.Domain.Enums;
6using KernelRecordsMVC.Application.ViewModels;
7using Microsoft.AspNetCore.Http;
8
9namespace KernelRecordsMVC.Controllers;
10
11public class AdminController : Controller
12{
13 private readonly KernelRecordsContext _context;
14
15 public AdminController(KernelRecordsContext context)
16 {
17 _context = context;
18 }
19
20
21 // ==========================================
22 // ADMIN DASHBOARD
23 // ==========================================
24
25 [HttpGet]
26 public IActionResult Index()
27 {
28 if (!IsAdmin())
29 return Forbid();
30
31 return View();
32 }
33
34
35 // ==========================================
36 // UC008
37 // NEW PRODUCT - GET
38 // ==========================================
39
40 [HttpGet]
41 public IActionResult CreateProduct()
42 {
43 if (!IsProductManager())
44 return Forbid();
45
46 ViewBag.Releases = _context.Releases
47 .OrderBy(x => x.Title)
48 .ToList();
49
50 return View(new CreateProductViewModel());
51 }
52
53
54 // ==========================================
55 // UC008
56 // NEW PRODUCT - POST
57 // ==========================================
58
59 [HttpPost]
60 [ValidateAntiForgeryToken]
61 public IActionResult CreateProduct(
62 CreateProductViewModel model)
63 {
64 if (!IsProductManager())
65 return Forbid();
66
67 if (!ModelState.IsValid)
68 {
69 LoadReleases();
70 return View(model);
71 }
72
73
74 // Make sure the release actually exists.
75 var release = _context.Releases
76 .FirstOrDefault(x =>
77 x.ReleaseId == model.ReleaseId);
78
79 if (release == null)
80 {
81 ModelState.AddModelError(
82 "ReleaseId",
83 "The selected release does not exist.");
84
85 LoadReleases();
86 return View(model);
87 }
88
89
90 // Don't allow duplicate format for
91 // the same release.
92 var alreadyExists = _context.Products.Any(p =>
93 p.ReleaseId == model.ReleaseId &&
94 p.Format == model.Format);
95
96 if (alreadyExists)
97 {
98 ModelState.AddModelError(
99 "Format",
100 "This release already has a product in this format.");
101
102 LoadReleases();
103 return View(model);
104 }
105
106
107 var product = new Product
108 {
109 ProductId = GetNextProductId(),
110
111 ReleaseId = model.ReleaseId,
112
113 Format = model.Format,
114
115 Price = model.Price,
116
117 ProductDescription =
118 model.ProductDescription,
119
120 Stock = model.Stock
121 };
122
123
124 _context.Products.Add(product);
125
126 _context.SaveChanges();
127
128
129 // Record the modification.
130 CreateModification(
131 ModificationType.CREATE,
132 product.ProductId);
133
134
135 TempData["Success"] =
136 "Product created successfully.";
137
138
139 return RedirectToAction(
140 nameof(Index));
141 }
142
143
144 // ==========================================
145 // HELPERS
146 // ==========================================
147
148 private bool IsAdmin()
149 {
150 return HttpContext.Session
151 .GetString("Role") == "Admin";
152 }
153
154
155 private bool IsProductManager()
156 {
157 var role = HttpContext.Session
158 .GetString("Role");
159
160 var adminType = HttpContext.Session
161 .GetString("AdminType");
162
163 return role == "Admin" &&
164 (adminType == "PRODUCT_MANAGER" ||
165 adminType == "SUPER_ADMIN");
166 }
167
168
169 private long? GetCurrentUserId()
170 {
171 var value = HttpContext.Session
172 .GetString("UserId");
173
174 if (long.TryParse(value, out var userId))
175 return userId;
176
177 return null;
178 }
179
180
181 private long GetNextProductId()
182 {
183 var maxId = _context.Products
184 .Select(x => (long?)x.ProductId)
185 .Max();
186
187 return (maxId ?? 0) + 1;
188 }
189
190
191 private void LoadReleases()
192 {
193 ViewBag.Releases = _context.Releases
194 .OrderBy(x => x.Title)
195 .ToList();
196 }
197
198
199 private void CreateModification(
200 ModificationType type,
201 long productId,
202 decimal? discount = null)
203 {
204 var adminId = GetCurrentUserId();
205
206 if (adminId == null)
207 return;
208
209
210 var modification = new Modification
211 {
212 ModificationId =
213 GetNextModificationId(),
214
215 AdminId = adminId.Value,
216
217 DateModified = DateTime.Today,
218
219 TypeOfModification = type,
220
221 Discount = discount
222 };
223
224
225 _context.Modifications.Add(modification);
226
227 _context.SaveChanges();
228
229
230 var modificationProduct =
231 new ModificationProduct
232 {
233 ModificationId =
234 modification.ModificationId,
235
236 ProductId = productId
237 };
238
239
240 _context.ModificationProducts.Add(
241 modificationProduct);
242
243 _context.SaveChanges();
244 }
245
246
247 private long GetNextModificationId()
248 {
249 var maxId = _context.Modifications
250 .Select(x => (long?)x.ModificationId)
251 .Max();
252
253 return (maxId ?? 0) + 1;
254 }
255 // ==========================================
256// UC009
257// MODIFY PRODUCT - GET
258// ==========================================
259
260 [HttpGet]
261 public IActionResult EditProduct(long id)
262 {
263 if (!IsProductManager())
264 return Forbid();
265
266 var product = _context.Products
267 .Include(p => p.Release)
268 .FirstOrDefault(p => p.ProductId == id);
269
270 if (product == null)
271 return NotFound();
272
273 var model = new EditProductViewModel
274 {
275 ProductId = product.ProductId,
276 ReleaseId = product.ReleaseId,
277 ReleaseTitle = product.Release.Title,
278 Format = product.Format,
279 Price = product.Price,
280 ProductDescription = product.ProductDescription,
281 Stock = product.Stock
282 };
283
284 return View(model);
285 }
286 // ==========================================
287// UC009
288// MODIFY PRODUCT - POST
289// ==========================================
290
291[HttpPost]
292[ValidateAntiForgeryToken]
293public IActionResult EditProduct(
294 EditProductViewModel model)
295{
296 if (!IsProductManager())
297 return Forbid();
298
299 var product = _context.Products
300 .Include(p => p.Release)
301 .FirstOrDefault(p =>
302 p.ProductId == model.ProductId);
303
304 if (product == null)
305 return NotFound();
306
307 if (!ModelState.IsValid)
308 {
309 model.ReleaseTitle = product.Release.Title;
310 model.ReleaseId = product.ReleaseId;
311 model.Format = product.Format;
312
313 return View(model);
314 }
315
316
317 // ==========================================
318 // DISCOUNT
319 // ==========================================
320
321 if (model.ModificationType ==
322 ModificationType.DISCOUNT)
323 {
324 if (!model.Discount.HasValue)
325 {
326 ModelState.AddModelError(
327 "Discount",
328 "Please enter a discount percentage.");
329
330 model.ReleaseTitle = product.Release.Title;
331 model.ReleaseId = product.ReleaseId;
332 model.Format = product.Format;
333
334 return View(model);
335 }
336
337 product.Price =
338 product.Price *
339 (1 - model.Discount.Value / 100m);
340 }
341 else
342 {
343 // ==========================================
344 // NORMAL UPDATE
345 // ==========================================
346
347 product.Price = model.Price;
348
349 product.ProductDescription =
350 model.ProductDescription;
351
352 product.Stock = model.Stock;
353 }
354
355
356 _context.SaveChanges();
357
358
359 // ==========================================
360 // RECORD MODIFICATION
361 // ==========================================
362
363 CreateModification(
364 model.ModificationType,
365 product.ProductId,
366 model.Discount);
367
368
369 TempData["Success"] =
370 "Product modified successfully.";
371
372 return RedirectToAction(nameof(Index));
373}
374[HttpGet]
375public IActionResult Products()
376{
377 if (!IsProductManager())
378 return Forbid();
379
380 var products = _context.Products
381 .Include(p => p.Release)
382 .OrderBy(p => p.Release.Title)
383 .ThenBy(p => p.Format)
384 .ToList();
385
386 return View(products);
387}
388}
Note: See TracBrowser for help on using the repository browser.