upload files

This commit is contained in:
sbinsalman
2025-11-25 11:11:42 -07:00
commit 36f9a5e69b
41 changed files with 1971 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
import Comment from "../models/comments.model.js";
import { StatusCodes } from "http-status-codes";
// POST /api/comments
export const createComment = async (req, res) =>
{
try
{
const { userStoryId, commentText, commentedBy } = req.body;
if (!userStoryId || !commentText)
{
return res.status(StatusCodes.BAD_REQUEST).json({ message: "userStoryId and text are required" });
}
const comment = new Comment({userStoryId,commentText,commentedBy});
const saved = await comment.save();
return res.status(StatusCodes.CREATED).json(saved);
}
catch (err)
{
console.error("Error creating comment", err);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ message: "Error creating comment" });
}
};
// PUT /api/comments/:id
export const updateComment = async (req, res) =>
{
try
{
const { id } = req.params;
const { commentText } = req.body;
if (!commentText)
{
return res.status(StatusCodes.BAD_REQUEST).json({ message: "text is required" });
}
const updated = await Comment.findByIdAndUpdate(id,{ commentText },{ new: true });
if (!updated)
{
return res.status(StatusCodes.NOT_FOUND).json({ message: "Comment not found" });
}
return res.status(StatusCodes.OK).json(updated);
}
catch (err)
{
console.error("Error updating comment", err);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ message: "Error updating comment" });
}
};
// DELETE /api/comments/:id
export const deleteComment = async (req, res) =>
{
try
{
const { id } = req.params;
const deleted = await Comment.findByIdAndDelete(id);
if (!deleted)
{
return res.status(StatusCodes.NOT_FOUND).json({ message: "Comment not found" });
}
return res.status(StatusCodes.OK).json({ message: "Comment deleted successfully" });
}
catch (err)
{
console.error("Error deleting comment", err);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ message: "Error deleting comment" });
}
};

View File

@@ -0,0 +1,64 @@
import UserStory from "../models/userStories.model.js";
import { StatusCodes } from "http-status-codes";
import Comment from "../models/comments.model.js";
export async function createUserStories(req, res) {
try {
const userStory = new UserStory(req.body);
const savedStory = await userStory.save();
res.status(StatusCodes.CREATED).json(savedStory);
} catch (e) {
res.status(StatusCodes.NOT_FOUND).json({ error: e.message });
}
}
export async function getUserStory(req, res) {
try {
const userStory = await UserStory.findById(req.params.id).populate({
path: "comments",
options: { sort: { createdAt: -1 } },
});
if (!userStory) {
return res.status(StatusCodes.NOT_FOUND).json({ message: "User story not found" });
}
return res.status(StatusCodes.OK).json(userStory);
} catch (error) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ message: error.message });
}
}
export async function updateUserStory(req, res) {
try {
const updatedStory = await UserStory.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!updatedStory) {
return res.status(StatusCodes.NOT_FOUND).json({ message: "User story not found" });
}
res.status(StatusCodes.OK).json(updatedStory);
} catch (error) {
res.status(StatusCodes.BAD_REQUEST).json({ message: error.message });
}
}
export async function deleteUserStory(req, res) {
try {
const out = await UserStory.findByIdAndDelete(req.params.id);
if (!out) return res.status(StatusCodes.NOT_FOUND).json({ error: "User story not found" });
res.status(StatusCodes.OK).json({ message: "User story deleted successfully" });
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: e.message });
}
}
export async function listUserStories(req, res) {
try {
const stories = await UserStory.find();
res.status(StatusCodes.OK).json(stories);
} catch (e) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: e.message });
}
}