mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-28 12:55:36 -04:00
rename the app
This commit is contained in:
parent
ce35924a54
commit
f65bf959b1
27 changed files with 1832 additions and 0 deletions
35
messages/src/models/db.js
Normal file
35
messages/src/models/db.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const { Database } = require("bun:sqlite");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { mkdir } = require("node:fs/promises");
|
||||
const { stat } = require("node:fs");
|
||||
|
||||
const dbDirPath = path.join(path.dirname(__dirname), "/db");
|
||||
const dbPath = path.join(dbDirPath, "/keynotes.db");
|
||||
|
||||
async function makeDirectory(path) {
|
||||
const dirCreation = await mkdir(path);
|
||||
return dirCreation;
|
||||
}
|
||||
|
||||
// Make sure DB path exists
|
||||
stat(dbDirPath, (err, stats) => {
|
||||
if (err !== null) {
|
||||
makeDirectory(dbDirPath);
|
||||
}
|
||||
});
|
||||
|
||||
const db = new Database(dbPath);
|
||||
// Enable WAL Mode
|
||||
db.exec("PRAGMA journal_mode = WAL;");
|
||||
|
||||
const initSQL = path.join(__dirname, "./init.sql");
|
||||
fs.readFile(initSQL, "utf8", (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
db.exec(data);
|
||||
});
|
||||
|
||||
module.exports = db;
|
73
messages/src/models/init.sql
Normal file
73
messages/src/models/init.sql
Normal file
|
@ -0,0 +1,73 @@
|
|||
CREATE TABLE IF NOT EXISTS users (
|
||||
user_id INTEGER PRIMARY KEY ASC,
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_profile_info (
|
||||
user_info_id INTEGER PRIMARY KEY ASC,
|
||||
email VARCHAR(50),
|
||||
user_id INTEGER REFERENCES users(user_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
message_id INTEGER PRIMARY KEY ASC,
|
||||
message TEXT,
|
||||
media VARCHAR(100),
|
||||
date NUMBER,
|
||||
user_id INTEGER REFERENCES users(user_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS likes (
|
||||
likes_id INTEGER PRIMARY KEY ASC,
|
||||
message_id INTEGER REFERENCES messages(message_id),
|
||||
user_id INTEGER REFERENCES users(user_id),
|
||||
liked_at NUMBER
|
||||
);
|
||||
|
||||
|
||||
|
||||
INSERT OR IGNORE INTO users (username, password)
|
||||
VALUES ("jim", "$2a$10$Bmjre5WSpSSAi.nWBfLZFOlhQhbIAoY/MM7ikJz3Ho9tqeXCExaB6"),
|
||||
("demo", "$2a$10$Vo.XmsVQVx9gGojRIdewpOap5SnhrgZ21/Im5IxhH3PC4FycM5uwC");
|
||||
|
||||
INSERT OR IGNORE INTO messages (message_id, message, media, date, user_id)
|
||||
VALUES
|
||||
(1, "When life gives you lemons, make lemonade.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(2, "Rome wasn’t built in a day – take your time.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(3, "The grass is always greener on the other side.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(4, "If it ain’t broke, don’t fix it.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(5, "You can’t have your cake and eat it too.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(6, "Better late than never.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(7, "What doesn’t kill you makes you stronger.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(8, "Every cloud has a silver lining.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(9, "Don’t bite the hand that feeds you.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(10, "Absence makes the heart grow fonder.", NULL, strftime('%s', 'now')*1000, 1),
|
||||
(11, "Always trust your gut feeling – it’s usually right.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(12, "Money can’t buy happiness, but it sure helps.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(13, "Everything happens for a reason.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(14, "Don’t go to bed angry – resolve conflicts before sleeping.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(15, "The early bird catches the worm.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(16, "You only live once – make the most of it.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(17, "Hard work pays off in the end.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(18, "Family is everything – cherish them.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(19, "Don’t put off until tomorrow what you can do today.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(20, "A smile costs nothing but gives so much.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(21, "You can’t please everyone – focus on yourself.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(22, "Health is wealth – take care of your body.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(23, "Patience is a virtue – good things take time.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(24, "Don’t judge a book by its cover.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(25, "Actions speak louder than words.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(26, "Learn from your mistakes – they make you wiser.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(27, "Kindness costs nothing but means everything.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(28, "Life is short – eat the cake.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(29, "Comparison is the thief of joy – focus on your journey.", NULL, strftime('%s', 'now')*1000, 2),
|
||||
(30, "The best things in life are free.", NULL, strftime('%s', 'now')*1000, 2);
|
||||
|
||||
-- ALTER TABLE likes
|
||||
-- ADD COLUMN user_id INTEGER REFERENCES users(user_id);
|
||||
|
||||
-- ALTER TABLE likes
|
||||
-- ADD COLUMN liked_at NUMBER;
|
||||
|
||||
-- ALTER TABLE likes DROP COLUMN sum_likes;
|
138
messages/src/models/query.js
Normal file
138
messages/src/models/query.js
Normal file
|
@ -0,0 +1,138 @@
|
|||
const db = require("./db");
|
||||
|
||||
function getEveryNote() {
|
||||
try {
|
||||
const query = db.query(
|
||||
`SELECT messages.message_id, message, media, date, users.username AS user, messages.user_id, COUNT(likes.likes_id) AS likes_count
|
||||
FROM messages LEFT JOIN users ON messages.user_id = users.user_id
|
||||
LEFT JOIN likes ON messages.message_id = likes.message_id
|
||||
GROUP BY messages.message_id, message, media, date, users.username
|
||||
ORDER BY date DESC
|
||||
`,
|
||||
);
|
||||
|
||||
const data = query.all();
|
||||
return data;
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
function putNewNote(note) {
|
||||
const { message, media, userId } = note;
|
||||
const date = Date.now();
|
||||
const query = db.query(
|
||||
`INSERT INTO messages (message, media, user_id, date) VALUES ($1, $2, $3, $4)`,
|
||||
);
|
||||
query.run({ $1: message, $2: media, $3: userId, $4: date });
|
||||
}
|
||||
|
||||
function deleteNote(messageId) {
|
||||
try {
|
||||
const query = db.query(`DELETE FROM likes WHERE message_id = $1`);
|
||||
query.run({ $1: messageId });
|
||||
const queryTwo = db.query(`DELETE FROM messages WHERE message_id = $1`);
|
||||
queryTwo.run({ $1: messageId });
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
function getLikesByUser(userId) {
|
||||
const query = db.query(`SELECT message_id FROM likes WHERE user_id = $1`);
|
||||
|
||||
return query.all({ $1: userId });
|
||||
}
|
||||
|
||||
function getTotalLikesByMessageId(messageId) {
|
||||
const query = db.query(`SELECT COUNT(1) FROM likes WHERE message_id = $1`);
|
||||
return query.get({ $1: messageId });
|
||||
}
|
||||
|
||||
function getNoteById(noteId) {
|
||||
const query = db.query(
|
||||
`SELECT message, user_id from messages WHERE message_id = $1`,
|
||||
);
|
||||
return query.get({ $1: noteId });
|
||||
}
|
||||
|
||||
function checkIfNotedLiked(messageId, userId) {
|
||||
const query = db.query(
|
||||
`SELECT * FROM likes WHERE message_id = $1 and user_id = $2`,
|
||||
);
|
||||
|
||||
const res = query.get({ $1: messageId, $2: userId });
|
||||
return res;
|
||||
}
|
||||
|
||||
function insertLike(messageId, userId) {
|
||||
try {
|
||||
const query = db.query(
|
||||
`INSERT INTO likes (message_id, user_id, liked_at) VALUES ($1, $2, $3)`,
|
||||
);
|
||||
query.run({ $1: messageId, $2: userId, $3: Date.now() });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
function deleteLike(messageId, userId) {
|
||||
try {
|
||||
const query = db.query(
|
||||
`DELETE FROM likes WHERE message_id = $1 and user_id = $2`,
|
||||
);
|
||||
query.run({ $1: messageId, $2: userId });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
function insertUser(username, password) {
|
||||
try {
|
||||
const query = db.query(
|
||||
"INSERT INTO users (username, password) VALUES ($username, $password)",
|
||||
);
|
||||
query.run({ $username: username, $password: password });
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
function getUserById(userId) {
|
||||
const query = db.query(
|
||||
`SELECT username, user_id FROM users WHERE user_id = $1`,
|
||||
);
|
||||
const res = query.get({ $1: userId });
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function getSumNotesByUserId(userId) {
|
||||
const query = db.query(`SELECT COUNT(1) FROM messages WHERE user_id = $1`);
|
||||
const res = query.get({ $1: userId });
|
||||
|
||||
return res["COUNT(1)"];
|
||||
}
|
||||
function getAllNotesByUserId(userId) {
|
||||
const query = db.query(`SELECT * FROM messages WHERE user_id = $1`);
|
||||
const res = query.all({ $1: userId });
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getEveryNote,
|
||||
getNoteById,
|
||||
putNewNote,
|
||||
insertLike,
|
||||
deleteLike,
|
||||
deleteNote,
|
||||
checkIfNotedLiked,
|
||||
insertUser,
|
||||
getLikesByUser,
|
||||
getTotalLikesByMessageId,
|
||||
getUserById,
|
||||
getSumNotesByUserId,
|
||||
getAllNotesByUserId,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue