Adicionando rotas e lógica para Vans e Locadores de Vans
This commit is contained in:
50
src/database/migrations/1655691282002-CreateVansTable.ts
Normal file
50
src/database/migrations/1655691282002-CreateVansTable.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
|
||||||
|
|
||||||
|
export class CreateVansTable1655691282002 implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: 'vans',
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: 'id_van',
|
||||||
|
type: 'uuid',
|
||||||
|
isPrimary: true,
|
||||||
|
generationStrategy: 'uuid',
|
||||||
|
default: 'uuid_generate_v4()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'plate',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'brand',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'model',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'seats_number',
|
||||||
|
type: 'numeric',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'created_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'updated_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable('vans');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
|
||||||
|
|
||||||
|
export class CreateVanLocatorTable1655692363126 implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: 'vanLocator',
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: 'id_vanLocator',
|
||||||
|
type: 'uuid',
|
||||||
|
isPrimary: true,
|
||||||
|
generationStrategy: 'uuid',
|
||||||
|
default: 'uuid_generate_v4()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'name',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'address',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'complement',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'city',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'state',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'created_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'updated_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable('vanLocator');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
|
||||||
|
|
||||||
|
export class CreateVanDocumentsTable1655692498005
|
||||||
|
implements MigrationInterface
|
||||||
|
{
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: 'vanDocuments',
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: 'id_vanDocuments',
|
||||||
|
type: 'uuid',
|
||||||
|
isPrimary: true,
|
||||||
|
generationStrategy: 'uuid',
|
||||||
|
default: 'uuid_generate_v4()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'vanLocator_id',
|
||||||
|
type: 'uuid',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'document_status',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'created_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'updated_at',
|
||||||
|
type: 'timestamp',
|
||||||
|
default: 'now()',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable('vanDocuments');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { MigrationInterface, QueryRunner, TableForeignKey } from 'typeorm';
|
||||||
|
|
||||||
|
export class AddFKVanLocatorIdToVanDocumentsTable1655692624024
|
||||||
|
implements MigrationInterface
|
||||||
|
{
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createForeignKey(
|
||||||
|
'vanDocuments',
|
||||||
|
new TableForeignKey({
|
||||||
|
name: 'VanLocatorVanDocuments', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
|
||||||
|
columnNames: ['vanLocator_id'], // coluna que vai virar FK
|
||||||
|
referencedColumnNames: ['id_vanLocator'], // coluna PK da primeira tabela
|
||||||
|
referencedTableName: 'vanLocator', // nome da tabela que possui a PK
|
||||||
|
onDelete: 'SET NULL',
|
||||||
|
onUpdate: 'CASCADE',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropForeignKey(
|
||||||
|
'vanDocuments',
|
||||||
|
'VanLocatorVanDocuments',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
40
src/models/Van.ts
Normal file
40
src/models/Van.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import {
|
||||||
|
Entity,
|
||||||
|
Column,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
CreateDateColumn,
|
||||||
|
UpdateDateColumn,
|
||||||
|
JoinColumn,
|
||||||
|
OneToOne,
|
||||||
|
} from 'typeorm';
|
||||||
|
import VanLocator from './VanLocator';
|
||||||
|
|
||||||
|
@Entity('vans')
|
||||||
|
class Van {
|
||||||
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
id_van: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
plate: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
brand: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
model: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
seats_number: number;
|
||||||
|
|
||||||
|
@OneToOne(() => VanLocator, { eager: true })
|
||||||
|
@JoinColumn({ name: 'vanLocator_id' })
|
||||||
|
vanLocator: VanLocator;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
created_at: Date;
|
||||||
|
|
||||||
|
@UpdateDateColumn()
|
||||||
|
updated_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Van;
|
||||||
36
src/models/VanLocator.ts
Normal file
36
src/models/VanLocator.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import {
|
||||||
|
Entity,
|
||||||
|
Column,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
CreateDateColumn,
|
||||||
|
UpdateDateColumn,
|
||||||
|
} from 'typeorm';
|
||||||
|
|
||||||
|
@Entity('vanLocator')
|
||||||
|
class VanLocator {
|
||||||
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
id_vanLocator: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
address: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
complement: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
city: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
state: string;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
created_at: Date;
|
||||||
|
|
||||||
|
@UpdateDateColumn()
|
||||||
|
updated_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default VanLocator;
|
||||||
67
src/routes/vans.routes.ts
Normal file
67
src/routes/vans.routes.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import { Router } from 'express';
|
||||||
|
import { getRepository } from 'typeorm';
|
||||||
|
import Van from '../models/Van';
|
||||||
|
|
||||||
|
import ensureAuthenticated from '../middlewares/ensureAuthenticated';
|
||||||
|
|
||||||
|
import FindVanService from '../services/FindVanService';
|
||||||
|
import CreateVanService from '../services/CreateVanService';
|
||||||
|
import UpdateVanService from '../services/UpdateVanService';
|
||||||
|
|
||||||
|
const vansRouter = Router();
|
||||||
|
|
||||||
|
vansRouter.get('/list', async (request, response) => {
|
||||||
|
const vansRepository = getRepository(Van);
|
||||||
|
|
||||||
|
const vans = await vansRepository.find();
|
||||||
|
|
||||||
|
return response.json({ data: vans });
|
||||||
|
});
|
||||||
|
|
||||||
|
vansRouter.get('/:id', ensureAuthenticated, async (request, response) => {
|
||||||
|
const { id } = request.params;
|
||||||
|
|
||||||
|
const findVan = new FindVanService();
|
||||||
|
|
||||||
|
const van = await findVan.execute(id);
|
||||||
|
|
||||||
|
return response.json({ data: van });
|
||||||
|
});
|
||||||
|
|
||||||
|
vansRouter.post('/', async (request, response) => {
|
||||||
|
const { plate, brand, model, seats_number } = request.body;
|
||||||
|
|
||||||
|
const createVan = new CreateVanService();
|
||||||
|
|
||||||
|
const van = await createVan.execute({
|
||||||
|
plate,
|
||||||
|
brand,
|
||||||
|
model,
|
||||||
|
seats_number,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.json({ message: 'Van criada com sucesso.', data: van });
|
||||||
|
});
|
||||||
|
|
||||||
|
vansRouter.patch(
|
||||||
|
'/edit/:plate',
|
||||||
|
ensureAuthenticated,
|
||||||
|
async (request, response) => {
|
||||||
|
const { brand, model, seats_number } = request.body;
|
||||||
|
|
||||||
|
const { plate } = request.params;
|
||||||
|
|
||||||
|
const updateUserService = new UpdateVanService();
|
||||||
|
|
||||||
|
await updateUserService.execute({
|
||||||
|
plate,
|
||||||
|
brand,
|
||||||
|
model,
|
||||||
|
seats_number,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.json({ message: 'Informações da van atualizadas com sucesso.' });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export default vansRouter;
|
||||||
47
src/services/CreateVanService.ts
Normal file
47
src/services/CreateVanService.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { getRepository } from 'typeorm';
|
||||||
|
import { v4 } from 'uuid';
|
||||||
|
import { hash } from 'bcryptjs';
|
||||||
|
|
||||||
|
import AppError from '../errors/AppError';
|
||||||
|
|
||||||
|
import Van from '../models/Van';
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
plate: string;
|
||||||
|
brand: string;
|
||||||
|
model: string;
|
||||||
|
seats_number: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CreateVanService {
|
||||||
|
public async execute({
|
||||||
|
plate,
|
||||||
|
brand,
|
||||||
|
model,
|
||||||
|
seats_number,
|
||||||
|
}: Request): Promise<Van> {
|
||||||
|
const vansRepository = getRepository(Van);
|
||||||
|
|
||||||
|
const checkVanPlateExists = await vansRepository.findOne({
|
||||||
|
where: { plate },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (checkVanPlateExists) {
|
||||||
|
throw new AppError('Placa do veículo já cadastrado!', 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
const van = vansRepository.create({
|
||||||
|
id_van: v4(),
|
||||||
|
plate,
|
||||||
|
brand,
|
||||||
|
model,
|
||||||
|
seats_number: (Number)(seats_number),
|
||||||
|
});
|
||||||
|
|
||||||
|
await vansRepository.save(van);
|
||||||
|
|
||||||
|
return van;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CreateVanService;
|
||||||
23
src/services/FindVanService.ts
Normal file
23
src/services/FindVanService.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { getRepository } from 'typeorm';
|
||||||
|
|
||||||
|
import AppError from '../errors/AppError';
|
||||||
|
|
||||||
|
import Van from '../models/Van';
|
||||||
|
|
||||||
|
class FindVanService {
|
||||||
|
public async execute(id_van: string): Promise<Van> {
|
||||||
|
const vansRepository = getRepository(Van);
|
||||||
|
|
||||||
|
const van = await vansRepository.findOne({
|
||||||
|
where: { id_van }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!van) {
|
||||||
|
throw new AppError('Van does not exist.');
|
||||||
|
};
|
||||||
|
|
||||||
|
return van;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default FindVanService;
|
||||||
66
src/services/UpdateUserSocialInformation.ts
Normal file
66
src/services/UpdateUserSocialInformation.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import { getRepository } from 'typeorm';
|
||||||
|
|
||||||
|
import AppError from '../errors/AppError';
|
||||||
|
|
||||||
|
import User from '../models/User';
|
||||||
|
import Social from '../models/SocialInformation';
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
id_user: string;
|
||||||
|
social_info_request: {
|
||||||
|
phone?: string;
|
||||||
|
whatsapp?: string;
|
||||||
|
facebook?: string;
|
||||||
|
telegram?: string;
|
||||||
|
};
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpdateUserSocialService {
|
||||||
|
public async execute({
|
||||||
|
id_user,
|
||||||
|
social_info_request,
|
||||||
|
username,
|
||||||
|
}: Request): Promise<Social> {
|
||||||
|
const usersRepository = getRepository(User);
|
||||||
|
const socialInformationRepository = getRepository(Social);
|
||||||
|
|
||||||
|
const user = await usersRepository.findOne({
|
||||||
|
where: { id_user },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new AppError('User does not exist.', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
let social = await socialInformationRepository.findOne({
|
||||||
|
where: { user },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!social) {
|
||||||
|
social = socialInformationRepository.create({ user, phone: "", whatsapp: "", facebook: "", telegram: "" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (social_info_request.phone) {
|
||||||
|
social.phone = social_info_request.phone
|
||||||
|
}
|
||||||
|
|
||||||
|
if (social_info_request.whatsapp) {
|
||||||
|
social.whatsapp = social_info_request.whatsapp
|
||||||
|
}
|
||||||
|
|
||||||
|
if (social_info_request.facebook) {
|
||||||
|
social.facebook = social_info_request.facebook
|
||||||
|
}
|
||||||
|
|
||||||
|
if (social_info_request.telegram) {
|
||||||
|
social.telegram = social_info_request.telegram
|
||||||
|
}
|
||||||
|
|
||||||
|
await socialInformationRepository.save(social);
|
||||||
|
|
||||||
|
return social;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UpdateUserSocialService;
|
||||||
42
src/services/UpdateVanService.ts
Normal file
42
src/services/UpdateVanService.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { getRepository } from 'typeorm';
|
||||||
|
|
||||||
|
import AppError from '../errors/AppError';
|
||||||
|
|
||||||
|
import Van from '../models/Van';
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
plate: string;
|
||||||
|
brand: string;
|
||||||
|
model: string;
|
||||||
|
seats_number: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpdateVanService {
|
||||||
|
public async execute({
|
||||||
|
plate,
|
||||||
|
brand,
|
||||||
|
model,
|
||||||
|
seats_number,
|
||||||
|
}: Request): Promise<Van> {
|
||||||
|
const vansRepository = getRepository(Van);
|
||||||
|
|
||||||
|
const van = await vansRepository.findOne({
|
||||||
|
where: { plate },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!van) {
|
||||||
|
throw new AppError('Van informada não existe.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plate) van.plate = plate;
|
||||||
|
if (brand) van.brand = brand;
|
||||||
|
if (model) van.model = model;
|
||||||
|
if (seats_number) van.seats_number = (Number)(seats_number);
|
||||||
|
|
||||||
|
await vansRepository.save(van);
|
||||||
|
|
||||||
|
return van;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UpdateVanService;
|
||||||
Reference in New Issue
Block a user