package com.StudentServices.MarksService.controller; import com.StudentServices.MarksService.exception.ResourceNotFoundException; import com.StudentServices.MarksService.model.Mark; import com.StudentServices.MarksService.model.StudentMarks; import com.StudentServices.MarksService.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @CrossOrigin(origins = "*") @RestController @RequestMapping("/api/") public class StudentMarksController { @Autowired StudentService studentService; // (GET) findall studentmarks @GetMapping("/studentMarks") ResponseEntity> findAllStudentMarks(){ return ResponseEntity.ok(studentService.findAllStudents()); } // (POST) create studentmark @PostMapping("/studentMarks") public ResponseEntity createMarks(@RequestBody StudentMarks studentMarks) { return ResponseEntity.ok(studentService.addStudent(studentMarks)); } // (GET) find studentmark by id @GetMapping("/studentMarks/{id}") ResponseEntity findStudentMarksById(@PathVariable String id) throws ResourceNotFoundException { StudentMarks sm = studentService.getStudentById(id). orElseThrow( () -> new ResourceNotFoundException("Student was not found: " + id)); return ResponseEntity.ok(sm); } // (PUT) update studentmark by id @PutMapping("/studentMarks/{id}/{course}") ResponseEntity updateStudent(@PathVariable String id, @PathVariable String course, @RequestBody String grade) throws ResourceNotFoundException { StudentMarks student = studentService.getStudentById(id). orElseThrow( () -> new ResourceNotFoundException("Student was not found: " + id)); Optional currentStudentMarks = student.getMarks().stream().filter(c -> c.getCourseId().equals(course)).findFirst(); if (currentStudentMarks.isPresent()) { currentStudentMarks.get().setGrade(grade); } else { throw new ResourceNotFoundException("course not found"); } studentService.updateStudent(student); return ResponseEntity.ok(student); } // (DELETE) delete studentmark by id @DeleteMapping("remove/{id}") ResponseEntity deleteStudentMarkById(@PathVariable String id) throws ResourceNotFoundException{ StudentMarks sm = studentService.getStudentById(id). orElseThrow( () -> new ResourceNotFoundException("Student was not found: " + id)); studentService.deleteStudent(sm); return ResponseEntity.ok("Successfully deleted student: " + sm.getEmailAddress()); } }