Skip to content

Commit

Permalink
Repositório do TypeORM
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-vieira committed Sep 30, 2020
1 parent 11ed5c8 commit 66833fb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 45 deletions.
3 changes: 3 additions & 0 deletions nivel-02/02-iniciando-back-end-do-app/ormconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"username": "docker",
"password": "docker",
"database": "gostack_gobarber",
"entities": [
"./src/models/*.ts"
],
"migrations": [
"./src/database/migrations/*.ts"
],
Expand Down
1 change: 1 addition & 0 deletions nivel-02/02-iniciando-back-end-do-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class CreateAppointments1601469847641
type: 'varchar',
isPrimary: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()',
},
{
name: 'provider',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Appoitment> {
public async findByDate(date: Date): Promise<Appoitment | null> {
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;
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { startOfHour } from 'date-fns';
import { getCustomRepository } from 'typeorm';

import Appointment from '../models/Appointment';
import AppointmentsRepository from '../repositories/AppointmentsRepository';
Expand All @@ -9,28 +10,26 @@ interface Request {
}

class CreateAppointmentService {
private appointmentsRepository: AppointmentsRepository;
public async execute({ date, provider }: Request): Promise<Appointment> {
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,
);

if (findAppointmentInSameDate) {
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;
}
}
Expand Down

0 comments on commit 66833fb

Please sign in to comment.