Skip to content

Instantly share code, notes, and snippets.

@bhawna94
Created July 5, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhawna94/c79bed8755366ef457d73dcdae735ce7 to your computer and use it in GitHub Desktop.
Save bhawna94/c79bed8755366ef457d73dcdae735ce7 to your computer and use it in GitHub Desktop.
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