farheen-psql-app 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/controllers/commentController.js +6 -0
- package/src/controllers/postController.js +12 -0
- package/src/database/schemas/commentSchema.js +21 -0
- package/src/database/schemas/permissionSchema.js +1 -2
- package/src/database/schemas/postSchema.js +15 -0
- package/src/database/schemas/roleSchema.js +0 -1
- package/src/database/schemas/userSchema.js +2 -4
- package/src/middleware/authentication.js +5 -1
- package/src/models/comment.js +17 -0
- package/src/models/post.js +36 -0
- package/src/repositories/commentRepository.js +7 -0
- package/src/repositories/postRepository.js +14 -0
- package/src/services/commentService.js +5 -0
- package/src/services/permissionService.js +7 -15
- package/src/services/postService.js +35 -0
package/package.json
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createPostService, getPostCommentsService } from "../services/postService.js";
|
|
2
|
+
|
|
3
|
+
export const createPostHandler = (req, res) => {
|
|
4
|
+
const result = await createPostService(req.body);
|
|
5
|
+
res.status(201).json(result);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const getPostCommentsHandler = (req, res) => {
|
|
9
|
+
const post_id = req.body;
|
|
10
|
+
const result = await getPostCommentsService(post_id);
|
|
11
|
+
res.status(200).json(result);
|
|
12
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const commentTable = `
|
|
2
|
+
CREATE TABLE IF NOT EXISTS comments (
|
|
3
|
+
id SERIAL PRIMARY KEY,
|
|
4
|
+
post_id INTEGER NOT NULL,
|
|
5
|
+
user_id INTEGER NOT NULL,
|
|
6
|
+
parent_id INTEGER NOT NULL,
|
|
7
|
+
comment VARCHAR(200) NOT NULL,
|
|
8
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
9
|
+
|
|
10
|
+
FOREIGN KEY(post_id)
|
|
11
|
+
REFERENCES posts(id)
|
|
12
|
+
|
|
13
|
+
FOREIGN KEY(user_id)
|
|
14
|
+
REFERENCES users(id)
|
|
15
|
+
|
|
16
|
+
FOREIGN KEY(parent_id)
|
|
17
|
+
REFERENCES comments(id)
|
|
18
|
+
);
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
export default commentTable;
|
|
@@ -2,10 +2,9 @@ const permissionTable = `
|
|
|
2
2
|
CREATE TABLE IF NOT EXISTS permissions (
|
|
3
3
|
id SERIAL PRIMARY KEY,
|
|
4
4
|
module VARCHAR(100) NOT NULL,
|
|
5
|
-
action
|
|
5
|
+
action JSONB NOT NULL,
|
|
6
6
|
description TEXT,
|
|
7
7
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
8
|
-
UNIQUE(module, action)
|
|
9
8
|
);
|
|
10
9
|
`;
|
|
11
10
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const postTable = `
|
|
2
|
+
CREATE TABLE IF NOT EXISTS posts (
|
|
3
|
+
id SERIAL PRIMARY KEY,
|
|
4
|
+
user_id INTEGER NOT NULL,
|
|
5
|
+
content VARCHAR(200) NOT NULL,
|
|
6
|
+
|
|
7
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
8
|
+
|
|
9
|
+
FOREIGN KEY(user_id)
|
|
10
|
+
REFERENCES users(id)
|
|
11
|
+
ON DELETE CASCADE
|
|
12
|
+
);
|
|
13
|
+
`;
|
|
14
|
+
|
|
15
|
+
export default postTable;
|
|
@@ -2,15 +2,13 @@ const userTable = `
|
|
|
2
2
|
CREATE TABLE IF NOT EXISTS users (
|
|
3
3
|
id SERIAL PRIMARY KEY,
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
last_name VARCHAR(100),
|
|
5
|
+
name VARCHAR(100) NOT NULL,
|
|
8
6
|
|
|
9
7
|
email VARCHAR(255) UNIQUE NOT NULL,
|
|
10
8
|
|
|
11
9
|
password TEXT NOT NULL,
|
|
12
10
|
|
|
13
|
-
role_id INTEGER,
|
|
11
|
+
role_id INTEGER NOT NULL,
|
|
14
12
|
|
|
15
13
|
is_active BOOLEAN DEFAULT TRUE,
|
|
16
14
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const createPostQuery = `
|
|
2
|
+
INSERT INTO posts (
|
|
3
|
+
user_id,
|
|
4
|
+
content,
|
|
5
|
+
created_at
|
|
6
|
+
)
|
|
7
|
+
VALUES(
|
|
8
|
+
$1,
|
|
9
|
+
$2,
|
|
10
|
+
NOW()
|
|
11
|
+
)
|
|
12
|
+
RETURNING *;
|
|
13
|
+
`;
|
|
14
|
+
|
|
15
|
+
export const getAllPosts = `
|
|
16
|
+
SELECT * FROM posts ORDER BY created_at ASC;
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
export const getNestedCommentsByPost = `
|
|
20
|
+
SELECT
|
|
21
|
+
|
|
22
|
+
c.id,
|
|
23
|
+
c.comment,
|
|
24
|
+
c.parent_id,
|
|
25
|
+
u.id,
|
|
26
|
+
u.name
|
|
27
|
+
|
|
28
|
+
FROM comments c
|
|
29
|
+
|
|
30
|
+
LEFT JOIN users u
|
|
31
|
+
ON u.id = c.user_id
|
|
32
|
+
|
|
33
|
+
WHERE c.post_id = $1
|
|
34
|
+
|
|
35
|
+
ORDER BY created_at;
|
|
36
|
+
`;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import pool from "../config/db.js"
|
|
2
|
+
import { addCommentQuery } from "../models/comment.js"
|
|
3
|
+
|
|
4
|
+
export const createComment = async (data) => {
|
|
5
|
+
const result = await pool.query(addCommentQuery, [data.user_id, data.post_id, data.comment, data.parent_id]);
|
|
6
|
+
return result.rows[0];
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import pool from "../config/db.js";
|
|
2
|
+
import { createPostQuery, getNestedCommentsByPost } from "../models/post.js";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export const createPost = (data) => {
|
|
6
|
+
const result = await pool.query(createPostQuery, [data.user_id, data.content]);
|
|
7
|
+
return result.rows[0];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const getPostComments = (post_id) => {
|
|
11
|
+
const result = await pool.query(getNestedCommentsByPost, [post_id]);
|
|
12
|
+
return result.rows;
|
|
13
|
+
}
|
|
14
|
+
|
|
@@ -1,21 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
from "../repositories/permissionRepository.js";
|
|
1
|
+
import { createPermission, deletePermission, getAllPermissions } from "../repositories/permissionRepo.js";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return await permissionRepository
|
|
8
|
-
.createPermission(data);
|
|
3
|
+
export const createPermissionService = async (data) => {
|
|
4
|
+
return await createPermission(data);
|
|
9
5
|
}
|
|
10
6
|
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
return await permissionRepository
|
|
14
|
-
.getAllPermissions();
|
|
7
|
+
export const getAllPermissionsService = async () => {
|
|
8
|
+
return await getAllPermissions();
|
|
15
9
|
}
|
|
16
10
|
|
|
17
|
-
export const
|
|
18
|
-
|
|
19
|
-
return await permissionRepository
|
|
20
|
-
.deletePermission(id);
|
|
11
|
+
export const deletePermissionService = async (id) => {
|
|
12
|
+
return await deletePermission(id);
|
|
21
13
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { getAllRolesQuery } from "../models/role";
|
|
2
|
+
import { createPost, getPostComments } from "../repositories/postRepository.js";
|
|
3
|
+
import { createRole } from "../repositories/roleRepository.js";
|
|
4
|
+
|
|
5
|
+
export const createPostService = async (data) => {
|
|
6
|
+
return await createPost(data);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const getPostCommentsService = async (post_id) => {
|
|
10
|
+
|
|
11
|
+
const comments = await getPostComments(post_id);
|
|
12
|
+
return buildCommentTree(comments);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const buildCommentTree = (comments) => {
|
|
16
|
+
const commentMap = {};
|
|
17
|
+
const rootComment = [];
|
|
18
|
+
|
|
19
|
+
comments.forEach(comment => {
|
|
20
|
+
commentMap[comment.id] = {
|
|
21
|
+
...comment,
|
|
22
|
+
replies: []
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
comments.forEach(comment => {
|
|
27
|
+
if (comment.parent_id) {
|
|
28
|
+
commentMap[comment.parent_id].replies.push(comment.id)
|
|
29
|
+
} else {
|
|
30
|
+
rootComment.push(commentMap[comment.id])
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return rootComment;
|
|
35
|
+
}
|