SubjectClassController.java
/**
* TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
* Copyright (C) 2020 Eduardo Rodriguez Carro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.uoc.tfg.sel.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.uoc.tfg.sel.repository.model.SubjectClass;
import org.uoc.tfg.sel.service.SubjectClassService;
import org.uoc.tfg.sel.web.model.ModelUtils;
/**
* The Class SubjectClassController.
* @author Eduardo Rodriguez Carro
*/
@RestController
@CrossOrigin
@RequestMapping("/classes")
public class SubjectClassController {
/** The subject class service. */
@Autowired
private SubjectClassService subjectClassService;
/**
* Listado de usuarios por tipo.
*
* @return the all
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTCLASSES')")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<List<SubjectClass>> getAll() throws Exception {
return ResponseEntity.ok(subjectClassService.getAll());
}
/**
* Gets the my classes.
*
* @param authentication the authentication
* @return the my classes
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MY_SUBJECTCLASSES')")
@RequestMapping(value = "/my", method = RequestMethod.GET)
public ResponseEntity<List<SubjectClass>> getMyClasses(Authentication authentication) throws Exception {
return ResponseEntity.ok(subjectClassService.getMyclasses(ControllerUtils.getUser(authentication)));
}
/**
* Listado de usuarios por tipo.
*
* @param id the id
* @param students the students
* @return the by id
* @throws Exception the exception
*/
@PreAuthorize ("hasRole('ROLE_API_SUBJECTCLASSES') or hasRole('ROLE_API_MY_SUBJECTCLASSES')")
@RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
public ResponseEntity<SubjectClass> getById(
@PathVariable("id") Integer id,
@RequestParam(name = "students",required = false,defaultValue = "false") boolean students,Authentication authentication) throws Exception {
return ResponseEntity.ok(subjectClassService.getById(id,students,ControllerUtils.getUser(authentication)));
}
/**
* Listado de usuarios por tipo.
*
* @param item the item
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTCLASSES_EDIT')")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResponseEntity<SubjectClass> save(@RequestBody SubjectClass item) throws Exception {
SubjectClass clonedItem = null;
if (item.getId() != null) {
clonedItem = subjectClassService.getById(item.getId(),false,null);
ModelUtils.copyObjectWithOutNulls(item, clonedItem);
clonedItem = subjectClassService.save(clonedItem);
} else {
clonedItem = subjectClassService.save(item);
}
return ResponseEntity.ok(clonedItem);
}
/**
* Delete.
*
* @param id the id
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTCLASSES_EDIT')")
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> delete(@PathVariable("id") Integer id) throws Exception {
subjectClassService.delete(id);
return ResponseEntity.ok().build();
}
/**
* Delete student from class.
*
* @param classId the class id
* @param studentId the student id
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTCLASSES_STUDENTS_EDIT')")
@RequestMapping(value = "/delete/{id}/student/{sid}", method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteStudentFromClass(@PathVariable("id") Integer classId,@PathVariable("sid") Integer studentId) throws Exception {
subjectClassService.deleteStudentFromClass(classId, studentId);
return ResponseEntity.ok().build();
}
/**
* Save student to class.
*
* @param classId the class id
* @param studentId the student id
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTCLASSES_STUDENTS_EDIT')")
@RequestMapping(value = "/save/{id}/student/{sid}", method = RequestMethod.POST)
public ResponseEntity<Void> saveStudentToClass(@PathVariable("id") Integer classId,@PathVariable("sid") Integer studentId) throws Exception {
subjectClassService.saveStudentToClass(classId, studentId);
return ResponseEntity.ok().build();
}
}