Skip to content

Aula 01 - Curso clean code & clean architecture com Rodrigo Branas

Notifications You must be signed in to change notification settings

Lucasbrunoferreira/cccat16_1

 
 

Repository files navigation

Curso Clean Code & Clean Architecture

Aula 01 - Refactoring

Com Rodrigo Branas: https://branas.io

Alterações

Verifique o histórico de commits para acompanhar a refatoração em passos.

Código inicial

O código abaixo foi apresentado em src/api.ts para realizar a refatoração.

import crypto from "crypto";
import express from "express";
import pgp from "pg-promise";
import { validate } from "./validateCpf";
const app = express();
app.use(express.json());

app.post("/signup", async function (req, res) {
	let result;
	const connection = pgp()("postgres://postgres:123456@localhost:5432/app");
	try {
		const id = crypto.randomUUID();

		const [acc] = await connection.query("select * from cccat15.account where email = $1", [req.body.email]);
		if (!acc) {

			if (req.body.name.match(/[a-zA-Z] [a-zA-Z]+/)) {
				if (req.body.email.match(/^(.+)@(.+)$/)) {

					if (validate(req.body.cpf)) {
						if (req.body.isDriver) {
							if (req.body.carPlate.match(/[A-Z]{3}[0-9]{4}/)) {
								await connection.query("insert into cccat15.account (account_id, name, email, cpf, car_plate, is_passenger, is_driver) values ($1, $2, $3, $4, $5, $6, $7)", [id, req.body.name, req.body.email, req.body.cpf, req.body.carPlate, !!req.body.isPassenger, !!req.body.isDriver]);

								const obj = {
									accountId: id
								};
								result = obj;
							} else {
								// invalid car plate
								result = -5;
							}
						} else {
							await connection.query("insert into cccat15.account (account_id, name, email, cpf, car_plate, is_passenger, is_driver) values ($1, $2, $3, $4, $5, $6, $7)", [id, req.body.name, req.body.email, req.body.cpf, req.body.carPlate, !!req.body.isPassenger, !!req.body.isDriver]);

							const obj = {
								accountId: id
							};
							result = obj;
						}
					} else {
						// invalid cpf
						result = -1;
					}
				} else {
					// invalid email
					result = -2;
				}

			} else {
				// invalid name
				result = -3;
			}

		} else {
			// already exists
			result = -4;
		}
		if (typeof result === "number") {
			res.status(422).send(result + "");
		} else {
			res.json(result);
		}
	} finally {
		await connection.$pool.end();
	}
});

app.listen(3000);

About

Aula 01 - Curso clean code & clean architecture com Rodrigo Branas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.4%
  • JavaScript 0.6%