-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindControllerClass.b4x_excluded
167 lines (152 loc) · 4.39 KB
/
FindControllerClass.b4x_excluded
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
' Find Controller
' Version 1.07
Sub Class_Globals
Private Request As ServletRequest
Private Response As ServletResponse
Private HRM As HttpResponseMessage
Private DB As MiniORM
Private Method As String
Private Version As String
Private RequestURI As String 'ignore
Private Elements() As String
Private ElementLastIndex As Int
Private ApiVersionIndex As Int
Private ControllerIndex As Int
Private ControllerElement As String 'ignore
Private FirstIndex As Int
Private FirstElement As String
Private SecondIndex As Int
Private SecondElement As String
Private ThirdIndex As Int
Private ThirdElement As String
End Sub
Public Sub Initialize (req As ServletRequest, resp As ServletResponse)
Request = req
Response = resp
HRM.Initialize
DB.Initialize(Main.DBOpen, Main.DBEngine)
End Sub
Private Sub ReturnApiResponse
HRM.SimpleResponse = Main.SimpleResponse
WebApiUtils.ReturnHttpResponse(HRM, Response)
End Sub
Private Sub ReturnBadRequest
WebApiUtils.ReturnBadRequest(Response)
End Sub
Private Sub ReturnMethodNotAllow
WebApiUtils.ReturnMethodNotAllow(Response)
End Sub
Private Sub ReturnInvalidKeywordValue
HRM.ResponseCode = 400
HRM.ResponseError = "Invalid keyword value"
End Sub
Private Sub ReturnErrorUnprocessableEntity
WebApiUtils.ReturnErrorUnprocessableEntity(Response)
End Sub
Public Sub RouteApi
Method = Request.Method.ToUpperCase
Elements = WebApiUtils.GetUriElements(Request.RequestURI)
ElementLastIndex = Elements.Length - 1
ApiVersionIndex = Main.Element.ApiVersionIndex
Version = Elements(ApiVersionIndex)
ControllerIndex = Main.Element.ApiControllerIndex
If ElementLastIndex > ControllerIndex Then
FirstIndex = ControllerIndex + 1
FirstElement = Elements(FirstIndex)
End If
If ElementLastIndex > ControllerIndex + 1 Then
SecondIndex = ControllerIndex + 2
SecondElement = Elements(SecondIndex)
End If
If ElementLastIndex > ControllerIndex + 2 Then
ThirdIndex = ControllerIndex + 3
ThirdElement = Elements(ThirdIndex)
End If
Select Method
Case "GET"
RouteGet
Case Else
Log("Unsupported method: " & Method)
ReturnMethodNotAllow
End Select
End Sub
' GET
Private Sub RouteGet
Select Version
Case "v2"
Select ElementLastIndex
Case ThirdIndex
Select FirstElement
Case "category"
GetFindCategory(SecondElement, ThirdElement)
Return
Case "product"
GetFindProduct(SecondElement, ThirdElement)
Return
End Select
End Select
End Select
ReturnBadRequest
End Sub
Private Sub GetFindCategory (keyword As String, value As String)
' #Version = v2
' #Desc = Find Category by name
' #Elements = ["category", ":keyword", ":value"]
Select keyword
Case "category_name", "name"
QueryCategoryByKeyword(Array("UPPER(category_name) LIKE ?"), Array($"%${value.ToUpperCase}%"$))
Case Else
ReturnInvalidKeywordValue
Return
End Select
DB.Close
ReturnApiResponse
End Sub
Private Sub GetFindProduct (keyword As String, value As String)
' #Version = v2
' #Desc = Find Product by id, cid, code or name
' #Elements = ["product", ":keyword", ":value"]
Select keyword
Case "id"
If IsNumber(value) Then
QueryProductByKeyword(Array("p.id = ?"), Array(value))
Else
ReturnErrorUnprocessableEntity
Return
End If
Case "category_id", "cid", "catid"
If IsNumber(value) Then
QueryProductByKeyword(Array("c.id = ?"), Array(value))
Else
ReturnErrorUnprocessableEntity
Return
End If
Case "product_code", "code"
QueryProductByKeyword(Array("p.product_code = ?"), Array(value)) ' product_code is case sensitive
Case "category_name", "category"
QueryProductByKeyword(Array("UPPER(c.category_name) LIKE ?"), Array($"%${value.ToUpperCase}%"$))
Case "product_name", "name"
QueryProductByKeyword(Array("UPPER(p.product_name) LIKE ?"), Array($"%${value.ToUpperCase}%"$))
Case Else
ReturnInvalidKeywordValue
Return
End Select
DB.Close
ReturnApiResponse
End Sub
Private Sub QueryCategoryByKeyword (Condition As List, Value As List)
DB.Table = "tbl_categories"
DB.setWhereValue(Condition, Value)
DB.Query
HRM.ResponseCode = 200
HRM.ResponseData = DB.Results
End Sub
Private Sub QueryProductByKeyword (Condition As List, Value As List)
DB.Table = "tbl_products p"
DB.Select = Array("p.*", "c.category_name")
DB.Join = DB.CreateORMJoin("tbl_categories c", "p.category_id = c.id", "")
DB.setWhereValue(Condition, Value)
DB.Query
HRM.ResponseCode = 200
HRM.ResponseData = DB.Results
End Sub