Commit b7d1807c authored by Sumaiyya Burney's avatar Sumaiyya Burney

Adds REST controller/endpoints

parent dea58750
package com.nisum.learning.student;
public class StudentService {
}
package com.nisum.learning.student.controller;
import com.nisum.learning.student.model.Student;
import org.springframework.http.ResponseEntity;
import com.nisum.learning.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
StudentService studentService;
//for demo/ref purposes
public static void main(String[] args) {
Flux<Integer> nums = Flux.just(1, 2, 3)/*.concatWith(Flux.error(new RuntimeException("Exception occurred")))*/;
nums.log()
......@@ -20,26 +25,27 @@ public class StudentController {
@GetMapping()
public Flux<Student> getAllStudents(){
return null;
return studentService.findAll();
}
@GetMapping("/{id}")
public Mono<Student> getStudentById(@PathVariable int id){
return null;
return studentService.findById(id);
}
@PostMapping()
public ResponseEntity<Student> createStudent(@RequestBody Student newStudent){
return null;
@ResponseStatus(HttpStatus.CREATED)
public void createStudent(@RequestBody Student newStudent){
studentService.create(newStudent);
}
@PutMapping("/{id}")
public Mono<ResponseEntity<Student>> updateStudentById(@RequestBody Student newStudent){
return null;
public Mono<Student> updateStudentById(@RequestBody Student newStudent){
return studentService.update(newStudent);
}
@DeleteMapping("/{id}")
public Mono<ResponseEntity<Student>> deleteStudentById(@PathVariable int id){
return null;
public void deleteStudentById(@PathVariable int id){
studentService.deleteById(id);
}
}
package com.nisum.learning.student.repository;
import com.nisum.learning.student.model.Student;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends ReactiveMongoRepository<Student, Integer> {
}
package com.nisum.learning.student.service;
import com.nisum.learning.student.model.Student;
import com.nisum.learning.student.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Service
public class StudentService {
@Autowired
StudentRepository studentRepo;
public Flux<Student> findAll() {
return studentRepo.findAll();
}
public Mono<Student> findById(Integer id){
return studentRepo.findById(id);
}
public void create(Student newStudent){
studentRepo.save(newStudent).subscribe();
}
public Mono<Student> update(Student newStudent){
return studentRepo.save(newStudent);
}
public void deleteById(int id){
studentRepo.deleteById(id);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment