package ciai.controller; import java.util.Collection; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.fasterxml.jackson.annotation.JsonView; import ciai.exception.EditionNotFoundException; import ciai.exception.StudentNotEditedException; import ciai.exception.StudentNotFoundException; import ciai.model.Edition; import ciai.model.Professor; import ciai.model.Result; import ciai.model.Student; import ciai.model.StudentEditionDetails; import ciai.model.StudentEvaluation; import ciai.repository.StudentEvaluationRepository; import ciai.service.AppService; import ciai.view.Views; import tools.Response; @Controller @RequestMapping(value = "/student") public class StudentController { @Autowired AppService service; @Autowired StudentEvaluationRepository studentEvalRep; /** * Detalhes de um estudante */ @RequestMapping(value = "/view/{number}", method = RequestMethod.GET) @PreAuthorize("hasRole('STUDENT') and @securityService.isStudentPage(#number)") @JsonView(Views.StudentView.class) public @ResponseBody ResponseEntity getStudent(@PathVariable int number) { Student s; try { s = service.getStudentByNumber(number); } catch (StudentNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(s, HttpStatus.OK); } /** * Lista de edições concluidas e a nota respetiva (resultados) */ @RequestMapping(value = "/view/{number}/results", method = RequestMethod.GET) @PreAuthorize("hasRole('STUDENT') and @securityService.isStudentPage(#number)") @JsonView(Views.StudentView.class) public @ResponseBody Collection getStudentResults(@PathVariable int number) { return service.getStudentResults(number); } /** * Lista de edições atuais de um estudante */ @RequestMapping(value = "/view/{number}/editions", method = RequestMethod.GET) @JsonView(Views.StudentView.class) public @ResponseBody Set getStudentEditions(@PathVariable int number) { return service.getStudentCurrentEditions(number); } /** * Detalhes de uma edição concluida de um estudante (professores e notas de * cada avaliação) */ @RequestMapping(value = "/view/{number}/finished/edition/{eid}", method = RequestMethod.GET) @JsonView(Views.StudentView.class) public @ResponseBody ResponseEntity getStudentFinishedEditionDetails(@PathVariable int number, @PathVariable Long eid) { StudentEditionDetails sed = new StudentEditionDetails(); try { Edition edition = service.getEdition(eid); Student student = service.getStudentByNumber(number); Set professors = service.getEditionProfessors(edition.getId()); Set studentEvals = studentEvalRep.sarchByStudentFinished(student.getId(),edition.getId()); sed.setEvaluations(studentEvals); sed.setProfessors(professors); } catch (EditionNotFoundException e) { System.out.println("edition not found"); return new ResponseEntity(HttpStatus.NOT_FOUND); } catch (StudentNotFoundException e) { System.out.println("student not found"); return new ResponseEntity(HttpStatus.NOT_FOUND); } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(sed,HttpStatus.OK); } /** * Detalhes de uma edição currente de um estudante (professores e notas de * cada avaliação) */ @RequestMapping(value = "/view/{number}/all/edition/{eid}", method = RequestMethod.GET) @JsonView(Views.StudentView.class) public @ResponseBody ResponseEntity getStudentCurrentEditionDetails(@PathVariable int number, @PathVariable Long eid) { StudentEditionDetails sed = new StudentEditionDetails(); try { Edition edition = service.getEdition(eid); Student student = service.getStudentByNumber(number); Set professors = service.getEditionProfessors(edition.getId()); Set studentEvals = studentEvalRep.sarchByStudentAll(student.getId(),edition.getId()); sed.setEvaluations(studentEvals); sed.setProfessors(professors); } catch (EditionNotFoundException e) { System.out.println("edition not found"); return new ResponseEntity(HttpStatus.NOT_FOUND); } catch (StudentNotFoundException e) { System.out.println("student not found"); return new ResponseEntity(HttpStatus.NOT_FOUND); } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(sed,HttpStatus.OK); } /** * Editar os detalhes de um estudante */ @RequestMapping(value = "/edit/{number}", method = RequestMethod.POST) @PreAuthorize("hasRole('STUDENT') and @securityService.isStudentPage(#number)") public @ResponseBody ResponseEntity editStudent(@PathVariable int number, @RequestParam Map requestParams) { String name = requestParams.get("name"); String photo = requestParams.get("photo"); String personalemail = requestParams.get("email"); String address = requestParams.get("address"); String birthday = requestParams.get("birthday"); try { service.editStudent(number, name, photo, personalemail, address, birthday); } catch (StudentNotEditedException e) { return new ResponseEntity(new Response("Error editing the student profile"),HttpStatus.INTERNAL_SERVER_ERROR); } catch (StudentNotFoundException e) { return new ResponseEntity(new Response("Student not found"),HttpStatus.NOT_FOUND); } return new ResponseEntity(new Response("Student profile edited"),HttpStatus.OK); } }