chyz 1.0.13-rc.4 → 1.0.13-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/BaseChyz.ts +384 -0
  2. package/Chyz.ts +11 -0
  3. package/Examples/Controllers/ApiController.ts +140 -0
  4. package/Examples/Controllers/KeyCloakController.ts +100 -0
  5. package/Examples/Controllers/PublicController.ts +42 -0
  6. package/Examples/Controllers/SiteController.ts +139 -0
  7. package/Examples/Models/Categories.ts +48 -0
  8. package/Examples/Models/Customer.ts +115 -0
  9. package/Examples/Models/KeycloakUser.ts +66 -0
  10. package/Examples/Models/Order.ts +64 -0
  11. package/Examples/Models/OrderItem.ts +27 -0
  12. package/Examples/Models/ProductModels.ts +49 -0
  13. package/Examples/Models/ProductToCategories.ts +46 -0
  14. package/Examples/Models/Products.ts +50 -0
  15. package/Examples/Models/Stocks.ts +60 -0
  16. package/Examples/Models/User.ts +100 -0
  17. package/Examples/Models/UserPermission.ts +37 -0
  18. package/Examples/Models/index.ts +19 -0
  19. package/Examples/index-keycloack.ts +78 -0
  20. package/Examples/index.ts +40 -0
  21. package/Examples/keycloak.json +7 -0
  22. package/Examples/log/access.log +0 -0
  23. package/Examples/log/app.log +6057 -0
  24. package/Examples/log/errors.log +1526 -0
  25. package/Examples/package.json +46 -0
  26. package/Examples/tsconfig.json +21 -0
  27. package/base/ActionFilter.ts +71 -0
  28. package/base/BaseError.ts +26 -0
  29. package/base/BaseObject.ts +19 -0
  30. package/base/Behavior.ts +6 -0
  31. package/base/Component.ts +66 -0
  32. package/base/Configurable.ts +10 -0
  33. package/base/Controller.ts +84 -0
  34. package/base/DataErrorDbException.ts +16 -0
  35. package/base/DbConnection.ts +51 -0
  36. package/base/ForbiddenHttpException.ts +15 -0
  37. package/base/InvalidConfigException.ts +16 -0
  38. package/base/Model.ts +358 -0
  39. package/base/ModelManager.ts +14 -0
  40. package/base/NotFoundHttpException.ts +14 -0
  41. package/base/RestClient.ts +28 -0
  42. package/base/UnauthorizedHttpException.ts +17 -0
  43. package/base/ValidationHttpException.ts +14 -0
  44. package/base/db/Exception.ts +14 -0
  45. package/base/index.ts +17 -0
  46. package/decorator/Middleware.ts +9 -0
  47. package/decorator/controller.ts +22 -0
  48. package/decorator/enums/ControllerDecoratorParams.ts +5 -0
  49. package/decorator/get.ts +37 -0
  50. package/decorator/index.ts +3 -0
  51. package/decorator/post.ts +36 -0
  52. package/{BaseChyz.js → dist/BaseChyz.js} +29 -4
  53. package/dist/BaseChyz.js.map +1 -0
  54. package/{Chyz.js → dist/Chyz.js} +0 -0
  55. package/{Chyz.js.map → dist/Chyz.js.map} +0 -0
  56. package/{base → dist/base}/ActionFilter.js +0 -0
  57. package/{base → dist/base}/ActionFilter.js.map +0 -0
  58. package/{base → dist/base}/BaseError.js +0 -0
  59. package/{base → dist/base}/BaseError.js.map +0 -0
  60. package/{base → dist/base}/BaseObject.js +0 -0
  61. package/{base → dist/base}/BaseObject.js.map +0 -0
  62. package/{base → dist/base}/Behavior.js +0 -0
  63. package/{base → dist/base}/Behavior.js.map +0 -0
  64. package/{base → dist/base}/Component.js +0 -0
  65. package/{base → dist/base}/Component.js.map +0 -0
  66. package/{base → dist/base}/Configurable.js +0 -0
  67. package/{base → dist/base}/Configurable.js.map +0 -0
  68. package/{base → dist/base}/Controller.js +0 -0
  69. package/{base → dist/base}/Controller.js.map +0 -0
  70. package/{base → dist/base}/DataErrorDbException.js +0 -0
  71. package/{base → dist/base}/DataErrorDbException.js.map +0 -0
  72. package/{base → dist/base}/DbConnection.js +0 -0
  73. package/{base → dist/base}/DbConnection.js.map +0 -0
  74. package/{base → dist/base}/ForbiddenHttpException.js +0 -0
  75. package/{base → dist/base}/ForbiddenHttpException.js.map +0 -0
  76. package/{base → dist/base}/InvalidConfigException.js +0 -0
  77. package/{base → dist/base}/InvalidConfigException.js.map +0 -0
  78. package/{base → dist/base}/Model.js +32 -28
  79. package/dist/base/Model.js.map +1 -0
  80. package/dist/base/ModelManager.js +17 -0
  81. package/dist/base/ModelManager.js.map +1 -0
  82. package/{base → dist/base}/NotFoundHttpException.js +0 -0
  83. package/{base → dist/base}/NotFoundHttpException.js.map +0 -0
  84. package/{base → dist/base}/RestClient.js +0 -0
  85. package/{base → dist/base}/RestClient.js.map +0 -0
  86. package/{base → dist/base}/UnauthorizedHttpException.js +0 -0
  87. package/{base → dist/base}/UnauthorizedHttpException.js.map +0 -0
  88. package/{base → dist/base}/ValidationHttpException.js +0 -0
  89. package/{base → dist/base}/ValidationHttpException.js.map +0 -0
  90. package/{base → dist/base}/db/Exception.js +0 -0
  91. package/{base → dist/base}/db/Exception.js.map +0 -0
  92. package/{base → dist/base}/index.js +1 -0
  93. package/{base → dist/base}/index.js.map +1 -1
  94. package/{decorator → dist/decorator}/Middleware.js +0 -0
  95. package/{decorator → dist/decorator}/Middleware.js.map +0 -0
  96. package/{decorator → dist/decorator}/controller.js +0 -0
  97. package/{decorator → dist/decorator}/controller.js.map +0 -0
  98. package/{decorator → dist/decorator}/enums/ControllerDecoratorParams.js +0 -0
  99. package/{decorator → dist/decorator}/enums/ControllerDecoratorParams.js.map +0 -0
  100. package/{decorator → dist/decorator}/get.js +0 -0
  101. package/{decorator → dist/decorator}/get.js.map +0 -0
  102. package/{decorator → dist/decorator}/index.js +0 -0
  103. package/{decorator → dist/decorator}/index.js.map +0 -0
  104. package/{decorator → dist/decorator}/post.js +0 -0
  105. package/{decorator → dist/decorator}/post.js.map +0 -0
  106. package/{filters → dist/filters}/AccessControl.js +0 -0
  107. package/{filters → dist/filters}/AccessControl.js.map +0 -0
  108. package/{filters → dist/filters}/AccessRule.js +0 -0
  109. package/{filters → dist/filters}/AccessRule.js.map +0 -0
  110. package/{filters → dist/filters}/auth/AuthInterface.js +0 -0
  111. package/{filters → dist/filters}/auth/AuthInterface.js.map +0 -0
  112. package/{filters → dist/filters}/auth/AuthMethod.js +0 -0
  113. package/{filters → dist/filters}/auth/AuthMethod.js.map +0 -0
  114. package/{filters → dist/filters}/auth/HttpBasicAuth.js +0 -0
  115. package/{filters → dist/filters}/auth/HttpBasicAuth.js.map +0 -0
  116. package/{filters → dist/filters}/auth/HttpBearerAuth.js +0 -0
  117. package/{filters → dist/filters}/auth/HttpBearerAuth.js.map +0 -0
  118. package/{filters → dist/filters}/auth/HttpHeaderAuth.js +0 -0
  119. package/{filters → dist/filters}/auth/HttpHeaderAuth.js.map +0 -0
  120. package/{filters → dist/filters}/auth/JwtHttpBearerAuth.js +0 -0
  121. package/{filters → dist/filters}/auth/JwtHttpBearerAuth.js.map +0 -0
  122. package/{filters → dist/filters}/auth/KeyCloakHttpBearerAuth.js +0 -0
  123. package/{filters → dist/filters}/auth/KeyCloakHttpBearerAuth.js.map +0 -0
  124. package/{filters → dist/filters}/auth/index.js +0 -0
  125. package/{filters → dist/filters}/auth/index.js.map +0 -0
  126. package/{filters → dist/filters}/index.js +0 -0
  127. package/{filters → dist/filters}/index.js.map +0 -0
  128. package/{index.js → dist/index.js} +0 -0
  129. package/{index.js.map → dist/index.js.map} +0 -0
  130. package/dist/log/config/log4js.json +55 -0
  131. package/{model → dist/model}/RouteDefinition.js +0 -0
  132. package/{model → dist/model}/RouteDefinition.js.map +0 -0
  133. package/{requiments → dist/requiments}/Glob.js +0 -0
  134. package/{requiments → dist/requiments}/Glob.js.map +0 -0
  135. package/{requiments → dist/requiments}/ReflectUtil.js +0 -0
  136. package/{requiments → dist/requiments}/ReflectUtil.js.map +0 -0
  137. package/{requiments → dist/requiments}/Utils.js +0 -0
  138. package/{requiments → dist/requiments}/Utils.js.map +0 -0
  139. package/{validators → dist/validators}/BooleanValidator.js +0 -0
  140. package/{validators → dist/validators}/BooleanValidator.js.map +0 -0
  141. package/{validators → dist/validators}/CompareValidator.js +0 -0
  142. package/{validators → dist/validators}/CompareValidator.js.map +0 -0
  143. package/{validators → dist/validators}/DateValidator.js +0 -0
  144. package/{validators → dist/validators}/DateValidator.js.map +0 -0
  145. package/{validators → dist/validators}/EmailValidator.js +0 -0
  146. package/{validators → dist/validators}/EmailValidator.js.map +0 -0
  147. package/{validators → dist/validators}/Validator.js +0 -0
  148. package/{validators → dist/validators}/Validator.js.map +0 -0
  149. package/{web → dist/web}/IdentityInterface.js +0 -0
  150. package/{web → dist/web}/IdentityInterface.js.map +0 -0
  151. package/{web → dist/web}/WebUser.js +0 -0
  152. package/{web → dist/web}/WebUser.js.map +0 -0
  153. package/filters/AccessControl.ts +64 -0
  154. package/filters/AccessRule.ts +137 -0
  155. package/filters/auth/AuthInterface.ts +34 -0
  156. package/filters/auth/AuthMethod.ts +88 -0
  157. package/filters/auth/HttpBasicAuth.ts +7 -0
  158. package/filters/auth/HttpBearerAuth.ts +31 -0
  159. package/filters/auth/HttpHeaderAuth.ts +53 -0
  160. package/filters/auth/JwtHttpBearerAuth.ts +80 -0
  161. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  162. package/filters/auth/index.ts +3 -0
  163. package/filters/index.ts +2 -0
  164. package/index.ts +14 -0
  165. package/model/RouteDefinition.ts +18 -0
  166. package/package-lock.json +5259 -0
  167. package/package.json +3 -3
  168. package/requiments/Glob.ts +9 -0
  169. package/requiments/ReflectUtil.ts +26 -0
  170. package/requiments/Utils.ts +57 -0
  171. package/tsconfig.json +18 -0
  172. package/validators/BooleanValidator.ts +0 -0
  173. package/validators/CompareValidator.ts +0 -0
  174. package/validators/DateValidator.ts +0 -0
  175. package/validators/EmailValidator.ts +0 -0
  176. package/validators/Validator.ts +27 -0
  177. package/web/IdentityInterface.ts +62 -0
  178. package/web/WebUser.ts +100 -0
  179. package/BaseChyz.js.map +0 -1
  180. package/base/Model.js.map +0 -1
package/BaseChyz.ts ADDED
@@ -0,0 +1,384 @@
1
+ import 'reflect-metadata';
2
+ import {RouteDefinition} from "./model/RouteDefinition";
3
+ import {NextFunction, Request, Response} from "express";
4
+ import {Controller} from "./base/Controller";
5
+ import Utils from "./requiments/Utils";
6
+ import {ModelManager} from "./base";
7
+
8
+
9
+ const express = require("express");
10
+ const log4js = require("log4js");
11
+ const fs = require('fs');
12
+
13
+ var ip = require('ip');
14
+ var bodyParser = require('body-parser')
15
+ var methodOverride = require('method-override')
16
+
17
+ export {Request, Response, NextFunction} from "express";
18
+ export default class BaseChyz {
19
+ private config: any;
20
+ static app: string;
21
+ static express = express()
22
+ private _port: number = 3001;
23
+ static db: any;
24
+ static routes: any;
25
+ private _logConfig: any = require('./log/config/log4js.json') ?? {}
26
+ private _controllerpath: string = "Controllers"
27
+ private static controllers: Array<Controller> = []
28
+ public static components: any = {}
29
+ public static middlewares: any = {}
30
+
31
+
32
+ get logConfig(): any {
33
+ return this._logConfig;
34
+ }
35
+
36
+ set logConfig(value: any) {
37
+ this._logConfig = value;
38
+ }
39
+
40
+ get controllerpath(): string {
41
+ return this._controllerpath;
42
+ }
43
+
44
+ set controllerpath(value: string) {
45
+ this._controllerpath = value;
46
+ }
47
+
48
+ init() {
49
+ this.logProvider().level = log4js.levels.ALL;
50
+ this.logProvider().configure(this._logConfig);
51
+
52
+ /**
53
+ * set request id
54
+ */
55
+ Object.defineProperty(BaseChyz.express.request, 'reqId', {
56
+ configurable: true,
57
+ enumerable: true,
58
+ writable: true
59
+ })
60
+
61
+ Object.defineProperty(BaseChyz.express.request, 'user', {
62
+ configurable: true,
63
+ enumerable: true,
64
+ writable: true
65
+ })
66
+
67
+ Object.defineProperty(BaseChyz.express.request, 'identity', {
68
+ configurable: true,
69
+ enumerable: true,
70
+ writable: true
71
+ })
72
+
73
+
74
+ /**
75
+ * server port setting
76
+ */
77
+ if (this.config.hasOwnProperty("port"))
78
+ this.port = this.config.port;
79
+
80
+ /**
81
+ * controller path
82
+ */
83
+ if (this.config.controllerpath) {
84
+ this.controllerpath = this.config.controllerpath
85
+ }
86
+
87
+
88
+ /**
89
+ * Model Register
90
+ */
91
+
92
+ this.loadModels();
93
+
94
+ /**
95
+ * Express Server
96
+ */
97
+ this.middleware()
98
+
99
+ /**
100
+ * Load Controller
101
+ */
102
+ this.loadController();
103
+
104
+
105
+ }
106
+
107
+ /**
108
+ * Listen port number
109
+ * Server port number get
110
+ */
111
+ get port(): number {
112
+ return this._port;
113
+ }
114
+
115
+ /**
116
+ * Listen port number
117
+ * Server port number setting
118
+ * @param value
119
+ */
120
+ set port(value: number) {
121
+ this._port = value;
122
+ }
123
+
124
+
125
+ app(config: any = {}): BaseChyz {
126
+
127
+ /**
128
+ * Config set
129
+ */
130
+ this.config = config;
131
+
132
+
133
+ let components = Utils.findKeyValue(config, "components")
134
+ if (components) {
135
+ for (const componentsKey in components) {
136
+ let comp = components[componentsKey];
137
+ BaseChyz.debug("Create Component ", componentsKey)
138
+ BaseChyz.components[componentsKey] = Utils.createObject(new comp.class, comp);
139
+ BaseChyz.components[componentsKey]?.init();
140
+ }
141
+ }
142
+
143
+
144
+ let middlewares = Utils.findKeyValue(config, "middlewares")
145
+ if (middlewares) {
146
+ for (const middlewareKey in middlewares) {
147
+ let middleware1 = middlewares[middlewareKey];
148
+ BaseChyz.debug("Create middlewares ", middlewareKey)
149
+ BaseChyz.middlewares[middlewareKey] = middleware1;
150
+ // BaseChyz.middlewares[middlewareKey] = Utils.createObject(new middleware1.class, middleware1);
151
+ }
152
+ }
153
+
154
+
155
+ this.init();
156
+
157
+ return this;
158
+ }
159
+
160
+
161
+ public logProvider() {
162
+ return log4js;
163
+ }
164
+
165
+ public getLogger() {
166
+ return this.logProvider().getLogger(this.constructor.name);
167
+ }
168
+
169
+ static logs(...args: any[]) {
170
+ return log4js.getLogger(this.name);
171
+ }
172
+
173
+ public static trace(...args: any[]) {
174
+ BaseChyz.logs().fatal(...arguments)
175
+ }
176
+
177
+ public static debug(...args: any[]) {
178
+ BaseChyz.logs().debug(...arguments)
179
+ }
180
+
181
+ public static info(...args: any[]) {
182
+ BaseChyz.logs().info(...arguments)
183
+ }
184
+
185
+ public static warn(...args: any[]) {
186
+ BaseChyz.logs().warn(...arguments)
187
+ }
188
+
189
+ public static error(...args: any[]) {
190
+ BaseChyz.logs().error(...arguments)
191
+ }
192
+
193
+ public static fatal(...args: any[]) {
194
+ BaseChyz.logs().fatal(...arguments)
195
+ }
196
+
197
+
198
+ public static warning(...args: any[]) {
199
+ BaseChyz.logs().warn(...arguments)
200
+ }
201
+
202
+ public static t(text: string) {
203
+ return text;
204
+ }
205
+
206
+
207
+ public errorLogger(error: any, req: any, res: any, next: any) { // for logging errors
208
+ BaseChyz.error(error)
209
+ next(error) // forward to next middleware
210
+ }
211
+
212
+ public errorResponder(error: any, req: any, res: any, next: any) { // responding to client
213
+ if (error.type == 'redirect')
214
+ res.redirect('/error')
215
+ else if (error.type == 'time-out') // arbitrary condition check
216
+ res.status(408).json(error)
217
+ else
218
+ next(error) // forwarding exceptional case to fail-safe middleware
219
+ }
220
+
221
+
222
+ public errorHandler(err: any, req: any, res: any, next: any) {
223
+ if (res.headersSent) {
224
+ return next(err)
225
+ }
226
+ res.status(500)
227
+ res.json('error', {error: err})
228
+ }
229
+
230
+ public static getComponent(key: any) {
231
+ return BaseChyz.components[key] ?? null
232
+ }
233
+
234
+
235
+ public static getMiddlewares(key: any) {
236
+ return BaseChyz.middlewares[key] ?? null
237
+ }
238
+
239
+ /**
240
+ * load model
241
+ */
242
+ async loadModels() {
243
+ let models: any = {}
244
+ let path = `${this._controllerpath}/../Models`;
245
+ fs.readdirSync(path).forEach((file: string) => {
246
+ if (file !== "index.ts") {
247
+ let model = require(`${path}/${file}`);
248
+ // @ts-ignore
249
+ let className = file.split(".")[0] + "Class";
250
+ if (model[className])
251
+ models[className.replace("Class","")] = new model[className];
252
+ }
253
+ })
254
+
255
+ ModelManager._register(models);
256
+
257
+ for (const key of Object.keys(ModelManager)) {
258
+ if(key!="_register"){
259
+ ModelManager[key].init();
260
+ }
261
+ }
262
+ }
263
+
264
+ /**
265
+ * load contoller
266
+ */
267
+ async loadController() {
268
+ let articlesEndpoints: string[] = [];
269
+ fs.readdirSync(`${this._controllerpath}/`).forEach((file: string) => {
270
+ let controller = require(`${this._controllerpath}/${file}`);
271
+
272
+ // This is our instantiated class
273
+ const instance: Controller = new controller();
274
+
275
+ BaseChyz.controllers.push(instance);
276
+
277
+ // The prefix saved to our controller
278
+ // @ts-ignore
279
+ const prefix = Reflect.getMetadata('prefix', controller);
280
+ // Our `routes` array containing all our routes for this controller
281
+ // @ts-ignore
282
+ const routes: Array<RouteDefinition> = Reflect.getMetadata('routes', controller);
283
+ BaseChyz.logs().debug("Controller load ", controller.name, `(${prefix})`)
284
+
285
+ if (routes) {
286
+ routes.forEach(route => {
287
+
288
+ let actionId = route.path == "/" || route.path == "" ? instance.defaultAction : route.path;
289
+ route.id = actionId;
290
+ BaseChyz.logs().debug("Controller route Path", prefix + (route.path.startsWith("/") ? route.path : `/${route.path}`))
291
+
292
+ BaseChyz.express[route.requestMethod](prefix + (route.path.startsWith("/") ? route.path : `/${route.path}`),
293
+ async (req: Request, res: Response, next: NextFunction) => {
294
+ try {
295
+ BaseChyz.debug(`Call Request id ${instance.id}`)
296
+ await instance.beforeAction(route, req, res)
297
+ next()
298
+ } catch (e) {
299
+ BaseChyz.error(e);
300
+
301
+ res.status(e.statusCode || 500)
302
+ res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
303
+ // next(e)
304
+ }
305
+
306
+ },
307
+ async (req: Request, res: Response, next: NextFunction) => {
308
+ try {
309
+ // @ts-ignore
310
+ BaseChyz.debug("Request ID ", req.reqId)
311
+ // @ts-ignore
312
+ await instance[route.methodName](req, res, next);
313
+ instance.afterAction(route, req, res);
314
+ } catch (e) {
315
+ if (e instanceof Error) {
316
+ BaseChyz.error(e)
317
+
318
+ // @ts-ignore
319
+ res.status(e.statusCode || 500)
320
+ // @ts-ignore
321
+ res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
322
+ } else {
323
+ res.json(e)
324
+ }
325
+ }
326
+ })
327
+
328
+
329
+ });
330
+ }
331
+ })
332
+ }
333
+
334
+ public middleware() {
335
+
336
+ BaseChyz.express.use(bodyParser.json())
337
+ BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
338
+ BaseChyz.express.use(methodOverride());
339
+ BaseChyz.express.use(methodOverride());
340
+
341
+
342
+ // CORS
343
+ BaseChyz.express.use(function (req: any, res: Response, next: any) {
344
+ // @ts-ignore
345
+ req.reqId = Utils.uniqueId("chyzzzz_")
346
+ res.setHeader('Content-Type', 'application/json');
347
+ res.setHeader("Access-Control-Allow-Origin", "*");
348
+ res.setHeader("Access-Control-Allow-Credentials", "true");
349
+ res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
350
+ res.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,Authorization");
351
+ next();
352
+ });
353
+
354
+ //Middlewares
355
+ for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
356
+ if (!Utils.isFunction(middleware1)) {
357
+ let keycloak = BaseChyz.middlewares[middleware1].keycloak;
358
+ BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
359
+ } else {
360
+ BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
361
+ }
362
+
363
+ }
364
+
365
+
366
+ BaseChyz.express.use(this.errorResponder)
367
+ BaseChyz.express.use(this.errorHandler)
368
+ }
369
+
370
+
371
+ public Start() {
372
+
373
+ BaseChyz.info("Express Server Starting")
374
+ BaseChyz.express.listen(this._port, () => {
375
+ BaseChyz.info("Express Server Start ")
376
+ BaseChyz.info(`Liten Port ${this._port}`)
377
+ BaseChyz.info(`http://localhost:${this._port}`)
378
+ BaseChyz.info(`http://${ip.address()}:${this._port}`)
379
+ })
380
+ return this;
381
+ }
382
+
383
+
384
+ }
package/Chyz.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+
8
+ import BaseChyz from "./BaseChyz";
9
+ const Chyz = new BaseChyz();
10
+
11
+ export default Chyz;
@@ -0,0 +1,140 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ import {Controller, ModelManager} from "../../base";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {get} from "../../decorator";
15
+ import {post} from "../../decorator";
16
+ import {controller} from "../../decorator";
17
+ import {JwtHttpBearerAuth} from "../../filters/auth";
18
+
19
+ import {ValidationHttpException} from "../../base";
20
+ import {ForbiddenHttpException} from "../../base";
21
+
22
+
23
+
24
+ @controller("/api")
25
+ class ApiController extends Controller {
26
+
27
+ public myCheck(token) {
28
+ console.log("myyyyyyyyyyyyyyyyyyyyy")
29
+ }
30
+
31
+ public behaviors(): any[] {
32
+
33
+ return [{
34
+ 'authenticator': {
35
+ "class": JwtHttpBearerAuth,
36
+ // "auth": this.myCheck
37
+ },
38
+ // 'access': {
39
+ // 'class': AccessControl,
40
+ // 'only': ['login', 'logout', 'signup'],
41
+ // 'rules': [
42
+ // {
43
+ // 'allow': true,
44
+ // 'actions': ['login', 'index'],
45
+ // 'roles': ['?'],
46
+ // },
47
+ // {
48
+ // 'allow': true,
49
+ // 'actions': ['logout', "logout2"],
50
+ // 'roles': ['@'],
51
+ // }
52
+ // ]
53
+ // }
54
+ }]
55
+ }
56
+
57
+ @get("/")
58
+ Index(req: Request, res: Response) {
59
+
60
+ BaseChyz.logs().info("Site Controller Burası", this.id)
61
+ return res.json({message: "index sayfası"})
62
+ }
63
+
64
+ @post("orderCreate")
65
+ async Login(req: Request, res: Response) {
66
+ let data = req.body;
67
+ data.Customer.status = "true";
68
+ data.Customer["2fa"] = "true";
69
+
70
+ //Customer Model Create
71
+ let customer = ModelManager.Customer;
72
+ //Order Model Create
73
+ let order = ModelManager.Order;
74
+
75
+
76
+ let transaction
77
+ try {
78
+ // get transaction
79
+ transaction = await BaseChyz.getComponent("db").transaction();
80
+ customer.load(data, "Customer");//load customer data
81
+ let cus: any = await customer.save({}, {transaction});
82
+
83
+ if (!cus) {
84
+ throw new ValidationHttpException(customer.errors);
85
+ }
86
+
87
+ data.Order.customer_id = cus.id;
88
+ // data.Order.total = 0;
89
+ // data.Order.status = true;
90
+ order.load(data, "Order");
91
+ let res1 = await order.save({}, {transaction});
92
+ if (!res1) {
93
+ throw new ValidationHttpException(order.errors);
94
+ }
95
+
96
+ // commit
97
+ await transaction.commit();
98
+
99
+ } catch (e) {
100
+ if (transaction) {
101
+ await transaction.rollback();
102
+ BaseChyz.warn("Rollback transaction")
103
+ }
104
+
105
+ if (e instanceof ValidationHttpException)
106
+ throw new ValidationHttpException(e.message)
107
+ else
108
+ throw new ForbiddenHttpException(e.message)
109
+ }
110
+ return res.send("Post Controller")
111
+ }
112
+
113
+
114
+ @get("order/list")
115
+ async listOrder(req: Request, res: Response) {
116
+ let product = await ModelManager.Products.findAll({include: [ModelManager.ProductModels.model()]});
117
+ return res.json(product)
118
+
119
+ }
120
+
121
+ @get("categories")
122
+ async Categories(req: Request, res: Response) {
123
+ let product = await ModelManager.Categories.findAll( {include:[
124
+ {
125
+ model: ModelManager.Products.model(),
126
+ // as: 'product',
127
+ // through: { attributes: [] } // Hide unwanted `PlayerGameTeam` nested object from results
128
+ }
129
+ ]} );
130
+ return res.json(product)
131
+
132
+ }
133
+
134
+ error(req: Request, res: Response) {
135
+ BaseChyz.logs().info("Error Sayfası")
136
+ return res.send("Post Controller")
137
+ }
138
+ }
139
+
140
+ module.exports = ApiController
@@ -0,0 +1,100 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ import {Controller} from "../../base/Controller";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {get,post,controller} from "../../decorator";
15
+
16
+ import {ValidationHttpException,ForbiddenHttpException} from "../../base";
17
+ import {KeyCloakHttpBearerAuth} from "../../filters/auth/KeyCloakHttpBearerAuth";
18
+
19
+ import {OrderClass} from "../Models/Order";
20
+ import {CustomerClass} from "../Models/Customer";
21
+
22
+ @controller("/oauth2.0")
23
+ class ApiController extends Controller {
24
+
25
+ public behaviors(): any[] {
26
+
27
+ return [{
28
+ 'authenticator': {
29
+ "class": KeyCloakHttpBearerAuth,
30
+ // "auth": this.myCheck
31
+ }
32
+ }]
33
+ }
34
+
35
+ @get("/")
36
+ Index(req: Request, res: Response) {
37
+
38
+ BaseChyz.logs().info("Site Controller Burası", this.id)
39
+ return res.json({message: "index sayfası"})
40
+ }
41
+
42
+ @post("orderCreate")
43
+ async Login(req: Request, res: Response) {
44
+ let data = req.body;
45
+ data.Customer.status = "true";
46
+ data.Customer["2fa"] = "true";
47
+
48
+ //Customer Model Create
49
+ let customer: CustomerClass = new CustomerClass();
50
+ //Order Model Create
51
+ let order: OrderClass = new OrderClass() ;
52
+
53
+
54
+ let transaction
55
+ try {
56
+ // get transaction
57
+ transaction = await BaseChyz.getComponent("db").transaction();
58
+
59
+
60
+ customer.load(data, "Customer");//load customer data
61
+ let cus: any = await customer.save({}, {transaction});
62
+
63
+ if (!cus) {
64
+ throw new ValidationHttpException(customer.errors);
65
+ }
66
+
67
+ data.Order.customer_id = cus.id;
68
+ // data.Order.total = 0;
69
+ // data.Order.status = true;
70
+ order.load(data, "Order");
71
+ let res1 = await order.save({}, {transaction});
72
+ if (!res1) {
73
+ throw new ValidationHttpException(order.errors);
74
+ }
75
+
76
+ // commit
77
+ await transaction.commit();
78
+
79
+ } catch (e) {
80
+ if (transaction) {
81
+ await transaction.rollback();
82
+ BaseChyz.warn("Rollback transaction")
83
+ }
84
+
85
+ if (e instanceof ValidationHttpException)
86
+ throw new ValidationHttpException(e.message)
87
+ else
88
+ throw new ForbiddenHttpException(e.message)
89
+ }
90
+ return res.send("Post Controller")
91
+ }
92
+
93
+
94
+ error(req: Request, res: Response) {
95
+ BaseChyz.logs().info("Error Sayfası")
96
+ return res.send("Post Controller")
97
+ }
98
+ }
99
+
100
+ module.exports = ApiController
@@ -0,0 +1,42 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ import {Controller} from "../../base/Controller";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {get} from "../../decorator/get";
15
+ import {controller} from "../../decorator/controller";
16
+
17
+ @controller("/public")
18
+ class PublicController extends Controller {
19
+
20
+ public myCheck(token) {
21
+ console.log("myyyyyyyyyyyyyyyyyyyyy")
22
+ }
23
+
24
+ public behaviors(): any[] {
25
+
26
+ return [ ]
27
+ }
28
+
29
+ @get("/")
30
+ Index(req: Request, res: Response) {
31
+
32
+ BaseChyz.logs().info("Site Controller Burası", super.id)
33
+ return res.json({message: "index sayfası"})
34
+ }
35
+
36
+
37
+ error(req: Request, res: Response) {
38
+ BaseChyz.logs().info("Error Sayfası")
39
+ return res.send("Post Controller")
40
+ }
41
+ }
42
+ module.exports=PublicController