chyz 1.0.12-rc.8 → 1.0.13-rc.11

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 (119) hide show
  1. package/BaseChyz.ts +115 -17
  2. package/Examples/Controllers/ApiController.ts +99 -29
  3. package/Examples/Controllers/BasicApiController.ts +121 -0
  4. package/Examples/Controllers/KeyCloakController.ts +100 -0
  5. package/Examples/Controllers/PublicController.ts +5 -7
  6. package/Examples/Controllers/SiteController.ts +99 -32
  7. package/Examples/Models/Categories.ts +47 -0
  8. package/Examples/Models/Customer.ts +115 -0
  9. package/Examples/Models/KeycloakUser.ts +70 -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 +72 -34
  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 +16 -14
  21. package/Examples/keycloak.json +7 -0
  22. package/Examples/log/app.log +6829 -1025
  23. package/Examples/log/errors.log +1569 -215
  24. package/Examples/package.json +46 -44
  25. package/Examples/tsconfig.json +3 -2
  26. package/Examples/yarn.lock +2549 -0
  27. package/README.md +116 -16
  28. package/base/ActionFilter.ts +2 -2
  29. package/base/BaseError.ts +2 -2
  30. package/base/DataErrorDbException.ts +1 -1
  31. package/base/DbConnection.ts +6 -0
  32. package/base/ForbiddenHttpException.ts +1 -1
  33. package/base/InvalidArgumentException.ts +16 -0
  34. package/base/InvalidConfigException.ts +1 -1
  35. package/base/Model.ts +207 -17
  36. package/base/ModelManager.ts +19 -0
  37. package/base/NotFoundHttpException.ts +1 -1
  38. package/base/RestClient.ts +28 -0
  39. package/base/UnauthorizedHttpException.ts +2 -1
  40. package/base/ValidationHttpException.ts +14 -0
  41. package/base/db/Exception.ts +14 -0
  42. package/base/index.ts +4 -0
  43. package/dist/BaseChyz.js +97 -13
  44. package/dist/BaseChyz.js.map +1 -1
  45. package/dist/base/ActionFilter.js +2 -2
  46. package/dist/base/ActionFilter.js.map +1 -1
  47. package/dist/base/BaseError.js +2 -2
  48. package/dist/base/BaseError.js.map +1 -1
  49. package/dist/base/DataErrorDbException.js +1 -1
  50. package/dist/base/DataErrorDbException.js.map +1 -1
  51. package/dist/base/DbConnection.js +3 -0
  52. package/dist/base/DbConnection.js.map +1 -1
  53. package/dist/base/ForbiddenHttpException.js +1 -1
  54. package/dist/base/ForbiddenHttpException.js.map +1 -1
  55. package/dist/base/InvalidArgumentException.js +18 -0
  56. package/dist/base/InvalidArgumentException.js.map +1 -0
  57. package/dist/base/InvalidConfigException.js +1 -1
  58. package/dist/base/InvalidConfigException.js.map +1 -1
  59. package/dist/base/Model.js +187 -15
  60. package/dist/base/Model.js.map +1 -1
  61. package/dist/base/ModelManager.js +9 -0
  62. package/dist/base/ModelManager.js.map +1 -0
  63. package/dist/base/NotFoundHttpException.js +1 -1
  64. package/dist/base/NotFoundHttpException.js.map +1 -1
  65. package/dist/base/RestClient.js +27 -0
  66. package/dist/base/RestClient.js.map +1 -0
  67. package/dist/base/UnauthorizedHttpException.js +1 -1
  68. package/dist/base/UnauthorizedHttpException.js.map +1 -1
  69. package/dist/base/ValidationHttpException.js +18 -0
  70. package/dist/base/ValidationHttpException.js.map +1 -0
  71. package/dist/base/db/Exception.js +16 -0
  72. package/dist/base/db/Exception.js.map +1 -0
  73. package/dist/base/index.js +4 -0
  74. package/dist/base/index.js.map +1 -1
  75. package/dist/filters/AccessControl.js +2 -2
  76. package/dist/filters/AccessControl.js.map +1 -1
  77. package/dist/filters/AccessRule.js.map +1 -1
  78. package/dist/filters/auth/AuthMethod.js +2 -2
  79. package/dist/filters/auth/AuthMethod.js.map +1 -1
  80. package/dist/filters/auth/HttpBasicAuth.js +65 -0
  81. package/dist/filters/auth/HttpBasicAuth.js.map +1 -1
  82. package/dist/filters/auth/HttpHeaderAuth.js.map +1 -1
  83. package/dist/filters/auth/JwtHttpBearerAuth.js +1 -0
  84. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  85. package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
  86. package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
  87. package/dist/index.js +28 -0
  88. package/dist/index.js.map +1 -0
  89. package/dist/package.json +57 -0
  90. package/dist/rbac/AuthAssignment.js +45 -0
  91. package/dist/rbac/AuthAssignment.js.map +1 -0
  92. package/dist/rbac/AuthItem.js +52 -0
  93. package/dist/rbac/AuthItem.js.map +1 -0
  94. package/dist/rbac/AuthItemChild.js +44 -0
  95. package/dist/rbac/AuthItemChild.js.map +1 -0
  96. package/dist/rbac/AuthManager.js +351 -0
  97. package/dist/rbac/AuthManager.js.map +1 -0
  98. package/dist/web/{User.js → WebUser.js} +12 -7
  99. package/dist/web/WebUser.js.map +1 -0
  100. package/filters/AccessControl.ts +3 -3
  101. package/filters/AccessRule.ts +2 -2
  102. package/filters/auth/AuthMethod.ts +4 -4
  103. package/filters/auth/HttpBasicAuth.ts +68 -0
  104. package/filters/auth/HttpHeaderAuth.ts +2 -2
  105. package/filters/auth/JwtHttpBearerAuth.ts +4 -5
  106. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  107. package/index.ts +14 -0
  108. package/package.json +57 -52
  109. package/rbac/AuthAssignment.ts +50 -0
  110. package/rbac/AuthItem.ts +57 -0
  111. package/rbac/AuthItemChild.ts +50 -0
  112. package/rbac/AuthManager.ts +390 -0
  113. package/web/IdentityInterface.ts +6 -0
  114. package/web/{User.ts → WebUser.ts} +10 -6
  115. package/.idea/Chy-Nodejs-Framework.iml +0 -12
  116. package/.idea/jsLibraryMappings.xml +0 -6
  117. package/.idea/modules.xml +0 -8
  118. package/.idea/vcs.xml +0 -6
  119. package/dist/web/User.js.map +0 -1
package/BaseChyz.ts CHANGED
@@ -3,13 +3,15 @@ import {RouteDefinition} from "./model/RouteDefinition";
3
3
  import {NextFunction, Request, Response} from "express";
4
4
  import {Controller} from "./base/Controller";
5
5
  import Utils from "./requiments/Utils";
6
+ import {ModelManager} from "./base";
6
7
 
7
8
 
8
9
  const express = require("express");
9
10
  const log4js = require("log4js");
10
11
  const fs = require('fs');
11
- const _ = require('lodash');
12
+ const validate = require('validate.js');
12
13
 
14
+ var ip = require('ip');
13
15
  var bodyParser = require('body-parser')
14
16
  var methodOverride = require('method-override')
15
17
 
@@ -21,10 +23,13 @@ export default class BaseChyz {
21
23
  private _port: number = 3001;
22
24
  static db: any;
23
25
  static routes: any;
26
+ private static _validate:any=validate;
24
27
  private _logConfig: any = require('./log/config/log4js.json') ?? {}
25
28
  private _controllerpath: string = "Controllers"
26
29
  private static controllers: Array<Controller> = []
27
30
  public static components: any = {}
31
+ public static middlewares: any = {}
32
+
28
33
 
29
34
  get logConfig(): any {
30
35
  return this._logConfig;
@@ -61,7 +66,6 @@ export default class BaseChyz {
61
66
  writable: true
62
67
  })
63
68
 
64
-
65
69
  Object.defineProperty(BaseChyz.express.request, 'identity', {
66
70
  configurable: true,
67
71
  enumerable: true,
@@ -82,6 +86,13 @@ export default class BaseChyz {
82
86
  this.controllerpath = this.config.controllerpath
83
87
  }
84
88
 
89
+
90
+ /**
91
+ * Model Register
92
+ */
93
+
94
+ this.loadModels();
95
+
85
96
  /**
86
97
  * Express Server
87
98
  */
@@ -113,6 +124,14 @@ export default class BaseChyz {
113
124
  }
114
125
 
115
126
 
127
+ static get validate(): any {
128
+ return this._validate;
129
+ }
130
+
131
+ static set validate(value: any) {
132
+ this._validate = value;
133
+ }
134
+
116
135
  app(config: any = {}): BaseChyz {
117
136
 
118
137
  /**
@@ -120,18 +139,36 @@ export default class BaseChyz {
120
139
  */
121
140
  this.config = config;
122
141
 
123
- this.init();
124
142
 
125
143
  let components = Utils.findKeyValue(config, "components")
126
144
  if (components) {
127
145
  for (const componentsKey in components) {
128
146
  let comp = components[componentsKey];
129
- BaseChyz.debug("Create Component ", componentsKey)
130
- BaseChyz.components[componentsKey] = Utils.createObject(new comp.class, comp);
131
- BaseChyz.components[componentsKey]?.init();
147
+ BaseChyz.logs().info("Create Component ", componentsKey)
148
+ try {
149
+ BaseChyz.components[componentsKey] = Utils.createObject(new comp.class, comp);
150
+ BaseChyz.components[componentsKey]?.init();
151
+ }catch (e) {
152
+ console.error(e)
153
+ }
154
+
132
155
  }
133
156
  }
134
157
 
158
+
159
+ let middlewares = Utils.findKeyValue(config, "middlewares")
160
+ if (middlewares) {
161
+ for (const middlewareKey in middlewares) {
162
+ let middleware1 = middlewares[middlewareKey];
163
+ BaseChyz.logs().debug("Create middlewares ", middlewareKey)
164
+ BaseChyz.middlewares[middlewareKey] = middleware1;
165
+ // BaseChyz.middlewares[middlewareKey] = Utils.createObject(new middleware1.class, middleware1);
166
+ }
167
+ }
168
+
169
+
170
+ this.init();
171
+
135
172
  return this;
136
173
  }
137
174
 
@@ -140,7 +177,7 @@ export default class BaseChyz {
140
177
  return log4js;
141
178
  }
142
179
 
143
- public getLogger(){
180
+ public getLogger() {
144
181
  return this.logProvider().getLogger(this.constructor.name);
145
182
  }
146
183
 
@@ -148,18 +185,31 @@ export default class BaseChyz {
148
185
  return log4js.getLogger(this.name);
149
186
  }
150
187
 
188
+ public static trace(...args: any[]) {
189
+ BaseChyz.logs().fatal(...arguments)
190
+ }
191
+
192
+ public static debug(...args: any[]) {
193
+ BaseChyz.logs().debug(...arguments)
194
+ }
195
+
151
196
  public static info(...args: any[]) {
152
197
  BaseChyz.logs().info(...arguments)
153
198
  }
154
199
 
200
+ public static warn(...args: any[]) {
201
+ BaseChyz.logs().warn(...arguments)
202
+ }
203
+
155
204
  public static error(...args: any[]) {
156
205
  BaseChyz.logs().error(...arguments)
157
206
  }
158
207
 
159
- public static debug(...args: any[]) {
160
- BaseChyz.logs().debug(...arguments)
208
+ public static fatal(...args: any[]) {
209
+ BaseChyz.logs().fatal(...arguments)
161
210
  }
162
211
 
212
+
163
213
  public static warning(...args: any[]) {
164
214
  BaseChyz.logs().warn(...arguments)
165
215
  }
@@ -196,6 +246,36 @@ export default class BaseChyz {
196
246
  return BaseChyz.components[key] ?? null
197
247
  }
198
248
 
249
+
250
+ public static getMiddlewares(key: any) {
251
+ return BaseChyz.middlewares[key] ?? null
252
+ }
253
+
254
+ /**
255
+ * load model
256
+ */
257
+ async loadModels() {
258
+ let models: any = {}
259
+ let path = `${this._controllerpath}/../Models`;
260
+ fs.readdirSync(path).forEach((file: string) => {
261
+ if (file !== "index.ts") {
262
+ let model = require(`${path}/${file}`);
263
+ // @ts-ignore
264
+ let className = file.split(".")[0] + "Class";
265
+ if (model[className])
266
+ models[className.replace("Class","")] = new model[className];
267
+ }
268
+ })
269
+
270
+ ModelManager._register(models);
271
+
272
+ for (const key of Object.keys(ModelManager)) {
273
+ if(key!="_register"){
274
+ ModelManager[key].init();
275
+ }
276
+ }
277
+ }
278
+
199
279
  /**
200
280
  * load contoller
201
281
  */
@@ -247,10 +327,16 @@ export default class BaseChyz {
247
327
  await instance[route.methodName](req, res, next);
248
328
  instance.afterAction(route, req, res);
249
329
  } catch (e) {
250
- BaseChyz.error(e)
251
- // next(e)
252
- res.status(e.statusCode)
253
- res.json({error: {code: e.statusCode, name: e.name, message: e.message}})
330
+ if (e instanceof Error) {
331
+ BaseChyz.error(e)
332
+
333
+ // @ts-ignore
334
+ res.status(e.statusCode || 500)
335
+ // @ts-ignore
336
+ res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
337
+ } else {
338
+ res.json(e)
339
+ }
254
340
  }
255
341
  })
256
342
 
@@ -265,8 +351,7 @@ export default class BaseChyz {
265
351
  BaseChyz.express.use(bodyParser.json())
266
352
  BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
267
353
  BaseChyz.express.use(methodOverride());
268
- BaseChyz.express.use(this.errorResponder)
269
- BaseChyz.express.use(this.errorHandler)
354
+ BaseChyz.express.use(methodOverride());
270
355
 
271
356
 
272
357
  // CORS
@@ -281,7 +366,20 @@ export default class BaseChyz {
281
366
  next();
282
367
  });
283
368
 
369
+ //Middlewares
370
+ for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
371
+ if (!Utils.isFunction(middleware1)) {
372
+ let keycloak = BaseChyz.middlewares[middleware1].keycloak;
373
+ BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
374
+ } else {
375
+ BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
376
+ }
377
+
378
+ }
379
+
284
380
 
381
+ BaseChyz.express.use(this.errorResponder)
382
+ BaseChyz.express.use(this.errorHandler)
285
383
  }
286
384
 
287
385
 
@@ -291,11 +389,11 @@ export default class BaseChyz {
291
389
  BaseChyz.express.listen(this._port, () => {
292
390
  BaseChyz.info("Express Server Start ")
293
391
  BaseChyz.info(`Liten Port ${this._port}`)
392
+ BaseChyz.info(`http://localhost:${this._port}`)
393
+ BaseChyz.info(`http://${ip.address()}:${this._port}`)
294
394
  })
295
395
  return this;
296
396
  }
297
397
 
298
398
 
299
399
  }
300
-
301
-
@@ -1,19 +1,25 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
- import {Controller} from "../../base/Controller";
10
+ import {Controller, ModelManager} from "../../base";
9
11
  import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
- import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
- import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
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
+ import {ProductsClass} from "../Models/Products";
22
+
17
23
 
18
24
  @controller("/api")
19
25
  class ApiController extends Controller {
@@ -29,22 +35,22 @@ class ApiController extends Controller {
29
35
  "class": JwtHttpBearerAuth,
30
36
  // "auth": this.myCheck
31
37
  },
32
- 'access': {
33
- 'class': AccessControl,
34
- 'only': ['login', 'logout', 'signup'],
35
- 'rules': [
36
- {
37
- 'allow': true,
38
- 'actions': ['login', 'index'],
39
- 'roles': ['?'],
40
- },
41
- {
42
- 'allow': true,
43
- 'actions': ['logout', "logout2"],
44
- 'roles': ['@'],
45
- }
46
- ]
47
- }
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
+ // }
48
54
  }]
49
55
  }
50
56
 
@@ -55,19 +61,83 @@ class ApiController extends Controller {
55
61
  return res.json({message: "index sayfası"})
56
62
  }
57
63
 
58
- @post("login")
59
- Login(req: Request, res: Response) {
60
- BaseChyz.logs().info("Post Controller")
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.save();
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
+ }
61
110
  return res.send("Post Controller")
62
111
  }
63
112
 
64
113
 
114
+ @get("order/list")
115
+ async listOrder(req: Request, res: Response) {
116
+ const {Products}: { Products: ProductsClass } = ModelManager;
117
+ let product = await Products.findAll({include: [ModelManager.ProductModels.model()]});
118
+ return res.json(product)
65
119
 
120
+ }
121
+
122
+ @get("categories")
123
+ async Categories(req: Request, res: Response) {
124
+ let product = await ModelManager.Categories.findAll({
125
+ include: [
126
+ {
127
+ model: ModelManager.Products.model(),
128
+ // as: 'product',
129
+ // through: { attributes: [] } // Hide unwanted `PlayerGameTeam` nested object from results
130
+ }
131
+ ]
132
+ });
133
+ return res.json(product)
66
134
 
135
+ }
67
136
 
68
137
  error(req: Request, res: Response) {
69
138
  BaseChyz.logs().info("Error Sayfası")
70
139
  return res.send("Post Controller")
71
140
  }
72
141
  }
73
- module.exports=ApiController
142
+
143
+ module.exports = ApiController
@@ -0,0 +1,121 @@
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, ForbiddenHttpException, ModelManager, ValidationHttpException} from "../../base";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {controller, get, post} from "../../decorator";
15
+ import {ProductsClass} from "../Models/Products";
16
+ import {HttpBasicAuth} from "../../filters/auth/HttpBasicAuth";
17
+
18
+
19
+ @controller("/basic/api")
20
+ class ApiController extends Controller {
21
+
22
+
23
+
24
+ public behaviors(): any[] {
25
+
26
+ return [{
27
+ 'authenticator': {
28
+ "class": HttpBasicAuth,
29
+ // "auth": this.myCheck
30
+ }
31
+ }]
32
+ }
33
+
34
+ @get("/")
35
+ Index(req: Request, res: Response) {
36
+
37
+ BaseChyz.logs().info("Site Controller Burası", this.id)
38
+ return res.json({message: "index sayfası"})
39
+ }
40
+
41
+ @post("orderCreate")
42
+ async Login(req: Request, res: Response) {
43
+ let data = req.body;
44
+ data.Customer.status = "true";
45
+ data.Customer["2fa"] = "true";
46
+
47
+ //Customer Model Create
48
+ let customer = ModelManager.Customer.save();
49
+ //Order Model Create
50
+ let order = ModelManager.Order;
51
+
52
+
53
+ let transaction
54
+ try {
55
+ // get transaction
56
+ transaction = await BaseChyz.getComponent("db").transaction();
57
+ customer.load(data, "Customer");//load customer data
58
+ let cus: any = await customer.save({}, {transaction});
59
+
60
+ if (!cus) {
61
+ throw new ValidationHttpException(customer.errors);
62
+ }
63
+
64
+ data.Order.customer_id = cus.id;
65
+ // data.Order.total = 0;
66
+ // data.Order.status = true;
67
+ order.load(data, "Order");
68
+ let res1 = await order.save({}, {transaction});
69
+ if (!res1) {
70
+ throw new ValidationHttpException(order.errors);
71
+ }
72
+
73
+ // commit
74
+ await transaction.commit();
75
+
76
+ } catch (e) {
77
+ if (transaction) {
78
+ await transaction.rollback();
79
+ BaseChyz.warn("Rollback transaction")
80
+ }
81
+
82
+ if (e instanceof ValidationHttpException)
83
+ throw new ValidationHttpException(e.message)
84
+ else
85
+ throw new ForbiddenHttpException(e.message)
86
+ }
87
+ return res.send("Post Controller")
88
+ }
89
+
90
+
91
+ @get("order/list")
92
+ async listOrder(req: Request, res: Response) {
93
+ const {Products}: { Products: ProductsClass } = ModelManager;
94
+ let product = await Products.findAll({include: [ModelManager.ProductModels.model()]});
95
+ return res.json(product)
96
+
97
+ }
98
+
99
+ @get("categories")
100
+ async Categories(req: Request, res: Response) {
101
+ let product = await ModelManager.Categories.findAll({
102
+ include: [
103
+ {
104
+ model: ModelManager.Products.model(),
105
+ // as: 'product',
106
+ // through: { attributes: [] } // Hide unwanted `PlayerGameTeam` nested object from results
107
+ }
108
+ ]
109
+ });
110
+ return res.json(product)
111
+
112
+ }
113
+
114
+
115
+ error(req: Request, res: Response) {
116
+ BaseChyz.logs().info("Error Sayfası")
117
+ return res.send("Post Controller")
118
+ }
119
+ }
120
+
121
+ 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
@@ -1,8 +1,10 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
10
  import {Controller} from "../../base/Controller";
@@ -10,11 +12,7 @@ import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
14
  import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
15
  import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
- import Utils from "../../requiments/Utils";
18
16
 
19
17
  @controller("/public")
20
18
  class PublicController extends Controller {