-
Notifications
You must be signed in to change notification settings - Fork 159
/
ShopController.cs
76 lines (68 loc) · 3.1 KB
/
ShopController.cs
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
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using AuthPermissions.AspNetCore;
using Example4.MvcWebApp.IndividualAccounts.PermissionsCode;
using Example4.ShopCode.Dtos;
using Example4.ShopCode.EfCoreClasses;
using GenericServices;
using GenericServices.AspNetCore;
using Microsoft.AspNetCore.Mvc;
namespace Example4.MvcWebApp.IndividualAccounts.Controllers
{
public class ShopController : Controller
{
[HttpGet]
[HasPermission(Example4Permissions.SalesSell)]
public IActionResult Till([FromServices] ICrudServices service)
{
var dto = new SellItemDto();
dto.SetResetDto(service.ReadManyNoTracked<StockSelectDto>().ToList());
return View(dto);
}
[HttpPost]
[ValidateAntiForgeryToken]
[HasPermission(Example4Permissions.SalesSell)]
public IActionResult Till([FromServices] ICrudServices service, SellItemDto dto)
{
if (!ModelState.IsValid)
{
dto.SetResetDto(service.ReadManyNoTracked<StockSelectDto>().ToList());
return View(dto);
}
var stockToBuy = service.ReadSingle<ShopStock>(dto.ShopStockId);
var status = ShopSale.CreateSellAndUpdateStock(dto.NumBought, stockToBuy, null);
if (status.IsValid)
{
service.Context.Add(status.Result);
service.Context.SaveChanges();
return RedirectToAction("BuySuccess", new { message = status.Message, status.Result.ShopSaleId });
}
//Error state
service.CopyErrorsToModelState(ModelState, dto);
dto.SetResetDto(service.ReadManyNoTracked<StockSelectDto>().ToList());
return View(dto);
}
public IActionResult BuySuccess([FromServices] ICrudServices service, string message, int shopSaleId)
{
var saleInfo = service.ReadSingle<ListSalesDto>(shopSaleId);
return View(new Tuple<ListSalesDto, string>(saleInfo, message));
}
[HasPermission(Example4Permissions.StockRead)]
public IActionResult Stock([FromServices] ICrudServices service)
{
var allStock = service.ReadManyNoTracked<ListStockDto>().ToList();
var allTheSameShop = allStock.Any() && allStock.All(x => x.ShopShortName == allStock.First().ShopShortName);
return View(new Tuple<List<ListStockDto>, bool>(allStock, allTheSameShop));
}
[HasPermission(Example4Permissions.SalesRead)]
public IActionResult Sales([FromServices] ICrudServices service)
{
var allSales = service.ReadManyNoTracked<ListSalesDto>().ToList();
var allTheSameShop = allSales.Any() && allSales.All(x => x.StockItemShopShortName == allSales.First().StockItemShopShortName);
return View(new Tuple<List<ListSalesDto>, bool>(allSales, allTheSameShop));
}
}
}