From 66833fbaca66266169af584d49253aab87ce7124 Mon Sep 17 00:00:00 2001 From: Danilo Vieira Date: Wed, 30 Sep 2020 10:53:28 -0300 Subject: [PATCH] =?UTF-8?q?Reposit=C3=B3rio=20do=20TypeORM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ormconfig.json | 3 ++ .../02-iniciando-back-end-do-app/package.json | 1 + .../1601469847641-CreateAppointments.ts | 1 + .../repositories/AppointmentsRepository.ts | 37 ++++--------------- .../src/routes/appointments.routes.ts | 17 +++++---- .../src/services/CreateAppointmentService.ts | 15 ++++---- 6 files changed, 29 insertions(+), 45 deletions(-) diff --git a/nivel-02/02-iniciando-back-end-do-app/ormconfig.json b/nivel-02/02-iniciando-back-end-do-app/ormconfig.json index 0b51e34..d074cde 100644 --- a/nivel-02/02-iniciando-back-end-do-app/ormconfig.json +++ b/nivel-02/02-iniciando-back-end-do-app/ormconfig.json @@ -5,6 +5,9 @@ "username": "docker", "password": "docker", "database": "gostack_gobarber", + "entities": [ + "./src/models/*.ts" + ], "migrations": [ "./src/database/migrations/*.ts" ], diff --git a/nivel-02/02-iniciando-back-end-do-app/package.json b/nivel-02/02-iniciando-back-end-do-app/package.json index f13a383..734ae24 100644 --- a/nivel-02/02-iniciando-back-end-do-app/package.json +++ b/nivel-02/02-iniciando-back-end-do-app/package.json @@ -12,6 +12,7 @@ "date-fns": "^2.16.1", "express": "^4.17.1", "pg": "^8.3.3", + "reflect-metadata": "^0.1.13", "typeorm": "^0.2.27", "uuidv4": "^6.2.3" }, diff --git a/nivel-02/02-iniciando-back-end-do-app/src/database/migrations/1601469847641-CreateAppointments.ts b/nivel-02/02-iniciando-back-end-do-app/src/database/migrations/1601469847641-CreateAppointments.ts index 5b76275..dad6cc0 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/database/migrations/1601469847641-CreateAppointments.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/database/migrations/1601469847641-CreateAppointments.ts @@ -12,6 +12,7 @@ export default class CreateAppointments1601469847641 type: 'varchar', isPrimary: true, generationStrategy: 'uuid', + default: 'uuid_generate_v4()', }, { name: 'provider', diff --git a/nivel-02/02-iniciando-back-end-do-app/src/repositories/AppointmentsRepository.ts b/nivel-02/02-iniciando-back-end-do-app/src/repositories/AppointmentsRepository.ts index 8cf91a0..3a74674 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/repositories/AppointmentsRepository.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/repositories/AppointmentsRepository.ts @@ -1,37 +1,16 @@ -import { isEqual } from 'date-fns'; -import Appoitment from '../models/Appointment'; - -interface CreateAppointmentDTO { - provider: string; - date: Date; -} - -class AppointmentsRepository { - private appointments: Appoitment[]; - - constructor() { - this.appointments = []; - } +import { EntityRepository, Repository } from 'typeorm'; - public all(): Appoitment[] { - return this.appointments; - } +import Appoitment from '../models/Appointment'; - public findByDate(date: Date): Appoitment | null { - const findAppointment = this.appointments.find(appointment => - isEqual(date, appointment.date), - ); +@EntityRepository(Appoitment) +class AppointmentsRepository extends Repository { + public async findByDate(date: Date): Promise { + const findAppointment = await this.findOne({ + where: { date }, + }); return findAppointment || null; } - - public create({ provider, date }: CreateAppointmentDTO): Appoitment { - const appointment = new Appoitment({ provider, date }); - - this.appointments.push(appointment); - - return appointment; - } } export default AppointmentsRepository; diff --git a/nivel-02/02-iniciando-back-end-do-app/src/routes/appointments.routes.ts b/nivel-02/02-iniciando-back-end-do-app/src/routes/appointments.routes.ts index aafb145..00fcb03 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/routes/appointments.routes.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/routes/appointments.routes.ts @@ -1,29 +1,30 @@ +import 'reflect-metadata'; + import { Router } from 'express'; +import { getCustomRepository } from 'typeorm'; import { parseISO } from 'date-fns'; import AppointmentsRepository from '../repositories/AppointmentsRepository'; import CreateAppointmentService from '../services/CreateAppointmentService'; const appointmentsRouter = Router(); -const appointmentsRepository = new AppointmentsRepository(); -appointmentsRouter.get('/', (request, response) => { - const appointments = appointmentsRepository.all(); +appointmentsRouter.get('/', async (request, response) => { + const appointmentsRepository = getCustomRepository(AppointmentsRepository); + const appointments = await appointmentsRepository.find(); return response.json(appointments); }); -appointmentsRouter.post('/', (request, response) => { +appointmentsRouter.post('/', async (request, response) => { try { const { provider, date } = request.body; const parsedDate = parseISO(date); - const createAppointment = new CreateAppointmentService( - appointmentsRepository, - ); + const createAppointment = new CreateAppointmentService(); - const appointment = createAppointment.execute({ + const appointment = await createAppointment.execute({ date: parsedDate, provider, }); diff --git a/nivel-02/02-iniciando-back-end-do-app/src/services/CreateAppointmentService.ts b/nivel-02/02-iniciando-back-end-do-app/src/services/CreateAppointmentService.ts index d9ac091..188ca49 100644 --- a/nivel-02/02-iniciando-back-end-do-app/src/services/CreateAppointmentService.ts +++ b/nivel-02/02-iniciando-back-end-do-app/src/services/CreateAppointmentService.ts @@ -1,4 +1,5 @@ import { startOfHour } from 'date-fns'; +import { getCustomRepository } from 'typeorm'; import Appointment from '../models/Appointment'; import AppointmentsRepository from '../repositories/AppointmentsRepository'; @@ -9,16 +10,12 @@ interface Request { } class CreateAppointmentService { - private appointmentsRepository: AppointmentsRepository; + public async execute({ date, provider }: Request): Promise { + const appointmentsRepository = getCustomRepository(AppointmentsRepository); - constructor(appointmentsRepository: AppointmentsRepository) { - this.appointmentsRepository = appointmentsRepository; - } - - public execute({ date, provider }: Request): Appointment { const appointmentDate = startOfHour(date); - const findAppointmentInSameDate = this.appointmentsRepository.findByDate( + const findAppointmentInSameDate = await appointmentsRepository.findByDate( appointmentDate, ); @@ -26,11 +23,13 @@ class CreateAppointmentService { throw Error('This appointment is already booked'); } - const appointment = this.appointmentsRepository.create({ + const appointment = appointmentsRepository.create({ provider, date: appointmentDate, }); + await appointmentsRepository.save(appointment); + return appointment; } }