Skip to content
Snippets Groups Projects
Commit 8f5b80f7 authored by 박준우's avatar 박준우
Browse files

refactor: change RestController to controller

parent f5fa3a5b
No related branches found
No related tags found
No related merge requests found
package com.guide.server; package com.guide.server;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @Controller
public class StudentController { public class StudentController {
private final StudentService studentService; private final StudentService studentService;
...@@ -16,45 +19,48 @@ public class StudentController { ...@@ -16,45 +19,48 @@ public class StudentController {
} }
@PostMapping("/student/add") @PostMapping("/student/add")
public String createStudent( public ResponseEntity<?> createStudent(
@RequestParam String name, @RequestParam String name,
@RequestParam String stdId, @RequestParam String stdId,
@RequestParam Integer grade, @RequestParam Integer grade,
@RequestParam String major) { @RequestParam String major) {
System.out.println("실행중1");
studentService.saveStudent(Student.builder().name(name).stdId(stdId).grade(grade).major(major).build()); studentService.saveStudent(Student.builder().name(name).stdId(stdId).grade(grade).major(major).build());
return "success"; return new ResponseEntity<>("success", HttpStatus.OK);
} }
@PutMapping("/student/update") @PutMapping("/student/update")
public String updateStudent( public ResponseEntity<?> updateStudent(
@RequestParam Long id, @RequestParam Long id,
@RequestParam String major) { @RequestParam String major) {
System.out.println("실행중3");
Optional<Student> student = studentService.findOne(id); Optional<Student> student = studentService.findOne(id);
if(student.isPresent()){ if(student.isPresent()){
Student newStudent = student.get(); Student newStudent = student.get();
newStudent.setMajor(major); newStudent.setMajor(major);
studentService.saveStudent(newStudent); studentService.saveStudent(newStudent);
return "success"; return new ResponseEntity<>("success", HttpStatus.OK);
} }
return "fail"; return new ResponseEntity<>("fail", HttpStatus.OK);
} }
@GetMapping("/student/stdId") @GetMapping("/student/stdId")
public Student getStudentByStdId(@RequestParam String stdId){ public ResponseEntity<?> getStudentByStdId(@RequestParam String stdId){
return studentService.findOne(stdId).orElse(null); return new ResponseEntity<>(studentService.findOne(stdId).orElse(null), HttpStatus.OK);
} }
@GetMapping("/student/list") @GetMapping("/student/list")
public List<Student> getStudentList(){ public ResponseEntity<?> getStudentList(){
return studentService.findStudents(); System.out.println("실행중2");
return new ResponseEntity<>(studentService.findStudents(), HttpStatus.OK);
} }
@DeleteMapping("/student/delete") @DeleteMapping("/student/delete")
public String deleteStudent(@RequestParam Long id){ public ResponseEntity<?> deleteStudent(@RequestParam Long id){
Optional<Student> student = studentService.findOne(id); Optional<Student> student = studentService.findOne(id);
if(student.isPresent()){ if(student.isPresent()){
studentService.deleteStudent(id); studentService.deleteStudent(id);
return "success"; return new ResponseEntity<>("success", HttpStatus.OK);
} }
return "fail"; return new ResponseEntity<>("fail", HttpStatus.OK);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment