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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "farheen-psql-app",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -0,0 +1,6 @@
1
+ import { createCommentService } from "../services/commentService.js";
2
+
3
+ export const createCommentsHandler = (req, res) => {
4
+ const result = await createCommentService(req.body);
5
+ res.status(201).json(result);
6
+ }
@@ -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 VARCHAR(100) NOT NULL,
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;
@@ -3,7 +3,6 @@ CREATE TABLE IF NOT EXISTS roles (
3
3
  id SERIAL PRIMARY KEY,
4
4
  role_name VARCHAR(100) UNIQUE NOT NULL,
5
5
  description TEXT,
6
- is_active BOOLEAN DEFAULT TRUE,
7
6
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
8
7
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
9
8
  );
@@ -2,15 +2,13 @@ const userTable = `
2
2
  CREATE TABLE IF NOT EXISTS users (
3
3
  id SERIAL PRIMARY KEY,
4
4
 
5
- first_name VARCHAR(100) NOT NULL,
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
 
@@ -1,5 +1,9 @@
1
1
  export const authorization = (module, action) => {
2
2
  async (req, res, next) => {
3
- const user = await U
3
+ if(!req.user || !req.user.permissions){
4
+ return res.status(403).json({ error: "Unauthorized: No access token"})
5
+ }
6
+
7
+ const required
4
8
  }
5
9
  }
@@ -0,0 +1,17 @@
1
+ export const addCommentQuery = `
2
+ INSERT INTO comments (
3
+ user_id,
4
+ post_id,
5
+ comment,
6
+ parent_id,
7
+ created_at
8
+ )
9
+ VALUES (
10
+ $1,
11
+ $2,
12
+ $3,
13
+ $4,
14
+ NOW()
15
+ )
16
+ RETURNING *;
17
+ `;
@@ -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
+
@@ -0,0 +1,5 @@
1
+ import { createComment } from "../repositories/commentRepository"
2
+
3
+ export const createCommentService = async (data) => {
4
+ return await createComment(data);
5
+ }
@@ -1,21 +1,13 @@
1
- import permissionRepository
2
- from "../repositories/permissionRepository.js";
1
+ import { createPermission, deletePermission, getAllPermissions } from "../repositories/permissionRepo.js";
3
2
 
4
-
5
- export const createPermission = async (data) => {
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 getAllPermissions = async () => {
12
-
13
- return await permissionRepository
14
- .getAllPermissions();
7
+ export const getAllPermissionsService = async () => {
8
+ return await getAllPermissions();
15
9
  }
16
10
 
17
- export const deletePermission = async (id) => {
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
+ }