Repository navigation
Expand file tree
/
Copy pathOrdersController.cs
More file actions
103 lines (91 loc) · 4.1 KB
/
Copy pathOrdersController.cs
File metadata and controls
103 lines (91 loc) · 4.1 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2017 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 BizLogic.Orders;
using DataLayer.EfCode;
using ExampleWebApp.Helpers;
using GenericBizRunner;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceLayer.CheckoutServices.Concrete;
using ServiceLayer.OrderServices;
using ServiceLayer.OrderServices.Concrete;
namespace EfCoreInAction.Controllers
{
public class OrdersController : BaseTraceController
{
private readonly EfCoreContext _context;
public OrdersController(EfCoreContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
var listService = new DisplayOrdersService(_context);
SetupTraceInfo();
return View(listService.GetUsersOrders(HttpContext.Request.Cookies));
}
public IActionResult ConfirmOrder(int orderId, string message)
{
var detailService = new DisplayOrdersService(_context);
SetupTraceInfo();
ViewData["Message"] = message ?? "Your order has been updated.";
return View(detailService.GetOrderDetail(orderId));
}
public IActionResult ChangeDelivery(int id,
[FromServices]IActionService<IChangeDeliverAction> service)
{
//When the DTO is created it will run the SetupSecondaryData method to build the data needed for the display
//For the SetupSecondaryData to work it needs the orderId and UserId, so the GetOriginal method allow you to add these
//via the optional parameter. This sets these properties before the SetupSecondaryData method is called
var dto = service.GetDto<WebChangeDeliveryDto>(x =>
{
x.OrderId = id;
x.UserId = GetUserId(HttpContext);
});
//It is possible that the call to SetupSecondaryData may add an error
//CopyErrorsToModelState will do notthing if there are no errors
service.Status.CopyErrorsToModelState(ModelState, dto);
return View(dto);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangeDelivery(WebChangeDeliveryDto dto,
[FromServices]IActionService<IChangeDeliverAction> service)
{
if (!ModelState.IsValid)
{
//I have to reset the DTO, which will call SetupSecondaryData
//to set up the values needed for the display
service.ResetDto(dto);
//return to the same view to show the errors
return View(dto);
}
//This runs my business logic using the service injected in the
//Action's service parameter
service.RunBizAction(dto);
if (!service.Status.HasErrors)
{
SetupTraceInfo(); //Used to update the logs
//We copy the message from the business logic to show
return RedirectToAction("ConfirmOrder", "Orders",
new { dto.OrderId, message = service.Status.Message });
}
//Otherwise errors, so I need to redisplay the page to the user
service.Status.CopyErrorsToModelState(ModelState, dto);
//I reset the DTO, which will call SetupSecondaryData i set up the display props
service.ResetDto(dto);
SetupTraceInfo(); //Used to update the logs
return View(dto); //redisplay the page, with the errors
}
//---------------------------------------------------------------
//private methods
private string GetUserId(HttpContext httpContext)
{
var checkoutCookie = new CheckoutCookie(httpContext.Request.Cookies, httpContext.Response.Cookies);
var cookieService = new CheckoutCookieService(checkoutCookie.GetValue());
return cookieService.UserId;
}
}
}