-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2439922
commit 26c87d2
Showing
7 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
arrowParams: 'avoid', | ||
arrowParens: 'avoid', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters