Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make 2 apis for format the dashboard (getting specific note) #128

Merged
merged 1 commit into from
May 13, 2024
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
101 changes: 93 additions & 8 deletions server/controllers/note.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,42 @@ const searchNotes = wrapAsync(async (req, res) => {

const addNote = wrapAsync(async (req, res) => {
try {
const { title, description, subject, year, course, fileUrl } = req.body;
const {
title,
description,
thumbnail,
subject,
stream,
course,
year,
semester,
fileUrl,
} = req.body;
const author = req.user._id;
if (!title || !description || !subject || !year || !course || !fileUrl) {
if (
!title ||
!description ||
!subject ||
!stream ||
!course ||
!year ||
!semester ||
!fileUrl
) {
return res.status(400).json({ message: "Missing fields" });
}
const note = await Note.create({
title,
description,
thumbnail:
thumbnail ||
"https://www.umass.edu/studentsuccess/sites/default/files/inline-images/cornell-note-taking-strategy.jpg",
author,
subject,
year,
stream,
course,
year,
semester,
fileUrl,
});
if (!note) {
Expand All @@ -52,11 +76,10 @@ const addNote = wrapAsync(async (req, res) => {
}
user.notes.push(note);
//add the coins to the user 10 coins per note
if(user.coins){
user.coins+=10;
}
else{
user.coins=10;
if (user.coins) {
user.coins += 10;
} else {
user.coins = 10;
}
await user.save();

Expand Down Expand Up @@ -212,7 +235,67 @@ const getBookMarkedNotesByUser = async (req, res) => {
});
}
};
const getSpecificNotesController = async (req, res) => {
try {
const { stream, course, year, semester, subject } = req.params;

const notes = await Note.find({
stream: stream,
course: course,
year: parseInt(year),
semester: semester,
subject: subject,
}).populate("author");
if (notes.length === 0) {
return res
.status(404)
.json({ message: "No notes found for the provided criteria." });
}

res.status(200).json({ notes });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server Error" });
}
};
const getFormatedNote = async (req, res) => {
try {
const notes = await Note.find();
if (notes.length === 0) {
return res.status(404).json({ message: "No notes found" });
}
const formattedNotes = {};
notes.forEach((note) => {
if (!formattedNotes[note.stream]) {
formattedNotes[note.stream] = {};
}
if (!formattedNotes[note.stream][note.course]) {
formattedNotes[note.stream][note.course] = {};
}
if (!formattedNotes[note.stream][note.course][note.semester]) {
formattedNotes[note.stream][note.course][note.semester] = [];
}
formattedNotes[note.stream][note.course][note.semester].push({
id: note._id,
title: note.title,
description: note.description,
author: note.author,
subject: note.subject,
year: note.year,
fileUrl: note.fileUrl,
likes: note.likes,
likedBy: note.likedBy,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
});
});

res.status(200).json({ notes: formattedNotes });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server Error" });
}
};
module.exports = {
searchNotes,
addNote,
Expand All @@ -221,4 +304,6 @@ module.exports = {
checkIfLiked,
bookMarkNotes,
getBookMarkedNotesByUser,
getSpecificNotesController,
getFormatedNote,
};
12 changes: 12 additions & 0 deletions server/models/note.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ const noteSchema = new Schema(
required: true,
trim: true,
},
stream: {
type: String,
required: true,
trim: true,
default: "",
},
semester: {
type: String,
required: true,
trim: true,
default: "",
},
description: {
type: String,
required: true,
Expand Down
13 changes: 12 additions & 1 deletion server/routes/note.routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Router = require("express");
const Note = require("../models/note.model.js");
const {
searchNotes,
addNote,
Expand All @@ -7,11 +8,21 @@ const {
checkIfLiked,
bookMarkNotes,
getBookMarkedNotesByUser,
getSpecificNotesController,
getFormatedNote,
} = require("../controllers/note.controller.js");
const verifyToken = require("../middlewares/verifyToken.js");

const router = Router();

//----------------------------------//put all the details and get specific notes
router.get(
"/notes/:stream/:course/:year/:semester/:subject",
verifyToken,
getSpecificNotesController
);
//-----------------------------------//get formated note
router.get("/formatedNote", verifyToken, getFormatedNote);
//-------------------------------
router.post("/", verifyToken, addNote);
router.get("/", verifyToken, searchNotes);
router.get("/like/:noteId", verifyToken, likeNotes);
Expand Down