Skip to content

Commit

Permalink
result-marks api with more features
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashgabani845 committed May 27, 2024
1 parent d0cf41f commit 49800e1
Show file tree
Hide file tree
Showing 11 changed files with 1,222 additions and 0 deletions.
1 change: 1 addition & 0 deletions New_APIs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
|[Authentication API](./auth_API/)|This Authentication API provides endpoints for user registration, login, forgot password, and reset password functionalities. |
|[AWS S3 Multimedia Storage Client API](./aws-client-api/)|This is a client API that facilitates interaction with an AWS S3 bucket for storing and retrieving multimedia files. By utilizing the provided functions, you can easily upload and download multimedia files to and from the S3 bucket.|
|[Music API Web Application](./music-api/)|You can access any song and listen to it. Below are some useful links and information.|
[Result-marks Data api ](./Result-marks_API/)|this is for those who want to make college management project they can simple take this api and connect to project easily|
99 changes: 99 additions & 0 deletions New_APIs/Result_Marks_API/Controllers/studentContoller.js
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 New_APIs/Result_Marks_API/Controllers/teacherController.js
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
});
}
};
42 changes: 42 additions & 0 deletions New_APIs/Result_Marks_API/README.md
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
34 changes: 34 additions & 0 deletions New_APIs/Result_Marks_API/index.js
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}`);
});
Loading

0 comments on commit 49800e1

Please sign in to comment.