mirror of
https://github.com/artegoser/AnoPaper.git
synced 2024-11-21 19:36:22 +03:00
Stats (#8)
* feat: sent notes stats * fix: unused var in notes * refactor: stats increment * feat: received notes, deleted notes * fix: received notes stats inc * refactor: stats * fix: inc received * feat: api for stats * doc: update changelog
This commit is contained in:
parent
43097f9d22
commit
cf5151de7e
3 changed files with 32 additions and 2 deletions
|
@ -23,6 +23,7 @@
|
|||
- [ ] Settings for publish notes, such as: delete after reading, number of reads before deleting, adding your own data (name, picture, status in the settings) to the note.
|
||||
- [ ] Api for upload photos
|
||||
- [ ] Inserting a photo in a note
|
||||
- [x] Maintaining statistics on sent notes, the number of notes received, number of deleted notes
|
||||
|
||||
# AnoPaper v1.0.0
|
||||
|
||||
|
|
24
core.js
24
core.js
|
@ -26,11 +26,22 @@ class NotesCore {
|
|||
let db = connection.db(process.env.MONGO_DB);
|
||||
|
||||
this.notes = db.collection("notes");
|
||||
this.stats = db.collection("stats");
|
||||
}
|
||||
|
||||
async getNote(_id) {
|
||||
try {
|
||||
return await this.notes.findOne({ _id });
|
||||
let note = await this.notes.findOne({ _id });
|
||||
if (note !== null) await this.incStats("receivedNotes");
|
||||
return note;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getStats(_id) {
|
||||
try {
|
||||
return await this.stats.findOne({ _id });
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
@ -38,6 +49,7 @@ class NotesCore {
|
|||
|
||||
async deleteNote(_id) {
|
||||
try {
|
||||
await this.incStats("deletedNotes");
|
||||
return await this.notes.deleteOne({ _id });
|
||||
} catch {
|
||||
return null;
|
||||
|
@ -49,17 +61,25 @@ class NotesCore {
|
|||
note._id = sha3(JSON.stringify(note));
|
||||
note.time = Date.now();
|
||||
note.pub = true;
|
||||
note.pubTime = note.time;
|
||||
await this.notes.updateOne(
|
||||
{ _id: note._id },
|
||||
{ $set: note },
|
||||
{ upsert: true }
|
||||
);
|
||||
await this.incStats("sentNotes");
|
||||
return note._id;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async incStats(_id) {
|
||||
await this.stats.updateOne(
|
||||
{ _id },
|
||||
{ $inc: { value: 1 } },
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
9
index.js
9
index.js
|
@ -107,6 +107,15 @@ app.get("/get-note/:delorno/:id", async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
app.get("/stats/:id", async (req, res) => {
|
||||
let stats = await core.getStats(req.params.id);
|
||||
if (stats) {
|
||||
res.json(stats);
|
||||
} else {
|
||||
res.status(404).send("There is no stats with such id");
|
||||
}
|
||||
});
|
||||
|
||||
app.use(express.static("dist"));
|
||||
|
||||
app.get("*", function (req, res) {
|
||||
|
|
Loading…
Add table
Reference in a new issue