-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProductsController.asp
136 lines (102 loc) · 4.82 KB
/
ProductsController.asp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<% Option Explicit %>
<!--#include file="../../include_all.asp"-->
<!--#include file="../../ViewModels/ProductsViewModels.asp"-->
<%
Class ProductsController
Public Model
Public Sub Index
'setup paging
dim page_size : page_size = 10
dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
dim page_count, record_count
set Model = new Index_ViewModel_Class
Model.Title = "Products"
set Model.Products = ProductRepository.FindPaged(empty, "Name", page_size, page_num, page_count, record_count)
Model.CurrentPageNumber = page_num
Model.PageSize = page_size
Model.PageCount = page_count
Model.RecordCount = record_count
%> <!--#include file="../../Views/Products/Index.asp"--> <%
End Sub
' Edit
'---------------------------------------------------------------------------------------------------------------------
Public Sub Edit
dim id : id = CInt(Request.QueryString("Id"))
set Model = new Edit_ViewModel_Class
set Model.Product = ProductRepository.FindById(id)
Model.Categories = CategoryRepository.CategoriesKVArray()
Model.Suppliers = SupplierRepository.SuppliersKVArray()
Model.Title = "Edit Product"
HTMLSecurity.SetAntiCSRFToken "ProductEditForm"
%> <!--#include file="../../Views/Products/Edit.asp"--> <%
End Sub
Public Sub EditPost
MVC.RequirePost
HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt "ProductEditForm", Request.Form("nonce"), "Edit", Array("Id", Request.Form("Id"))
dim product_id : product_id = CInt(Request.Form("Id"))
dim model : set model = ProductRepository.FindById(product_id)
set model = Automapper.AutoMap(Request.Form, model)
model.Validator.Validate
If model.Validator.HasErrors then
FormCache.SerializeForm "EditProduct", Request.Form
Flash.Errors = model.Validator.Errors
MVC.RedirectToActionExt "Edit", Array("Id", product_id)
Else
model.Discontinued = Choice("on" = Request.Form("Discontinued"), true, false)
ProductRepository.Update model
FormCache.ClearForm "EditProduct"
Flash.Success = "Product updated."
MVC.RedirectToAction "Index"
End If
End Sub
' Add
'---------------------------------------------------------------------------------------------------------------------
Public Sub Create
dim form_params : set form_params = FormCache.DeserializeForm("NewProduct")
If Not form_params Is Nothing then
set Model = Automapper.AutoMap(form_params, new Create_ViewModel_Class)
Else
set Model = new Create_ViewModel_Class
End If
Model.Categories = CategoryRepository.CategoriesKVArray()
Model.Suppliers = SupplierRepository.SuppliersKVArray()
HTMLSecurity.SetAntiCSRFToken "ProductsCreateForm"
%> <!--#include file="../../Views/Products/Create.asp"--> <%
End Sub
Public Sub CreatePost
MVC.RequirePost
HTMLSecurity.OnInvalidAntiCSRFTokenRedirectToAction "ProductsCreateForm", Request.Form("nonce"), "Create"
dim new_product_model : set new_product_model = Automapper.AutoMap(Request.Form, new ProductModel_Class)
new_product_model.Validator.Validate
If new_product_model.Validator.HasErrors then
FormCache.SerializeForm "NewProduct", Request.Form
Flash.Errors = new_product_model.Validator.Errors
MVC.RedirectToAction "Create"
Else
ProductRepository.AddNew new_product_model
FormCache.ClearForm "NewProduct"
Flash.Success = "Product added."
MVC.RedirectToAction "Index"
End If
End Sub
' Delete
'---------------------------------------------------------------------------------------------------------------------
Public Sub Delete
dim id : id = CInt(Request.QueryString("Id"))
set Model = new Delete_ViewModel_Class
set Model.Product = ProductRepository.FindById(id)
Model.Title = "Delete Product"
HTMLSecurity.SetAntiCSRFToken "ProductsDeleteForm"
%> <!--#include file="../../Views/Products/Delete.asp"--> <%
End Sub
Public Sub DeletePost
MVC.RequirePost
HTMLSecurity.OnInvalidAntiCSRFTokenRedirectToAction "ProductsDeleteForm", Request.Form("nonce"), "Create"
dim id : id = CInt(Request.Form("Id"))
ProductRepository.Delete id
Flash.Success = "Product deleted."
MVC.RedirectToAction "Index"
End Sub
End Class
MVC.Dispatch
%>