Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"nodemailer": "^6.4.10",
"uuidv4": "^6.2.0"
},
Expand Down
10 changes: 9 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import express from 'express'
import express,{ Request, Response, NextFunction } from 'express'
import 'express-async-errors'

import { router } from './routes'

const app = express()

app.use(express.json())
app.use(router)
app.use((err:Error, request: Request, response: Response, next: NextFunction) => {
if(err instanceof Error) {
return response.status(400).json({ message: err.message})
}

return response.status(500).json({ message: 'Internal Server Error' })
})
export { app }
4 changes: 1 addition & 3 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { createUserController } from "./useCases/CreateUser";

const router = Router()

router.post('/users', (request, response) => {
return createUserController.handle(request, response);
});
router.post('/users', createUserController.handle);

export { router }
8 changes: 2 additions & 6 deletions src/useCases/CreateUser/CreateUserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ export class CreateUserController {
async handle(request: Request, response: Response): Promise<Response> {
const { name, email, password } = request.body;

try {

await this.createUserUseCase.execute({
name,
email,
password
})

return response.status(201).send();
} catch (err) {
return response.status(400).json({
message: err.message || 'Unexpected error.'
})
}

}
}