-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactorin back end, and fixing Class Schema #50
- Loading branch information
1 parent
e473ffd
commit 89e6468
Showing
7 changed files
with
204 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Request, Response } from 'express'; | ||
import { AvaliationModel, AvaliationDocument } from '../model/Avaliation'; | ||
|
||
export async function createAvaliationController (req: Request, res: Response) { | ||
try { | ||
const { typeAvaliation, themeAvaliation, questions, initialAvaliation, finalAvaliation, time, points } = req.body; | ||
console.log(req.body); | ||
const NewAvaliation = new AvaliationModel({ | ||
typeAvaliation, | ||
themeAvaliation, | ||
questions, | ||
initialAvaliation, | ||
finalAvaliation, | ||
time, | ||
points, | ||
}); | ||
|
||
const createAvaliation = await NewAvaliation.save(); | ||
|
||
res.status(201).json({ message: 'Form created successfully', createAvaliation }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error creating form' }); | ||
} | ||
} | ||
|
||
|
||
export async function getAvaliationsController (req: Request, res: Response) { | ||
try { | ||
const Avaliations = await AvaliationModel.find(); | ||
res.status(200).json({ message: 'Avaliations found successfully', Avaliations }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Error finding Avaliations' }); | ||
} | ||
} |
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,33 @@ | ||
import { Request, Response } from 'express'; | ||
import { Class, IClass } from '../model/Class'; | ||
|
||
|
||
export async function createClassController (req: Request, res: Response) { | ||
try { | ||
const { className, classSummary, professor, students } = req.body; | ||
|
||
const newClass: IClass = new Class({ | ||
className, | ||
classSummary, | ||
professor, | ||
students | ||
}); | ||
|
||
const savedClass: IClass = await newClass.save(); | ||
|
||
res.status(201).json({ message: 'Form created successfully', savedClass}); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
export async function getClassesControler(req: Request, res: Response) { | ||
try { | ||
const classes: IClass[] = await Class.find({}); | ||
res.status(200).json({ classes }); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} |
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,112 @@ | ||
import { Request, Response } from 'express'; | ||
import { Professor, IProfessor } from '../model/Professor'; | ||
import { Student, IStudent } from '../model/Student'; | ||
|
||
|
||
export async function createProfessorController (req: Request, res: Response){ | ||
try { | ||
const { name, password, email, user } = req.body; | ||
|
||
const newProfessor: IProfessor = new Professor({ | ||
name, | ||
password, | ||
email, | ||
user, | ||
}); | ||
|
||
const savedProfessor: IProfessor = await newProfessor.save(); | ||
|
||
res.status(201).json({ message: 'Professor created successfully', savedProfessor}); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
export async function getProfessorsController (req: Request, res: Response) { | ||
|
||
try { | ||
const Professors: IProfessor[] = await Professor.find({}); | ||
|
||
Professors.forEach((professor) => { | ||
professor.password = ''; | ||
}); | ||
|
||
console.log(Professors); | ||
res.status(200).json({ Professors }); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
export async function getProfessorByEmailController (req: Request, res: Response){ | ||
try { | ||
const { email } = req.params; | ||
const professor: IProfessor | null = await Professor.findOne({ email }); | ||
if (!professor) { | ||
return res.status(404).json({ message: 'Professor not found' }); | ||
} | ||
res.status(200).json({ professor }); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
export async function createStudentController (req: Request, res: Response) { | ||
|
||
try { | ||
const { name, password, email, user } = req.body; | ||
|
||
const newStudent: IStudent = new Student({ | ||
name, | ||
password, | ||
email, | ||
user, | ||
}); | ||
|
||
const savedStudent: IStudent = await newStudent.save(); | ||
|
||
res.status(201).json({ message: 'Student created successfully', savedStudent}); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
export async function getStudentsController (req: Request, res: Response) { | ||
|
||
try { | ||
const Students: IStudent[] = await Student.find({}); | ||
|
||
Students.forEach((student) => { | ||
student.password = ''; | ||
}); | ||
|
||
res.status(200).json({ Students }); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} | ||
|
||
|
||
export async function searchByEmailController (req: Request, res: Response){ | ||
|
||
try { | ||
const { email } = req.query; | ||
|
||
const professor: IProfessor | null = await Professor.findOne({ email }); | ||
const student: IStudent | null = await Student.findOne({ email }); | ||
|
||
if (professor || student) { | ||
res.status(200).json({ professor, student }); | ||
} else { | ||
res.status(404).json({ message: `No record found for email: ${email}` }); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send('Server error'); | ||
} | ||
} |
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
Oops, something went wrong.