1+ import {
2+ createStudent , deleteStudentById , studentList , updateStudentById ,
3+ } from "#services/student" ;
4+ import { logger } from "#util" ;
5+
6+ async function addStudent ( req , res ) {
7+ const {
8+ ERPID , name, joiningYear, branch, division, rollNo, coursesOpted
9+ } = req . body ;
10+ try {
11+ const newStudent = await createStudent ( ERPID , name , joiningYear , branch , division , rollNo , coursesOpted ) ;
12+ res . json ( { res : `added user ${ newStudent . id } ` } ) ;
13+ } catch ( error ) {
14+ logger . error ( "Error while inserting" , error ) ;
15+ res . status ( 500 ) ;
16+ res . json ( { err : "Error while inserting in DB" } ) ;
17+ }
18+ }
19+
20+ async function updateStudent ( req , res ) {
21+ const {
22+ id, ...data
23+ } = req . body ;
24+ try {
25+ await updateStudentById ( id , data ) ;
26+ res . json ( { res : `updated Student with id ${ id } ` } ) ;
27+ } catch ( error ) {
28+ logger . error ( "Error while updating" , error ) ;
29+ res . status ( 500 ) ;
30+ res . json ( { err : "Error while updaing in DB" } ) ;
31+ }
32+ }
33+
34+ async function getStudent ( req , res ) {
35+ const filter = req . query ;
36+ const StudList = await StudentList ( filter ) ;
37+ res . json ( { res : StudList } ) ;
38+ }
39+
40+ async function deleteStudent ( req , res ) {
41+ const { StudentId } = req . params ;
42+ try {
43+ await deleteStudentById ( StudentId ) ;
44+
45+ res . json ( { res : `Deleted Student with ID ${ StudentId } ` } ) ;
46+ } catch ( error ) {
47+ logger . error ( "Error while deleting" , error ) ;
48+ res . status ( 500 ) . json ( { error : "Error while deleting from DB" } ) ;
49+ }
50+ }
51+ export default {
52+ addStudent, deleteStudent, getStudent, updateStudent,
53+ } ;
54+
0 commit comments