Skip to content

Commit

Permalink
added models and repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
santanacostamarco committed May 4, 2020
1 parent 2439922 commit 26c87d2
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"dev:server": "ts-node-dev --inspect --transpileOnly --ignore-watch node_modules src/server.ts"
},
"dependencies": {
"express": "^4.17.1"
"date-fns": "^2.12.0",
"express": "^4.17.1",
"uuidv4": "^6.0.8"
},
"devDependencies": {
"@types/express": "^4.17.6",
Expand Down
2 changes: 1 addition & 1 deletion prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
singleQuote: true,
trailingComma: 'all',
arrowParams: 'avoid',
arrowParens: 'avoid',
};
17 changes: 17 additions & 0 deletions src/models/Appointment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { uuid } from 'uuidv4';

class Appointment {
id: string;

provider: string;

date: Date;

constructor(provider: string, date: Date) {
this.id = uuid();
this.provider = provider;
this.date = date;
}
}

export default Appointment;
30 changes: 30 additions & 0 deletions src/repositories/AppointmentsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isEqual } from 'date-fns';
import Appointment from '../models/Appointment';

class AppointmentsRepository {
private appointments: Appointment[];

constructor() {
this.appointments = [];
}

public create(provider: string, date: Date): Appointment {
const appointment = new Appointment(provider, date);
this.appointments.push(appointment);

return appointment;
}

public all(): Appointment[] {
return this.appointments;
}

public findByDate(date: Date): Appointment | null {
return (
this.appointments.find(appointment => isEqual(date, appointment.date)) ||
null
);
}
}

export default AppointmentsRepository;
29 changes: 29 additions & 0 deletions src/routes/appointments.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Router } from 'express';
import { startOfHour, parseISO } from 'date-fns';
import AppointmentsRepository from '../repositories/AppointmentsRepository';

const router = Router();

const appointmentsRepository = new AppointmentsRepository();

router.get('/', (request, response) => {
return response.json(appointmentsRepository.all());
});

router.post('/', (request, response) => {
const { provider, date } = request.body;

const parsedDate = startOfHour(parseISO(date));

if (appointmentsRepository.findByDate(parsedDate)) {
return response
.status(400)
.json({ message: 'This date is already booked.' });
}

const appointment = appointmentsRepository.create(provider, parsedDate);

return response.json(appointment);
});

export default router;
3 changes: 3 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Router } from 'express';
import appointmentsRouter from './appointments.router';

const routes = Router();

routes.use('/appointments', appointmentsRouter);

routes.get('/hello', (request, response) => {
return response.json({ message: 'hello, world!' });
});
Expand Down
4 changes: 0 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ const app = express();
app.use(express.json());
app.use(router);

app.get('/', (request, response) => {
return response.send('Hello, typescript!');
});

app.listen(3333);

0 comments on commit 26c87d2

Please sign in to comment.