Fix Error in create_db
This commit is contained in:
@@ -6,8 +6,12 @@
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import { DB } from "https://deno.land/x/sqlite/mod.ts";
|
||||
import { dirname, fromFileUrl, join } from "https://deno.land/std/path/mod.ts";
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import {
|
||||
dirname,
|
||||
fromFileUrl,
|
||||
join,
|
||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
||||
|
||||
// +++ VARIABLES ---------------------------------------------------- //
|
||||
const _dirname: string = dirname(fromFileUrl(import.meta.url));
|
||||
@@ -33,7 +37,7 @@ export function createDatabase(): void {
|
||||
following TEXT,
|
||||
contacts TEXT
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
posts_uuid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
@@ -43,7 +47,7 @@ export function createDatabase(): void {
|
||||
likes INTEGER,
|
||||
comments INTEGER
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS comments (
|
||||
comment_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
post_id INTEGER,
|
||||
@@ -51,7 +55,7 @@ export function createDatabase(): void {
|
||||
date_created_at TEXT,
|
||||
text TEXT,
|
||||
likes INTEGER
|
||||
)
|
||||
); -- Added semicolon here
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
message_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -61,7 +65,7 @@ export function createDatabase(): void {
|
||||
timestamp TEXT,
|
||||
FOREIGN KEY (chat_id) REFERENCES chats (chat_id),
|
||||
FOREIGN KEY (sender_id) REFERENCES accounts (user_id)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
chat_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
@@ -10,7 +10,10 @@ import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import { Comments } from "../interfaces.ts";
|
||||
import { mapCommentRow, queryDatabase } from "./mod.ts";
|
||||
|
||||
async function getCommentsFromDB(db: DB, post_id?: number): Promise<Comments[]> {
|
||||
async function getCommentsFromDB(
|
||||
db: DB,
|
||||
post_id?: number,
|
||||
): Promise<Comments[]> {
|
||||
const query = post_id
|
||||
? `SELECT * FROM comments WHERE post_id = ?`
|
||||
: `SELECT * FROM comments`;
|
||||
@@ -18,20 +21,31 @@ async function getCommentsFromDB(db: DB, post_id?: number): Promise<Comments[]>
|
||||
return await queryDatabase<Comments>(db, query, params, mapCommentRow);
|
||||
}
|
||||
|
||||
async function createComment(db: DB, postId: string, userId: string, createdAt: string, text: string): Promise<string> {
|
||||
function createComment(
|
||||
db: DB,
|
||||
postId: string,
|
||||
userId: string,
|
||||
createdAt: string,
|
||||
text: string,
|
||||
): string {
|
||||
const query = `
|
||||
INSERT INTO comments (post_id, author_user_id, date_created_at, text, likes)
|
||||
VALUES (?, ?, ?, ?, 0)
|
||||
`;
|
||||
db.query(query, [postId, userId, createdAt, text]);
|
||||
|
||||
const updatePostQuery = `UPDATE posts SET comments = comments + 1 WHERE posts_uuid = ?`;
|
||||
|
||||
const updatePostQuery =
|
||||
`UPDATE posts SET comments = comments + 1 WHERE posts_uuid = ?`;
|
||||
db.query(updatePostQuery, [postId]);
|
||||
|
||||
|
||||
return db.lastInsertRowId.toString();
|
||||
}
|
||||
|
||||
async function updateComment(db: DB, commentId: string, text: string): Promise<void> {
|
||||
async function updateComment(
|
||||
db: DB,
|
||||
commentId: string,
|
||||
text: string,
|
||||
): Promise<void> {
|
||||
const query = `UPDATE comments SET text = ? WHERE comment_id = ?`;
|
||||
db.query(query, [text, commentId]);
|
||||
}
|
||||
@@ -40,23 +54,28 @@ async function deleteComment(db: DB, commentId: string): Promise<void> {
|
||||
const getPostIdQuery = `SELECT post_id FROM comments WHERE comment_id = ?`;
|
||||
const result = db.query(getPostIdQuery, [commentId]);
|
||||
const postId: any = result[0][0];
|
||||
|
||||
|
||||
const deleteCommentQuery = `DELETE FROM comments WHERE comment_id = ?`;
|
||||
db.query(deleteCommentQuery, [commentId]);
|
||||
|
||||
const updatePostQuery = `UPDATE posts SET comments = comments - 1 WHERE posts_uuid = ?`;
|
||||
|
||||
const updatePostQuery =
|
||||
`UPDATE posts SET comments = comments - 1 WHERE posts_uuid = ?`;
|
||||
db.query(updatePostQuery, [postId]);
|
||||
}
|
||||
|
||||
async function likeComment(db: DB, commentId: string, userId: string): Promise<void> {
|
||||
async function likeComment(
|
||||
db: DB,
|
||||
commentId: string,
|
||||
userId: string,
|
||||
): Promise<void> {
|
||||
const query = `UPDATE comments SET likes = likes + 1 WHERE comment_id = ?`;
|
||||
db.query(query, [commentId]);
|
||||
}
|
||||
|
||||
export {
|
||||
createComment,
|
||||
updateComment,
|
||||
deleteComment,
|
||||
likeComment,
|
||||
getCommentsFromDB,
|
||||
}
|
||||
likeComment,
|
||||
updateComment,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user