Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #42 from SU-CS308-22FA/comment_sevval
Browse files Browse the repository at this point in the history
comment/ like feature added
  • Loading branch information
sevvalkaradeniz authored Nov 22, 2022
2 parents 5ae33d9 + b4dcd96 commit 857eb0b
Show file tree
Hide file tree
Showing 11 changed files with 930 additions and 0 deletions.
137 changes: 137 additions & 0 deletions backend/controllers/commentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import Comment from "../models/commentModel.js";
import asyncHandler from "express-async-handler";
import User from "../models/userModel.js";

// @desc Get logged in user comments
// @route GET /api/comments
// @access Private
const getComments = asyncHandler(async (req, res) => {

const comments = await Comment.find(); //{ user: req.user._id }
res.json(comments);

});


//@description Fetch single Comment
//@route GET /api/comments/:id
//@access Public
const getCommentById = asyncHandler(async (req, res) => {
const comment = await Comment.findById(req.params.id);

if (comment) {
res.json(comment);
} else {
res.status(404).json({ message: "Comment not found" });
}

res.json(comment);
});

//@description Create single Comment
//@route GET /api/comments/create
//@access Private
const CreateComment = asyncHandler(async (req, res) => {
const { title, content,username} = req.body;

if (!title || !content) {
res.status(400);
throw new Error("Please Fill all the feilds");
return;
} else {


const comment = new Comment({ user: req.user._id, title,content,username});

const createdComment = await comment.save();

res.status(201).json(createdComment);
}
});

//@description Delete single Comment
//@route GET /api/comments/:id
//@access Private
const DeleteComment = asyncHandler(async (req, res) => {
const comment = await Comment.findById(req.params.id);

if (comment.user.toString() !== req.user._id.toString()) {
res.status(401);
throw new Error("You can't perform this action");
}

if (comment) {
await comment.remove();
res.json({ message: "Comment Removed" });
} else {
res.status(404);
throw new Error("Comment not Found");
}
});

// @desc Update a comment
// @route PUT /api/comments/:id
// @access Private
const UpdateComment = asyncHandler(async (req, res) => {
const { title, content} = req.body;

const comment = await Comment.findById(req.params.id);

if (comment.user.toString() !== req.user._id.toString()) {
res.status(401);
throw new Error("You can't perform this action");
}

if (comment) {
comment.title = title;
comment.content = content;


const updatedComment = await comment.save();
res.json(updatedComment);
} else {
res.status(404);
throw new Error("Comment not found");
}
});



const LikeState = asyncHandler(async (req, res) => {
const { title, content,likes,username} = req.body;

const comment = await Comment.findById(req.params.id);
console.log(comment.usersThatLikedTheComment);
console.log(comment.usersThatLikedTheComment.includes(username));


if (comment) {
if(comment.usersThatLikedTheComment.includes(username)==false){
comment.usersThatLikedTheComment.push(username);
comment.title = title;
comment.content = content;
comment.likes=likes+1;
const updatedComment = await comment.save();
res.json(updatedComment);
}
else{
comment.usersThatLikedTheComment.splice(comment.usersThatLikedTheComment.indexOf(username,0),1);
comment.title = title;
comment.content = content;
comment.likes=likes-1;
const updatedComment = await comment.save();
res.json(updatedComment);
}

} else {
res.status(404);
throw new Error("Comment not found");
}


});



export { getCommentById, getComments, CreateComment, DeleteComment, UpdateComment,LikeState};

41 changes: 41 additions & 0 deletions backend/models/commentModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import mongoose from "mongoose";


const commentSchema = mongoose.Schema(
{
title: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
username: {
type: String,
required:true,
unique: false,
},
likes: {
type: Number,
default: 0
},

usersThatLikedTheComment:{
type: Array,
default: [],
},
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "User",
},
},
{
timestamps: true,
}
);

const Comment = mongoose.model("Comment", commentSchema);

export default Comment;
28 changes: 28 additions & 0 deletions backend/routes/commentRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import express from "express";
import {
getCommentById,
getComments,
CreateComment,
DeleteComment,
UpdateComment,
LikeState,

} from "../controllers/commentController.js";
const router = express.Router();
import { protect } from "../middleware/authMiddleware.js";

router.route("/").get(getComments); // protect,
router
.route("/:id")
.get(getCommentById)
.delete(protect, DeleteComment)
.put(protect, UpdateComment);
router.route("/create").post(protect,CreateComment);
router.route("/likes/:id").put(protect,LikeState);





export default router;
2 changes: 2 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import matchRoutes from "./routes/matchRoutes.js";
import requestRoutes from "./routes/requestRoutes.js";
import { errorHandler, notFound } from "./middleware/errorMiddleware.js";
import googleDrive from "./routes/googledrive.js";
import commentRoutes from "./routes/commentRoutes.js";

dotenv.config();

Expand All @@ -23,6 +24,7 @@ app.use("/api/users", userRoutes);
app.use("/app", googleDrive); //-> /app is the base path and routeUrls will be appemded to it
app.use("/api/matches", matchRoutes);
app.use("/api/requests", requestRoutes);
app.use("/api/comments", commentRoutes);

// --------------------------deployment------------------------------
const __dirname = path.resolve();
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import ProfileScreen from "./screens/ProfileScreen/ProfileScreen";
import AdminPage from "./screens/AdminPage/AdminPage";
import FixturePage from "./screens/FixturePage/FixturePage";
import StandingPage from "./screens/StandingPage/StandingPage";
import CreateComment from "./screens/CreateComment/CreateComment";
import MyComments from "./screens/MyComments/MyComments";
import SingleComment from "./screens/CreateComment/SingleComment";

function App() {
const [search, setSearch] = useState("");
Expand All @@ -27,6 +30,14 @@ function App() {
<Route path="/adminpage" component={AdminPage} />
<Route path="/fixture" component={FixturePage} />
<Route path="/standings" component={StandingPage} />
<Route
path="/mycomments"
component={({ history }) => (
<MyComments search={search} history={history} />
)}
/>
<Route path="/comment/:id" component={SingleComment} />
<Route path="/createcomment" component={CreateComment} />;
</main>
<Footer />
</Router>
Expand Down
Loading

0 comments on commit 857eb0b

Please sign in to comment.