Adicionando rotas e lógica para Vans e Locadores de Vans

This commit is contained in:
Matheus Albino Brunhara
2022-06-19 22:21:12 -05:00
parent 01a30b0f17
commit 98fb081648
11 changed files with 495 additions and 0 deletions

View 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;

View 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;

View 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;

View 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;