SubjectController.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.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.RestController;
import org.uoc.tfg.sel.repository.model.Subject;
import org.uoc.tfg.sel.service.SubjectService;
import org.uoc.tfg.sel.web.model.ModelUtils;
/**
* The Class SubjectController.
* @author Eduardo Rodriguez Carro
*/
@RestController
@CrossOrigin
@RequestMapping("/subjects")
public class SubjectController {
/** The subject service. */
@Autowired
private SubjectService subjectService;
/**
* Listado de usuarios por tipo.
*
* @return the all
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTS')")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<List<Subject>> getAll() throws Exception {
return ResponseEntity.ok(subjectService.getAll());
}
/**
* Listado de usuarios por tipo.
*
* @param id the id
* @return the by id
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTS')")
@RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
public ResponseEntity<Subject> getById(@PathVariable("id") Integer id) throws Exception {
Subject user = subjectService.getById(id);
return ResponseEntity.ok(user);
}
/**
* Listado de usuarios por tipo.
*
* @param item the item
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTS_EDIT')")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResponseEntity<Subject> save(@RequestBody Subject item) throws Exception {
Subject clonedItem = null;
if (item.getId() != null) {
clonedItem = subjectService.getById(item.getId());
ModelUtils.copyObjectWithOutNulls(item, clonedItem);
clonedItem = subjectService.save(clonedItem);
} else {
clonedItem = subjectService.save(item);
}
return ResponseEntity.ok(clonedItem);
}
/**
* Delete.
*
* @param id the id
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_SUBJECTS_EDIT')")
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> delete(@PathVariable("id") Integer id) throws Exception {
subjectService.delete(id);
return ResponseEntity.ok().build();
}
}