Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion backend/src/api/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
53 changes: 42 additions & 11 deletions backend/src/repository/dalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 });
Expand All @@ -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, ' '));
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -183,6 +213,7 @@ const dalPost = {
remove,
removeByBoard,
update,
getByUserId,
};

export default dalPost;
Loading