-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.js
More file actions
167 lines (151 loc) · 3.57 KB
/
handlers.js
File metadata and controls
167 lines (151 loc) · 3.57 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// @ts-check
const admin = require("firebase-admin");
const ALLOW_ADDING = process.env.ALLOW_ADDING || false;
/**
* Get a 'fail' response
*
* @param {any} err Error message
* @returns JSON object
*/
function errResponse(err) {
return {
status: "fail",
error: err,
data: null,
};
}
/**
* Get an 'ok' response
*
* @param {any} data Data to send
* @returns JSON object
*/
function okResponse(data) {
return {
status: "ok",
error: null,
data: data,
};
}
/**
* Initialize firebase (firstly checks if it's not already initialized)
*/
function initFirebase() {
if (admin.apps.length == 0) {
admin.initializeApp({
credential: admin.credential.cert(
JSON.parse(
Buffer.from(process.env.ADMIN_SDK_CREDS, "base64").toString("ascii")
)
),
});
}
}
/**
* Get a FieldPath query ready to use in a select
*
* Shorthand for `new admin.firestore.FieldPath(s)`
*
* @param {string} s Firebase field
* @returns {admin.firestore.FieldPath} generated query
*/
function getQuery(s) {
return new admin.firestore.FieldPath(s);
}
/**
* @param {string} brand Sneaker brand
* @param {string} name Sneaker name
* @returns string, URL Formatted link
*/
function getImageLink(brand, name) {
const _name = name.replace(/ /g, "-");
return `https://firebasestorage.googleapis.com/v0/b/curs-flutter.appspot.com/o/${brand}%2F${_name}.jpg?alt=media`;
}
module.exports = class Handlers {
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
static getAllSneakers(req, res) {
initFirebase();
let sneakers = [];
const db = admin.firestore().collection("sneakers");
db.select(
getQuery("brand"),
getQuery("name"),
getQuery("price"),
getQuery("image")
)
.get()
.then((doc) => {
doc.docs.map((doc) => {
sneakers.push({
id: doc.id,
...doc.data(),
});
});
res.status(200).json(okResponse(sneakers));
})
.catch((reason) => {
res.status(500).json(errResponse(reason));
return;
});
}
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
static getSneakerWithID(req, res) {
initFirebase();
if (!req.params.id) {
res.status(500).json(errResponse("Missing ID"));
return;
}
const db = admin.firestore().collection("sneakers");
db.doc(req.params.id)
.get()
.then((doc) => {
if (doc.exists) {
res.status(200).json(
okResponse({
id: doc.id,
...doc.data(),
})
);
} else {
res.status(500).json(errResponse("Invalid ID"));
}
})
.catch((reason) => {
res.status(500).json(errResponse(reason));
return;
});
}
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
static addSneaker(req, res) {
if (ALLOW_ADDING !== "true") {
res.status(500).json(errResponse("Method disabled"));
return;
}
initFirebase();
const db = admin.firestore().collection("sneakers");
db.add({
name: req.body.name,
brand: req.body.brand,
price: req.body.price,
image: getImageLink(req.body.brand, req.body.name),
desc: req.body.desc,
url: req.body.url,
})
.then((doc) => {
res.status(200).json(okResponse(doc.id));
})
.catch((reason) => {
res.status(500).json(errResponse(reason));
return;
});
}
};