From 1095a0352600c181de179d8e00823b3a4caccbf4 Mon Sep 17 00:00:00 2001 From: "markiian.babiak" Date: Thu, 17 Jul 2025 02:08:33 -0400 Subject: [PATCH] Updated posts API calls --- backend/src/api/posts.ts | 12 ++++++- backend/src/repository/dalPost.ts | 53 ++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/backend/src/api/posts.ts b/backend/src/api/posts.ts index 01c6b826..1044fbf7 100644 --- a/backend/src/api/posts.ts +++ b/backend/src/api/posts.ts @@ -14,8 +14,18 @@ router.get('/:id', async (req, res) => { router.get('/boards/:id', async (req, res) => { const id = req.params.id; const { type } = req.query; + const { amount } = req.query; + const { userId } = req.query; - const posts = await dalPost.getByBoard(id, type); + const posts = await dalPost.getByBoard(id, type, amount, userId); + res.json(posts); +}); + +router.get('/users/:id', async (req, res) => { + const id = req.params.id; + const { type } = req.query; + + const posts = await dalPost.getByUserId(id, type); res.json(posts); }); diff --git a/backend/src/repository/dalPost.ts b/backend/src/repository/dalPost.ts index 55b6587f..92cf4d91 100644 --- a/backend/src/repository/dalPost.ts +++ b/backend/src/repository/dalPost.ts @@ -7,7 +7,7 @@ import { Options } from '../utils/api.helpers'; import dalBucket from './dalBucket'; import dalComment from './dalComment'; import dalVote from './dalVote'; -import User, { Role } from '../models/User'; +import User, { Role } from '../models/User'; import { STUDENT_POST_COLOR, TEACHER_POST_COLOR } from '../utils/Utils'; export const getById = async (id: string) => { @@ -19,6 +19,20 @@ export const getById = async (id: string) => { } }; +export const getByUserId = async (id: string, type?: any) => { + try { + let posts; + if (type) { + posts = await Post.find({ userID: id, type: type }); + } else { + posts = await Post.find({ userID: id }); + } + return posts; + } catch (err) { + throw new Error(JSON.stringify(err, null, ' ')); + } +}; + export const getManyById = async (ids: string[]) => { try { const post = await Post.find({ postID: ids }); @@ -28,14 +42,25 @@ export const getManyById = async (ids: string[]) => { } }; -export const getByBoard = async (boardID: string, type?: any) => { +export const getByBoard = async ( + boardID: string, + type?: any, + amount?: any, + userId?: any +) => { try { - let posts; - if (type) { - posts = await Post.find({ boardID: boardID, type: type }); - } else { - posts = await Post.find({ boardID }); + const request: any = { boardID }; + if (type) request.type = type; + if (userId) request.userID = userId; + + let postRequest = Post.find(request).sort({ updatedAt: -1 }); + + if (amount) { + postRequest = postRequest.limit(amount); } + + const posts = await postRequest.exec(); + return posts; } catch (err) { throw new Error(JSON.stringify(err, null, ' ')); @@ -68,12 +93,17 @@ export const create = async (post: PostModel) => { // 1. Determine the correct default color by looking up the user's role. let defaultFillColor = STUDENT_POST_COLOR; // Default to the most common color. try { - const user = await User.findOne({ userID: post.userID }).select('role').lean(); + const user = await User.findOne({ userID: post.userID }) + .select('role') + .lean(); if (user?.role === Role.TEACHER) { defaultFillColor = TEACHER_POST_COLOR; } } catch (e) { - console.error("Could not fetch user role for default color. Falling back to default.", e); + console.error( + 'Could not fetch user role for default color. Falling back to default.', + e + ); } // 2. Define the complete default structure using our dynamic color. @@ -87,10 +117,10 @@ export const create = async (post: PostModel) => { // The incoming 'post' values will overwrite our defaults if they exist. post.displayAttributes = { ...defaultDisplayAttributes, // Establishes our smart defaults (including the correct color) - ...post.displayAttributes, // Client-provided attributes (like a specific color) overwrite the defaults + ...post.displayAttributes, // Client-provided attributes (like a specific color) overwrite the defaults position: { ...defaultDisplayAttributes.position, // Ensures position object has defaults - ...(post.displayAttributes?.position), // Client-provided position values overwrite default position + ...post.displayAttributes?.position, // Client-provided position values overwrite default position }, }; try { @@ -183,6 +213,7 @@ const dalPost = { remove, removeByBoard, update, + getByUserId, }; export default dalPost;