Adicionando rotas e lógica para Vans e Locadores de Vans
This commit is contained in:
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