upload files
This commit is contained in:
78
backend/controllers/comments.controller.js
Normal file
78
backend/controllers/comments.controller.js
Normal 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" });
|
||||
}
|
||||
};
|
||||
64
backend/controllers/userStories.controller.js
Normal file
64
backend/controllers/userStories.controller.js
Normal 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user