-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
251 lines (230 loc) · 7.09 KB
/
server.js
File metadata and controls
251 lines (230 loc) · 7.09 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const express = require("express");
const cors = require("cors");
const path = require("path");
const admin = require('firebase-admin');
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
const ServiceAccount = require('./ServiceAccount.json');
const { time } = require("console");
const app = express();
const PORT = process.env.PORT || 5000;
admin.initializeApp({ credential: admin.credential.cert(ServiceAccount) });
const db = getFirestore();
const ScheduleCollection = 'schedules' // スケジュールDB名
const DiaryCollection = 'diary' // ダイアリーDB名
// ミドルウェア
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
let items = [];
/**スケジュールを取得するAPI*/
app.get('/items', async (req, res) => {
const { date,name,month } = req.query; // クエリパラメータから日付を取得
items = [];
try {
const itemsRef = db.collection(ScheduleCollection);
let q;
let snapshot;
/*日付で絞り込んだスケジュールを取得する*/
if(date)
{
snapshot = await itemsRef.where("date", "==", date).get();
}else{
snapshot = await itemsRef.get();
}
if (snapshot.empty) {
console.log('No matching documents.');
return res.json([]);
}
snapshot.forEach(doc => {
/*取得したデータを絞り込む*/
if(month)
{
/*指定した月のデータを絞り込む*/
let result = doc.get('date').split("-");
if(month != result[1])
{
return;
}
}
if(name)
{
if (!doc.get('name').includes(name)) {
return;
}
}
console.log("GET " + doc.id, '=>', doc.data());
items.push(doc.data());
});
res.json(items);
} catch (error) {
console.error('Error getting documents:', error);
res.status(500).json({ error: 'データ取得に失敗しました' });
}
});
/**IsCheckedの状態を配列で取得するAPI*/
app.get('/items/GetIsCheckedList', async (req, res) => {
items = [];
IsChecked = [];
try {
const itemsRef = db.collection(ScheduleCollection);
const snapshot = await itemsRef.get();
if (snapshot.empty) {
console.log('No matching documents.');
return res.json([]);
}
// 取得した全データに処理を行う
snapshot.forEach(doc => {
console.log("GET " + doc.id, '=>', doc.get('IsChecked'));
items.push(doc.get('IsChecked'));
});
console.log(items);
// リストを格納
res.json(items);
} catch (error) {
console.error('Error getting documents:', error);
res.status(500).json({ error: 'データ取得に失敗しました' });
}
});
/**スケジュールを追加するAPI*/
app.post('/items', async (req, res) => {
// データの定義場所
const { id, date,IsChecked,name,Time } = req.body;
items.push(req.body);
try {
const docRef = db.collection(ScheduleCollection).doc(id);
const setAda = await docRef.set({
id : id,
date: date,
IsChecked: IsChecked,
name: name,
Time : Time,
});
res.json({ message: '登録完了', items });
console.log("登録しました " + name)
} catch (error) {
console.error('Error registering documents:', error);
res.status(500).json({ error: '登録に失敗しました' });
}
});
/**スケジュールを更新するAPI*/
app.put('/items/:id', async (req, res) => {
// データの定義場所
const { id } = req.params;
const { date,IsChecked,name,Time } = req.body; // データ本体
try {
// sv-SEロケールはYYYY-MM-DD形式の日付文字列を戻す
const docRef = db.collection(ScheduleCollection).doc(id);
const setAda = await docRef.set({
id : id,
date: date,
IsChecked: IsChecked,
name: name,
Time : Time,
});
res.json({ message: '更新完了', items });
console.log("更新しました " + name)
} catch (error) {
console.error('Error updating documents:', error);
res.status(500).json({ error: '更新に失敗しました' });
}
});
/**スケジュールを削除するAPI*/
app.delete('/items/:id', async (req, res) => {
items = [];
try {
const { id } = req.params;
await db.collection(ScheduleCollection).doc(id).delete();
res.json({ message: '削除完了', items });
console.log("削除しました " + id)
} catch (error) {
console.error('Error removeing documents:', error);
res.status(500).json({ error: '削除に失敗しました' });
}
});
/**日記を追加するAPI*/
app.post('/items/Diary', async (req, res) => {
// データの定義場所
const { id, date,text } = req.body;
items.push(req.body);
try {
const docRef = db.collection(DiaryCollection).doc(id);
const setAda = await docRef.set({
id : id,
date: date,
text: text,
});
res.json({ message: '登録完了', items });
console.log("登録しました " + text)
} catch (error) {
console.error('Error registering documents:', error);
res.status(500).json({ error: '登録に失敗しました' });
}
});
/**日記を更新するAPI*/
app.put('/items/Diary/:id', async (req, res) => {
// データの定義場所
const { id, date,text } = req.body;
items.push(req.body);
try {
const docRef = db.collection(DiaryCollection).doc(id);
const setAda = await docRef.set({
id : id,
date: date,
text: text,
});
res.json({ message: '更新完了', items });
console.log("更新しました " + name)
} catch (error) {
console.error('Error updating documents:', error);
res.status(500).json({ error: '更新に失敗しました' });
}
});
/**日記を取得するAPI*/
app.get('/items/Diary', async (req, res) => {
const { date,name,month } = req.query; // クエリパラメータから日付を取得
items = [];
try {
const itemsRef = db.collection(DiaryCollection);
let snapshot;
/*日付で絞り込んだスケジュールを取得する*/
if(date)
{
snapshot = await itemsRef.where("date", "==", date).get();
}else{
return res.error('ERROR:Please Enter Date');
}
if (snapshot.empty) {
console.log('No matching documents.');
return res.json([]);
}
snapshot.forEach(doc => {
/*取得したデータを絞り込む*/
if(month)
{
/*指定した月のデータを絞り込む*/
let result = doc.get('date').split("-");
if(month != result[1])
{
return;
}
}
if(name)
{
if (!doc.get('name').includes(name)) {
return;
}
}
console.log("Diary GET " + doc.id, '=>', doc.data());
items.push(doc.data());
});
res.json(items);
} catch (error) {
console.error('Error getting documents:', error);
res.status(500).json({ error: 'データ取得に失敗しました' });
}
});
// サーバー起動
app.listen(PORT,"localhost", () => {
console.log(`Server running on port ${PORT}`);
});