Skip to content
Snippets Groups Projects
Commit adfa7dc7 authored by Petr Seidl's avatar Petr Seidl
Browse files

Update corrected return type for get methods

parent ae4975b5
No related branches found
No related tags found
No related merge requests found
package cz.cvut.fit.tjv.seidlpet.semestralka.controller;
 
import cz.cvut.fit.tjv.seidlpet.semestralka.business.TeacherService;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.dto.SubjectDTO;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.dto.TeacherCreateDTO;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.dto.TeacherDTO;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.entity.Student;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.entity.Subject;
import cz.cvut.fit.tjv.seidlpet.semestralka.data.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -9,6 +14,7 @@ import org.springframework.web.server.ResponseStatusException;
 
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/api/v1/teacher")
......@@ -31,23 +37,23 @@ public class TeacherController {
}
 
@GetMapping(value = "/{id}")
public Teacher findById(@PathVariable int id) {
return teacherService.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
public TeacherDTO findById(@PathVariable int id) {
return toDTO(teacherService.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)));
}
 
@GetMapping
public List<Teacher> findAll(@RequestParam(required = false) String firstName, @RequestParam(required = false) String lastName) {
public List<TeacherDTO> findAll(@RequestParam(required = false) String firstName, @RequestParam(required = false) String lastName) {
if(firstName != null && lastName != null)
return teacherService.findByFirstNameAndLastName(firstName,lastName);
return teacherService.findByFirstNameAndLastName(firstName,lastName).stream().map(this::toDTO).collect(Collectors.toList());
if(firstName != null)
return teacherService.findAllByFirstName(firstName);
return teacherService.findAllByFirstName(firstName).stream().map(this::toDTO).collect(Collectors.toList());
if(lastName != null)
return teacherService.findAllByLastName(lastName);
return teacherService.findAll();
return teacherService.findAllByLastName(lastName).stream().map(this::toDTO).collect(Collectors.toList());
return teacherService.findAll().stream().map(this::toDTO).collect(Collectors.toList());
}
 
@PutMapping("/{id}")
public void update(@PathVariable int id, @RequestBody Teacher teacher) {
public void update(@PathVariable int id, @RequestBody TeacherCreateDTO teacher) {
try {
teacherService.update(id, teacher);
} catch (NoSuchElementException e) {
......@@ -63,4 +69,8 @@ public class TeacherController {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
private TeacherDTO toDTO(Teacher teacher) {
return new TeacherDTO(teacher.getId(),teacher.getFirstName(),teacher.getLastName());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment