Skip to content
Open
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
11 changes: 7 additions & 4 deletions backend/Registration/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import express from "express";
import * as path from 'path';
import bodyParser from "body-parser";
import cors from "cors";
import cookieParser from "cookie-parser";
const userrouter = require("./routes/user/user.route")


const app = express();
app.use('/uploads', express.static('public/uploads'));

app.use(cors());
app.use(bodyParser.json());
app.use(cookieParser());
app.use("/user",userrouter)
// app.get("/", (req , res) => {
// res.send("Hello World!");
// });
app.use("/",userrouter)
app.get("/", (req , res) => {
res.send("Server is running.");
});

export = app;
2 changes: 1 addition & 1 deletion backend/Registration/src/models/student.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let studentSchema = new mongoose.Schema({
},
Stud_Image:
{
type:Buffer,
type:String,
required:false
},
Stud_Name:{
Expand Down
50 changes: 50 additions & 0 deletions backend/Registration/src/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import mongoose, { Document, Schema, Model } from 'mongoose';

type UserDocument = Document & {
name: string;
picture: string;
};

type UserInput = {
name: UserDocument['name'];
picture: UserDocument['picture'];
};

const userSchema = new Schema(
{
name: {
type: String,
required: true,
},
picture: {
type: String,
required: true,
},
},
{
timestamps: true,
collection: 'col_users',
},
);


const newUserSchema = new Schema(
{
name: {
type: String,
required: true,
},
picture: {
type: String,
required: true,
},
},
{
timestamps: true,
collection: 'col_users',
},
);

const User: Model<UserDocument> = mongoose.model<UserDocument>('User', userSchema);

export { User, UserDocument, UserInput };
265 changes: 157 additions & 108 deletions backend/Registration/src/routes/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { Request,Response } from "express";
import { Request, Response } from "express";
import * as path from 'path';
import mongoose from "mongoose";
const fs = require('fs');
const csv = require('csv-parser');

let results:any = [];
import { handleSingleUploadFile } from '../../utils/uploadSingle';
import { User, UserInput } from '../../models/user.model';

export type UploadedFile = {
fieldname: string;
originalname: string;
encoding: string;
mimetype: string;
destination: string;
filename: string;
path: string;
size: number;
};

let results: any = [];

const Student = require('../../models/student.model');
const Staff = require('../../models/staff.model');
Expand All @@ -13,141 +28,175 @@ const Course = require('../../models/course.model');
const Curriculum = require('../../models/curriculum.model');
const Assignment = require('../../models/Assignment.model');

export const getAllStudents = async (req: Request, res: Response) => {
try {
const students = await Student.find({});
res.status(200).json(students);
} catch (error: any) {
res.status(500).json({ message: error.message });
}
};

export const getStudentImage = async (req: Request, res: Response) => {
try {
const student = await Student.findOne({ Stud_Id : req.params.id });
if (student && student.Stud_Image) {
const imagePath = path.join(__dirname, '../../../public/uploads', student.Stud_Image);
console.log(`Sending file: ${imagePath}`);
res.sendFile(imagePath);
} else {
res.status(404).json({ message: 'Image not found' });
}
} catch (error: any) {
res.status(500).json({ message: error.message });
}
};

export const uploadFile = (req: Request, res: Response) => {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}

const results:any = [];
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}

const results: any = [];

const file: any = req.file


const file:any = req.file

// Process the uploaded CSV file
fs.createReadStream(req.file.path)
.pipe(csv())
.on('data', (data:any) => results.push(data))
.on('end', () => {
// Remove the temporary file
fs.unlinkSync(file.path);

// Do something with the parsed CSV data
console.log(results);

// Return a response
res.json({ message: 'File uploaded and processed successfully' });
})
.on('error', (error:any) => {
// Handle any errors
console.error(error);
res.status(500).json({ error: 'Internal server error' });
});

// Process the uploaded CSV file
fs.createReadStream(req.file.path)
.pipe(csv())
.on('data', (data: any) => results.push(data))
.on('end', () => {
// Remove the temporary file
fs.unlinkSync(file.path);

// Do something with the parsed CSV data
console.log(results);

// Return a response
res.json({ message: 'File uploaded and processed successfully' });
})
.on('error', (error: any) => {
// Handle any errors
console.error(error);
res.status(500).json({ error: 'Internal server error' });
});


};


};


export const registerStaff = (req: Request, res: Response) => {
// Handle student registration logic here

// Handle student registration logic here

try {
const data = req.body;

try {
const data = req.body;


const newStaff = new Staff(data);
newStaff.save()
const newStaff = new Staff(data);
newStaff.save()

res.status(200).json({ message: data });

} catch (error:any) {

res.status(500).json({ message: error.message });

}
};
} catch (error: any) {

res.status(500).json({ message: error.message });

export const registerDependency = async(req: Request, res: Response) => {
// Handle student registration logic here
}
};

export const registerDependency = async (req: Request, res: Response) => {
// Handle student registration logic here

try {
const data = req.body;

try {
const data = req.body;


const newValue = new Assignment(data);
newValue.save()


const newValue = new Assignment(data);
newValue.save()


res.status(200).json({ message: data });

} catch (error:any) {

res.status(500).json({ message: error.message });

}
};
} catch (error: any) {

res.status(500).json({ message: error.message });

}
};


export const registerStudent = async(req: Request, res: Response) => {
// Handle student registration logic here

export const registerStudent = async (req: Request, res: Response) => {
// Handle student registration logic here

try {
let uploadResult;

try {
const data = req.body;
uploadResult = await handleSingleUploadFile('Stud_Image', req, res);
} catch (e: any) {
return res.status(422).json({ errors: [e.message] });
}

const uploadedFile: UploadedFile = uploadResult.file;
console.log(uploadResult);

const { body } = uploadResult;
let data = req.body;
data.Stud_Image = uploadResult.file.filename;
// Add the uploaded file name to the data object

const student = await Student.findOne({Stud_Name: data.Stud_Name});
const student = await Student.findOne({ Stud_Id : data.Stud_Id });

if(student)
{
res.status(400).json({ message: student.Stud_Name });
return;
}
if (student) {
res.status(400).json({ message: student.Stud_Name });
return;
}

const newStudent = await new Student(data);
await newStudent.save()

const newStudent = await new Student(data);
await newStudent.save()
/* const newStatus = await new Status({status:"Active"})
await newStatus.save() */

/* const newStatus = await new Status({status:"Active"})
await newStatus.save() */

res.status(200).json({ message: data });

} catch (error:any) {

res.status(500).json({ message: error.message });

}
};

export const registerStudentCsv = async(req: Request, res: Response) => {

fs.createReadStream('./students.csv')
.pipe(csv())
.on('data', (data:any) => {
// Process each row of data
results.push(data);
})
.on('end', () => {
// The parsing is complete
console.log(results);

Student.create(results)
.then(() => {
console.log('Data inserted successfully');
res.status(200).json({message:"Data inserted successfully"});
})
.catch((error:any) => {
console.error('Error inserting data:', error);
} catch (error: any) {

res.status(500).json({ message: error.message });
});
results = []
})
.on('error', (error:any) => {
// Handle any errors that occur during the parsing
console.error(error);
});

};

}
};

export const registerStudentCsv = async (req: Request, res: Response) => {

fs.createReadStream('./students.csv')
.pipe(csv())
.on('data', (data: any) => {
// Process each row of data
results.push(data);
})
.on('end', () => {
// The parsing is complete
console.log(results);

Student.create(results)
.then(() => {
console.log('Data inserted successfully');
res.status(200).json({ message: "Data inserted successfully" });
})
.catch((error: any) => {
console.error('Error inserting data:', error);
res.status(500).json({ message: error.message });
});
results = []
})
.on('error', (error: any) => {
// Handle any errors that occur during the parsing
console.error(error);
});

};
Loading