package ciai.controller; 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.ProfessorNotEditedException; import ciai.exception.ProfessorNotFoundException; import ciai.model.Edition; import ciai.model.Professor; import ciai.service.AppService; import ciai.view.Views; import tools.Response; @Controller @RequestMapping(value = "/professor") public class ProfessorController { @Autowired AppService service; /** * Detalhes de um professor */ @RequestMapping(value = "/view/{id}", method = RequestMethod.GET) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorPage(#id)") @JsonView(Views.ProfessorView.class) public @ResponseBody ResponseEntity getStudent(@PathVariable Long id) { Professor p; try { p = service.getProfessor(id); } catch (ProfessorNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(p, HttpStatus.OK); } /** * Edições em que o professor dá aulas */ @RequestMapping(value = "/view/{id}/editions", method = RequestMethod.GET) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorPage(#id)") @JsonView(Views.ProfessorView.class) public @ResponseBody Set getAllProfessorEditions(@PathVariable Long id) { return service.getProfessorCurrentEditions(id); } /** * Editar detalhes de um professor */ @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorPage(#id)") public @ResponseBody ResponseEntity editProfessor(@PathVariable Long id, @RequestParam Map requestParams) { String name = requestParams.get("name"); String photo = requestParams.get("photo"); String email = requestParams.get("email"); try { service.editProfessor(id, name, photo, email); } catch (ProfessorNotFoundException e) { return new ResponseEntity(new Response("Professor profile not found"),HttpStatus.NOT_FOUND); } catch (ProfessorNotEditedException e) { return new ResponseEntity(new Response("Error editing the professor profile"),HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Professor profile edited"),HttpStatus.OK); } }