* 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:
Artemy Egorov 2023-04-25 17:42:50 +03:00 committed by GitHub
parent 43097f9d22
commit cf5151de7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View file

@ -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. - [ ] 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 - [ ] Api for upload photos
- [ ] Inserting a photo in a note - [ ] 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 # AnoPaper v1.0.0

24
core.js
View file

@ -26,11 +26,22 @@ class NotesCore {
let db = connection.db(process.env.MONGO_DB); let db = connection.db(process.env.MONGO_DB);
this.notes = db.collection("notes"); this.notes = db.collection("notes");
this.stats = db.collection("stats");
} }
async getNote(_id) { async getNote(_id) {
try { 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 { } catch {
return null; return null;
} }
@ -38,6 +49,7 @@ class NotesCore {
async deleteNote(_id) { async deleteNote(_id) {
try { try {
await this.incStats("deletedNotes");
return await this.notes.deleteOne({ _id }); return await this.notes.deleteOne({ _id });
} catch { } catch {
return null; return null;
@ -49,17 +61,25 @@ class NotesCore {
note._id = sha3(JSON.stringify(note)); note._id = sha3(JSON.stringify(note));
note.time = Date.now(); note.time = Date.now();
note.pub = true; note.pub = true;
note.pubTime = note.time;
await this.notes.updateOne( await this.notes.updateOne(
{ _id: note._id }, { _id: note._id },
{ $set: note }, { $set: note },
{ upsert: true } { upsert: true }
); );
await this.incStats("sentNotes");
return note._id; return note._id;
} catch { } catch {
return null; return null;
} }
} }
async incStats(_id) {
await this.stats.updateOne(
{ _id },
{ $inc: { value: 1 } },
{ upsert: true }
);
}
} }
module.exports = { module.exports = {

View file

@ -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.use(express.static("dist"));
app.get("*", function (req, res) { app.get("*", function (req, res) {