Created
July 5, 2019 08:15
-
-
Save bhawna94/c79bed8755366ef457d73dcdae735ce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.knoldus.wallet.model.StudentInfo; | |
import com.knoldus.wallet.repository.StudentRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.web.bind.annotation.DeleteMapping; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.client.HttpClientErrorException; | |
@RestController | |
public class StudentController { | |
@Autowired | |
private StudentRepository studentRepository; | |
@PostMapping("/student") | |
public StudentInfo addStudentDetails(@RequestBody StudentInfo studentInfo) { | |
return studentRepository.save(studentInfo); | |
} | |
@GetMapping("/student/{id}") | |
public StudentInfo getStudentById(@PathVariable String id) { | |
return studentRepository.findById(id) | |
.orElseThrow(() -> new HttpClientErrorException(HttpStatus.NOT_FOUND)); | |
} | |
@DeleteMapping("/student/{id}") | |
public void deleteStudentById(@PathVariable String id) { | |
studentRepository.deleteById(id); | |
} | |
@GetMapping("/student/name/{name}") | |
public StudentInfo getStudentByName(@PathVariable String name) { | |
return studentRepository.findByName(name) | |
.orElseThrow(() -> new HttpClientErrorException(HttpStatus.NOT_FOUND)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment