-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.js
89 lines (83 loc) · 2.14 KB
/
Time.js
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
import firebase from 'firebase';
function toDateString(time) {
const date = new Date(time.seconds * 1000);
const dateString = `${date.getFullYear().toString()}/${
(date.getMonth() + 1).toString().padStart(2, '0')}/${
date.getDate().toString().padStart(2, '0')} ${
date.getHours().toString().padStart(2, '0')}:${
date.getMinutes().toString().padStart(2, '0')}:${
date.getSeconds().toString().padStart(2, '0')}`;
return dateString;
}
async function getLastestTime() {
const db = firebase.firestore().collection('time');
const timeRef = await db.orderBy('time', 'desc').limit(1).get();
const output = [];
if (timeRef.empty) {
output.push({
id: 0,
time: 'Time queue is empty.',
});
console.log('Time queue is empty.');
}
else {
timeRef.forEach((doc) => {
doc.data().id=doc.id;
console.log(doc.data().time);
output.push({
id: doc.id,
time :toDateString(doc.data().time),
});
});
console.log(output);
}
return output;
}
async function getAllTimes() {
const db = firebase.firestore().collection('time');
const timeRef = await db.orderBy('time', 'desc').get();
const output = [];
if (timeRef.empty) {
output.push({
id: 0,
time: 'Time queue is empty.',
});
console.log('Time queue is empty.');
}
else {
timeRef.forEach((doc) => {
output.push({
id: doc.id,
time : toDateString(doc.data().time),
});
});
console.log(output);
}
return output;
}
function addCurrentTime() {
const db = firebase.firestore();
const addtimeRef = db.collection('time');
const date = new Date();
const addtime = { time: date };
addtimeRef.add(addtime);
}
async function deleteEarliestTime() {
const db = firebase.firestore().collection('time');
const timeRef = await db.orderBy('time', 'asc').limit(1).get();
const del = [];
timeRef.forEach((doc) => {
del.push({
id: doc.id,
});
});
//console.log(del.data());
const delEarlist = db.doc(del[0].id);
delEarlist.delete();
}
export default {
getLastestTime,
getAllTimes,
addCurrentTime,
deleteEarliestTime,
};