-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappointments.controller.js
More file actions
101 lines (86 loc) · 2.78 KB
/
appointments.controller.js
File metadata and controls
101 lines (86 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const appointmentsService = require('../services/appointments.service');
const responses = require('../models/responses/index');
const readAll = (req, res) => {
const promise = appointmentsService.readAll();
promise
.then(item => {
const r = new responses.ItemResponse(item)
res.json(r)
})
.catch(err => {
res.set(500).send(err);
})
return promise;
}
const readById = (req, res) => {
const id = parseInt(req.params.id) // req.params.id comes in as a string, used parseInt to convert to integer
const promise = appointmentsService.readById(id);
promise // the promise has an if/else statement based on the userId. will show or hide appointments based on id
.then(item => {
if (req.auth.id !== id) { // if the logged in Id (req.auth.id) is NOT EQUAL to the /appointments/user/`id` (req.params.id)
const newArr = item.map(e => ({ ...e, description: ''})) // if above is true, update the object property 'description' with an empty string
res.json(new responses.ItemResponse(newArr)); // send updated object as the response
} else {
res.json(new responses.ItemResponse(item)); // if the logged in Id matches the req.params.id, then send the original object as response
}
})
.catch(err => {
res.set(500).send(err);
})
return promise;
}
const create = (req, res) => {
const promise = appointmentsService.create(req.model);
promise
.then(response => {
res.status(201).json(response)
})
.catch(err => {
res.status(500).send(err.stack);
})
}
const updateById = (req, res) => {
const id = req.params.id;
const promise = appointmentsService.updateById(id, req.model)
promise
.then(response => {
res.sendStatus(200)
})
.catch(err => {
res.set(500).send(err);
})
}
const deleteById = (req, res) => {
const id = req.params.id
const promise = appointmentsService.del(id)
promise
.then(response => {
res.sendStatus(200);
})
.catch(err => {
res.set(500).send(err);
})
}
const readByMonthYear = (req, res) => {
const id = req.params.id
const year = req.params.year
const month = req.params.month
const promise = appointmentsService.readByMonthYear(id, year, month);
promise
.then(item => {
const r = new responses.ItemResponse(item)
res.json(r)
})
.catch(err => {
res.set(500).set(err);
})
return promise;
}
module.exports = {
readAll
, readById
, create
, updateById
, deleteById
, readByMonthYear
}