-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProductRepository.asp
234 lines (189 loc) · 9.2 KB
/
ProductRepository.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<%
'=======================================================================================================================
' Product Model
'=======================================================================================================================
Class ProductModel_Class
Public Validator
Public Class_Get_Properties
Public Id
Public Name
Public UnitPrice
Public UnitsInStock
Public UnitsOnOrder
Public ReorderLevel
Public CategoryId
Public Category
Public CategoryName
Public SupplierId
Public Supplier
Public SupplierName
private m_discontinued
Public Property Get Discontinued
Discontinued = Choice(1 = m_discontinued, true, false)
End Property
Public Property Let Discontinued(v)
'die v
m_discontinued = Choice(true = v, 1, 0)
End Property
Private Sub Class_Initialize
ValidateExists Me, "Name", "Product Name must exist."
ValidateNumeric Me, "UnitPrice", "Unit Price must be numeric."
ValidateNumeric Me, "UnitsInStock", "Units In Stock must be numeric."
ValidateNumeric Me, "UnitsOnOrder", "Units On Order must be numeric."
ValidateNumeric Me, "ReorderLevel", "Reorder Level must be numeric."
Class_Get_Properties = Array("Id", "Name", "CategoryId", "Category", "CategoryName", _
"SupplierId", "Supplier", "SupplierName", "UnitPrice", _
"UnitsInStock", "UnitsOnOrder", "ReorderLevel", "Discontinued")
End Sub
End Class
'=======================================================================================================================
' Product Repository
'=======================================================================================================================
Class ProductRepository_Class
Public Function FindById(id)
dim sql : sql = "SELECT Products.ProductId Id, Products.ProductName Name, Products.CategoryId, c.CategoryName, Products.SupplierId, s.CompanyName SupplierName, " &_
" Products.UnitPrice, Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Products.Discontinued " &_
"FROM Products " &_
"INNER JOIN Categories c ON Products.CategoryId = c.CategoryId " &_
"INNER JOIN Suppliers s ON Products.SupplierId = s.SupplierId " &_
"WHERE Products.ProductId = ?"
dim rs : set rs = DAL.Query(sql, id)
If rs.EOF then
Err.Raise 1, "ProductRepository_Class:FindById", ProductNotFoundException("id", id)
Else
set FindById = Automapper.AutoMap(rs, "ProductModel_Class")
End If
End Function
' List ProductModels
'---------------------------------------------------------------------------------------------------------------------
Public Function GetAll()
set GetAll = Find(empty, "Name")
End Function
Public Function Find(where_kvarray, order_string_or_array)
dim sql : sql = "SELECT Products.ProductId Id, Products.ProductName Name, Products.CategoryId, c.CategoryName, Products.SupplierId, s.CompanyName SupplierName, " &_
" Products.UnitPrice, Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Products.Discontinued " &_
"FROM Products " &_
"INNER JOIN Categories c ON Products.CategoryId = c.CategoryId " &_
"INNER JOIN Suppliers s ON Products.SupplierId = s.SupplierId "
If Not IsEmpty(where_kvarray) then
sql = sql & " WHERE "
dim where_keys, where_values
KVUnzip where_kvarray, where_keys, where_values
dim i
For i = 0 to UBound(where_keys)
If i > 0 then sql = sql & " AND "
sql = sql & " " & where_keys(i) & " "
Next
End If
If Not IsEmpty(order_string_or_array) then
sql = sql & "ORDER BY "
If IsArray(order_string_or_array) then
dim order_array : order_array = order_string_or_array
For i = 0 to UBound(order_array)
If i > 0 then sql = sql & ", "
sql = sql & " " & order_array(i)
Next
Else
sql = sql & order_string_or_array & " "
End If
End If
dim rs : set rs = DAL.Query(sql, where_values)
set Find = ProductList(rs)
Destroy rs
End Function
Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
dim sql : sql = "SELECT Products.ProductId Id, Products.ProductName Name, Products.CategoryId, c.CategoryName, Products.SupplierId, s.CompanyName SupplierName, " &_
" Products.UnitPrice, Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Products.Discontinued " &_
"FROM Products " &_
"INNER JOIN Categories c ON Products.CategoryId = c.CategoryId " &_
"INNER JOIN Suppliers s ON Products.SupplierId = s.SupplierId "
If Not IsEmpty(where_kvarray) then
sql = sql & " WHERE "
dim where_keys, where_values
KVUnzip where_kvarray, where_keys, where_values
dim i
For i = 0 to UBound(where_keys)
If i > 0 then sql = sql & " AND "
sql = sql & " " & where_keys(i) & " "
Next
End If
If Not IsEmpty(order_string_or_array) then
sql = sql & "ORDER BY "
If IsArray(order_string_or_array) then
dim order_array : order_array = order_string_or_array
For i = 0 to UBound(order_array)
If i > 0 then sql = sql & ", "
sql = sql & " " & order_array(i)
Next
Else
sql = sql & order_string_or_array & " "
End If
End If
dim list : set list = new LinkedList_Class
dim rs : set rs = DAL.PagedQuery(sql, where_values, per_page, page_num)
If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
rs.PageSize = per_page
rs.AbsolutePage = page_num
page_count = rs.PageCount
record_count = rs.RecordCount
End If
set FindPaged = PagedProductList(rs, per_page)
Destroy rs
End Function
Private Function ProductList(rs)
dim list : set list = new LinkedList_Class
dim model
Do until rs.EOF
set model = new ProductModel_Class
list.Push Automapper.AutoMap(rs, model)
rs.MoveNext
Loop
set ProductList = list
End Function
Private Function PagedProductList(rs, per_page)
dim list : set list = new LinkedList_Class
dim x : x = 0
Do While x < per_page and Not rs.EOF
list.Push Automapper.AutoMap(rs, new ProductModel_Class)
x = x + 1
rs.MoveNext
Loop
set PagedProductList = list
End Function
Private Function ProductNotFoundException(ByVal field_name, ByVal field_val)
ProductNotFoundException = "Product was not found with " & field_name & " of '" & field_val & "'."
End Function
' Add / Update / Delete ProductModels
'---------------------------------------------------------------------------------------------------------------------
'sets the model.Id on successful insert
Public Sub AddNew(ByRef model)
dim sql : sql = "INSERT INTO Products " &_
"(ProductName, CategoryId, SupplierId, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel) " &_
"VALUES (?, ?, ?, ?, ?, ?, ?)"
DAL.Execute sql, Array(model.Name, model.CategoryId, model.SupplierId, model.UnitPrice, model.UnitsInStock, model.UnitsOnOrder, model.ReorderLevel)
sql = "SELECT TOP 1 ProductId FROM Products ORDER BY ProductId DESC"
dim rs : set rs = DAL.Query(sql, empty)
model.Id = rs("ProductId")
Destroy rs
End Sub
Public Sub Update(model)
dim sql : sql = "UPDATE Products SET ProductName = ?, CategoryId = ?, SupplierId = ?, " &_
"UnitPrice = ?, UnitsInStock = ?, UnitsOnOrder = ?, ReorderLevel = ?, Discontinued = ? " &_
"WHERE ProductId = ?"
DAL.Execute sql, Array(model.Name, model.CategoryId, model.SupplierId, _
model.UnitPrice, model.UnitsInStock, model.UnitsOnOrder, _
model.ReorderLevel, model.Discontinued, model.Id)
End Sub
Public Sub Delete(id)
dim sql : sql = "DELETE FROM Products WHERE ProductId = ?"
DAL.Execute sql, id
End Sub
End Class
dim ProductRepository__Singleton
Function ProductRepository()
If IsEmpty(ProductRepository__Singleton) then
set ProductRepository__Singleton = new ProductRepository_Class
End If
set ProductRepository = ProductRepository__Singleton
End Function
%>