-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0cf41f
commit 49800e1
Showing
11 changed files
with
1,222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const Student = require('../models/studentModel'); | ||
|
||
// Controller functions | ||
// Get marks of all students | ||
exports.getAllStudentsMarks = async (req, res) => { | ||
try { | ||
const students = await Student.find({}, 'name marks'); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: students | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Calculate percentage of a student | ||
exports.calculatePercentage = async (req, res) => { | ||
try { | ||
const studentId = req.params.id; | ||
const student = await Student.findById(studentId); | ||
if (!student) { | ||
return res.status(404).json({ | ||
status: 'fail', | ||
message: 'Student not found' | ||
}); | ||
} | ||
const totalMarks = student.marks.reduce((acc, curr) => acc + curr, 0); | ||
const percentage = (totalMarks / (student.marks.length * 100)) * 100; // Assuming each mark is out of 100 | ||
res.status(200).json({ | ||
status: 'success', | ||
data: { | ||
percentage: percentage.toFixed(2) + '%' | ||
} | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Generate overall result of a student | ||
exports.generateResult = async (req, res) => { | ||
try { | ||
const studentId = req.params.id; | ||
const student = await Student.findById(studentId); | ||
if (!student) { | ||
return res.status(404).json({ | ||
status: 'fail', | ||
message: 'Student not found' | ||
}); | ||
} | ||
const totalMarks = student.marks.reduce((acc, curr) => acc + curr, 0); | ||
const percentage = (totalMarks / (student.marks.length * 100)) * 100; // Assuming each mark is out of 100 | ||
let result; | ||
if (percentage >= 60) { | ||
result = 'Pass'; | ||
} else { | ||
result = 'Fail'; | ||
} | ||
res.status(200).json({ | ||
status: 'success', | ||
data: { | ||
result: result | ||
} | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Generate result of a particular subject for all students | ||
exports.generateSubjectResult = async (req, res) => { | ||
try { | ||
const subject = req.params.subject; | ||
const students = await Student.find(); | ||
const subjectResults = students.map(student => ({ | ||
name: student.name, | ||
result: student.marks[subject] >= 60 ? 'Pass' : 'Fail' // Assuming marks are stored as an object with subject as key and marks as value | ||
})); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: subjectResults | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; |
155 changes: 155 additions & 0 deletions
155
New_APIs/Result_Marks_API/Controllers/teacherController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
const Student = require('../models/studentModel'); | ||
|
||
// Controller functions | ||
// Get all students' marks | ||
exports.getAllStudentsMarks = async (req, res) => { | ||
try { | ||
const students = await Student.find(); | ||
const studentsMarks = students.map(student => ({ | ||
name: student.name, | ||
marks: student.marks, | ||
grade: student.grade | ||
})); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: studentsMarks | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Get marks of a particular student | ||
exports.getStudentMarks = async (req, res) => { | ||
try { | ||
const studentId = req.params.id; | ||
const student = await Student.findById(studentId); | ||
if (!student) { | ||
return res.status(404).json({ | ||
status: 'fail', | ||
message: 'Student not found' | ||
}); | ||
} | ||
res.status(200).json({ | ||
status: 'success', | ||
data: { | ||
name: student.name, | ||
marks: student.marks, | ||
grade: student.grade | ||
} | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Add marks for a student | ||
exports.addMarks = async (req, res) => { | ||
try { | ||
const { studentId, marks } = req.body; | ||
const student = await Student.findByIdAndUpdate(studentId, { $push: { marks: marks } }, { new: true }); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: student | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Update marks of a student | ||
exports.updateMarks = async (req, res) => { | ||
try { | ||
const { studentId, markIndex, newMarks } = req.body; | ||
const student = await Student.findById(studentId); | ||
if (!student) { | ||
return res.status(404).json({ | ||
status: 'fail', | ||
message: 'Student not found' | ||
}); | ||
} | ||
student.marks[markIndex] = newMarks; | ||
await student.save(); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: student | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Delete marks of a student | ||
exports.deleteMarks = async (req, res) => { | ||
try { | ||
const { studentId, markIndex } = req.body; | ||
const student = await Student.findById(studentId); | ||
if (!student) { | ||
return res.status(404).json({ | ||
status: 'fail', | ||
message: 'Student not found' | ||
}); | ||
} | ||
student.marks.splice(markIndex, 1); | ||
await student.save(); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: student | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Add grade for a student | ||
exports.addGrade = async (req, res) => { | ||
try { | ||
const { studentId, grade } = req.body; | ||
const student = await Student.findByIdAndUpdate(studentId, { $set: { grade: grade } }, { new: true }); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: student | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; | ||
|
||
// Fetch marks of a particular subject | ||
exports.getSubjectMarks = async (req, res) => { | ||
try { | ||
const subject = req.params.subject; | ||
const students = await Student.find(); | ||
const subjectMarks = students.map(student => ({ | ||
name: student.name, | ||
marks: student.marks[subject] // Assuming marks are stored as an object with subject as key and marks as value | ||
})); | ||
res.status(200).json({ | ||
status: 'success', | ||
data: subjectMarks | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
status: 'error', | ||
message: err.message | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Result-marks API | ||
|
||
This is a Node.js application for managing result markers | ||
|
||
|
||
|
||
### Installation | ||
|
||
1. **Clone the repository:** | ||
|
||
2. **Install dependencies:** | ||
|
||
3. **Set up MongoDB:** | ||
- Ensure that MongoDB is installed and running on your local machine or accessible via a URI if it's hosted remotely. | ||
- Update the MongoDB URI in the `index.js` file to point to your MongoDB instance. Replace `'mongodb://localhost/apiverse'` with your actual MongoDB URI. | ||
|
||
### Usage | ||
|
||
1. **Start the server:** | ||
|
||
2. **Access the APIs:** | ||
- The server will start running on `http://localhost:3000` (or the port specified in `index.js`). | ||
- You can use tools like Postman or cURL to send HTTP requests to the endpoints mentioned below. | ||
|
||
3. **API Endpoints:** | ||
- **Students API:** | ||
- `GET /api/students`: Get all students. | ||
- `POST /api/students`: Add a new student. | ||
- `GET /api/students/:id`: Get a specific student by ID. | ||
- `PUT /api/students/:id`: Update a student by ID. | ||
- `DELETE /api/students/:id`: Delete a student by ID. | ||
- **Teachers API:** | ||
- `GET /api/teachers`: Get all teachers. | ||
- `POST /api/teachers`: Add a new teacher. | ||
- `GET /api/teachers/:id`: Get a specific teacher by ID. | ||
- `PUT /api/teachers/:id`: Update a teacher by ID. | ||
- `DELETE /api/teachers/:id`: Delete a teacher by ID. | ||
|
||
|
||
|
||
i hope this helps you it is glade to work with APIverse | ||
thank you |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
const mongoose = require('mongoose'); | ||
|
||
const studentRoutes = require('./routes/studentRoutes'); | ||
const teacherRoutes = require('./routes/teacherRoutes'); | ||
|
||
const app = express(); | ||
|
||
// Middleware | ||
app.use(bodyParser.json()); | ||
|
||
|
||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// API routes | ||
app.use('/api/students', studentRoutes); | ||
app.use('/api/teachers', teacherRoutes); | ||
|
||
// Connect to MongoDB (replace 'mongodb://localhost/apiverse' with your MongoDB URI) | ||
mongoose.connect('mongodb://localhost/apiverse', { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
useCreateIndex: true, | ||
useFindAndModify: false | ||
}).then(() => console.log('Connected to MongoDB')) | ||
.catch(err => console.error('Error connecting to MongoDB:', err)); | ||
|
||
// Start the server | ||
const port = process.env.PORT || 3000; | ||
app.listen(port, () => { | ||
console.log(`Server is running on port ${port}`); | ||
}); |
Oops, something went wrong.