Added other files

This commit is contained in:
Akif Yüce 2022-02-26 21:26:27 +03:00 committed by GitHub
parent 9c4cd23464
commit 4009344da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 0 deletions

5
classes/index.js Normal file
View File

@ -0,0 +1,5 @@
const Message = require("./message")
const Thread = require("./thread")
const User = require("./user")
module.exports = { Message, Thread, User }

15
classes/message.js Normal file
View File

@ -0,0 +1,15 @@
const User = require("./user")
module.exports = class Message {
constructor(content, author = new User(), time = new Date().getTime()) {
this.content = content;
this.author = author;
this.time = time;
}
}

49
classes/thread.js Normal file
View File

@ -0,0 +1,49 @@
const db = require("quick.db")
const User = require("./user")
module.exports = class Thread {
constructor(title, content, author = new User(), messages = [], time = new Date().getTime()) {
this.content = content;
this.author = author;
this.title = title;
this.messages = messages;
this.time = time;
}
getId(id = this.id) {
const thread = db.get("threads."+id);
if (!thread) return null;
this.id = id;
const { content, title, author, messages = [], time = new Date().getTime() } = thread;
this.content = content
this.title = title
this.author = author
this.messages = messages;
this.time = time;
return this
}
takeId(){
this.id = db.get("threads").length;
return this
}
push(message){
this.messages.push(message)
return this;
}
write(id = this.id) {
db.set("threads."+id, this)
}
getLink(id = this.id) {
return "/threads/" + id;
}
}

40
classes/user.js Normal file
View File

@ -0,0 +1,40 @@
const db = require("quick.db")
module.exports = class User {
constructor(name = "guest", avatar = "/images/guest.png", time = new Date().getTime()) {
this.name = name;
this.avatar = avatar;
this.time = time;
}
getId(id = this.id) {
const user = db.get("users." + id);
if (!user) return null;
this.id = id;
const { name = "guest", avatar = "/images/guest.png", time = new Date().getTime() } = user;
this.name = name;
this.avatar = avatar;
this.time = time;
return this
}
takeId() {
let id = db.get("users");
this.id = id ? id.length : 0;
return this
}
write(id = this.id) {
db.set("users." + id, this)
}
getLink(id = this.id) {
return "/users/" + id;
}
}

2
errors/error.js Normal file
View File

@ -0,0 +1,2 @@
module.exports = (res, type, error) =>
res.status(type).render("error", { type, error });