From 603baf4a502ccd0c041b07fa65b0afc5a6f9b0e5 Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Fri, 20 Dec 2019 02:34:33 -0300 Subject: [PATCH 1/3] Adicionada estrutura do projeto na pasta src --- api/nodemon.json | 4 ++-- api/package.json | 4 ++-- api/src/app.ts | 24 ++++++++++++++++++++++++ api/src/controllers/user.controller.ts | 9 +++++++++ api/src/routes.ts | 10 ++++++++++ api/src/server.ts | 3 +++ api/tsconfig.json | 20 +++++++++++++------- 7 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 api/src/app.ts create mode 100644 api/src/controllers/user.controller.ts create mode 100644 api/src/routes.ts create mode 100644 api/src/server.ts diff --git a/api/nodemon.json b/api/nodemon.json index c0cfcfa..73656a9 100644 --- a/api/nodemon.json +++ b/api/nodemon.json @@ -1,5 +1,5 @@ { - "watch": ["."], + "watch": ["src"], "ext": "ts", - "exec": "ts-node ./index.ts" + "exec": "ts-node src/server.ts" } diff --git a/api/package.json b/api/package.json index edb8078..4aea1a4 100644 --- a/api/package.json +++ b/api/package.json @@ -4,9 +4,9 @@ "description": "Simple Rest API for manage the project", "main": "index.js", "scripts": { + "dev": "nodemon src/server.ts", "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon", - "tsc": "tsc" + "build": "tsc -p src" }, "repository": { "type": "git", diff --git a/api/src/app.ts b/api/src/app.ts new file mode 100644 index 0000000..3931b7a --- /dev/null +++ b/api/src/app.ts @@ -0,0 +1,24 @@ +import express from 'express'; + +import routes from './routes'; + +class App { + public express: express.Application; + + public constructor() { + this.express = express(); + + this.middlewares(); + this.routes(); + } + + private middlewares(): void { + this.express.use(express.json()); + } + + private routes(): void { + this.express.use(routes); + } +} + +export default new App().express; \ No newline at end of file diff --git a/api/src/controllers/user.controller.ts b/api/src/controllers/user.controller.ts new file mode 100644 index 0000000..3e90b93 --- /dev/null +++ b/api/src/controllers/user.controller.ts @@ -0,0 +1,9 @@ +import { Request, Response } from 'express'; + +class UserController { + public async index(_req: Request, res: Response): Promise { + return res.send("Hellow world"); + } +} + +export default new UserController(); \ No newline at end of file diff --git a/api/src/routes.ts b/api/src/routes.ts new file mode 100644 index 0000000..8a45eac --- /dev/null +++ b/api/src/routes.ts @@ -0,0 +1,10 @@ +import { Router } from 'express'; + +import UserController from './controllers/user.controller'; + +const routes = Router(); + +routes.get('/', UserController.index); + +export default routes; + diff --git a/api/src/server.ts b/api/src/server.ts new file mode 100644 index 0000000..f6030a9 --- /dev/null +++ b/api/src/server.ts @@ -0,0 +1,3 @@ +import app from './app'; + +app.listen(process.env.PORT || 3333); \ No newline at end of file diff --git a/api/tsconfig.json b/api/tsconfig.json index e259c99..05a55f5 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -12,7 +12,7 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - // "outDir": "./", /* Redirect output structure to the directory. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ @@ -24,7 +24,7 @@ /* Strict Type-Checking Options */ "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ @@ -33,10 +33,10 @@ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ /* Additional Checks */ - // "noUnusedLocals": true, /* Report errors on unused locals. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + "noUnusedLocals": true, /* Report errors on unused locals. */ + "noUnusedParameters": true, /* Report errors on unused parameters. */ + "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ @@ -62,5 +62,11 @@ /* Advanced Options */ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ - } + }, + "include": [ + "./src/" + ], + "exclude": [ + "node_modules" + ] } From 7f8eada40ed5b9d0374ad9609e2e7f27763a9ca0 Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Sat, 21 Dec 2019 13:40:05 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Modifcada=20organiza=C3=A7=C3=A3o=20dos=20a?= =?UTF-8?q?rquivos=20e=20autentica=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.env.example | 0 api/src/controllers/auth.controller.ts | 11 +++++++++++ .../{user.controller.ts => users.controller.ts} | 9 ++++++--- api/src/routes.ts | 8 ++++++-- api/src/services/auth/interfaces/auth.interface.ts | 0 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 api/.env.example create mode 100644 api/src/controllers/auth.controller.ts rename api/src/controllers/{user.controller.ts => users.controller.ts} (51%) create mode 100644 api/src/services/auth/interfaces/auth.interface.ts diff --git a/api/.env.example b/api/.env.example new file mode 100644 index 0000000..e69de29 diff --git a/api/src/controllers/auth.controller.ts b/api/src/controllers/auth.controller.ts new file mode 100644 index 0000000..070e4e2 --- /dev/null +++ b/api/src/controllers/auth.controller.ts @@ -0,0 +1,11 @@ +import { Request, Response } from 'express'; + +class AuthController { + public async token(req: Request, res: Response): Promise { + const a = req.body; + + return res.send("Send your credentials"); + } +} + +export default new AuthController(); diff --git a/api/src/controllers/user.controller.ts b/api/src/controllers/users.controller.ts similarity index 51% rename from api/src/controllers/user.controller.ts rename to api/src/controllers/users.controller.ts index 3e90b93..e3ad1a2 100644 --- a/api/src/controllers/user.controller.ts +++ b/api/src/controllers/users.controller.ts @@ -1,9 +1,12 @@ import { Request, Response } from 'express'; -class UserController { +class UsersController { + /** + * Usual names for CRUD methods are: index, store, update, delete + */ public async index(_req: Request, res: Response): Promise { return res.send("Hellow world"); - } + } } -export default new UserController(); \ No newline at end of file +export default new UsersController(); diff --git a/api/src/routes.ts b/api/src/routes.ts index 8a45eac..3de97d6 100644 --- a/api/src/routes.ts +++ b/api/src/routes.ts @@ -1,10 +1,14 @@ import { Router } from 'express'; -import UserController from './controllers/user.controller'; +import AuthController from './controllers/auth.controller'; +import UserController from './controllers/users.controller'; const routes = Router(); +// Auth +routes.post('/token', AuthController.token); + +// Users routes.get('/', UserController.index); export default routes; - diff --git a/api/src/services/auth/interfaces/auth.interface.ts b/api/src/services/auth/interfaces/auth.interface.ts new file mode 100644 index 0000000..e69de29 From 8ff726cc51dac91f4cb7202b65e03dc647f78780 Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Sat, 21 Dec 2019 14:04:53 -0300 Subject: [PATCH 3/3] Rotas reorganizadas em arquivo separado para cada recurso --- api/src/app.ts | 6 ++++-- api/src/routes.ts | 14 -------------- api/src/routes/auth.route.ts | 19 +++++++++++++++++++ api/src/routes/users.route.ts | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 16 deletions(-) delete mode 100644 api/src/routes.ts create mode 100644 api/src/routes/auth.route.ts create mode 100644 api/src/routes/users.route.ts diff --git a/api/src/app.ts b/api/src/app.ts index 3931b7a..4f5dcd1 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -1,6 +1,7 @@ import express from 'express'; -import routes from './routes'; +import AuthRoutes from './routes/auth.route'; +import UsersRoutes from './routes/users.route'; class App { public express: express.Application; @@ -17,7 +18,8 @@ class App { } private routes(): void { - this.express.use(routes); + this.express.use('/', AuthRoutes.router); + this.express.use('/api/user', UsersRoutes.router); } } diff --git a/api/src/routes.ts b/api/src/routes.ts deleted file mode 100644 index 3de97d6..0000000 --- a/api/src/routes.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Router } from 'express'; - -import AuthController from './controllers/auth.controller'; -import UserController from './controllers/users.controller'; - -const routes = Router(); - -// Auth -routes.post('/token', AuthController.token); - -// Users -routes.get('/', UserController.index); - -export default routes; diff --git a/api/src/routes/auth.route.ts b/api/src/routes/auth.route.ts new file mode 100644 index 0000000..c0dd0a1 --- /dev/null +++ b/api/src/routes/auth.route.ts @@ -0,0 +1,19 @@ +import { Router } from 'express'; + +import AuthController from '../controllers/auth.controller'; + +class AuthRoutes { + public router: Router; + public authController = AuthController; + + constructor() { + this.router = Router(); + this.routes(); + } + + private routes() { + this.router.post('/token', AuthController.token); + } +} + +export default new AuthRoutes(); \ No newline at end of file diff --git a/api/src/routes/users.route.ts b/api/src/routes/users.route.ts new file mode 100644 index 0000000..abe0b86 --- /dev/null +++ b/api/src/routes/users.route.ts @@ -0,0 +1,19 @@ +import { Router } from 'express'; + +import UsersController from '../controllers/users.controller'; + +class UsersRoutes { + public router: Router; + public usersController = UsersController; + + constructor() { + this.router = Router(); + this.routes(); + } + + private routes() { + this.router.get('/', UsersController.index); + } +} + +export default new UsersRoutes(); \ No newline at end of file