1package com.example.demo.com.example.demo.resource;
2
3import java.util.List;
4import java.util.Optional;
5
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.web.bind.annotation.DeleteMapping;
8import org.springframework.web.bind.annotation.GetMapping;
9import org.springframework.web.bind.annotation.PathVariable;
10import org.springframework.web.bind.annotation.PostMapping;
11import org.springframework.web.bind.annotation.RequestBody;
12import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RequestMethod;
14import org.springframework.web.bind.annotation.RequestParam;
15import org.springframework.web.bind.annotation.ResponseBody;
16import org.springframework.web.bind.annotation.RestController;
17
18import com.example.demo.com.example.demo.model.Student;
19import com.example.demo.com.example.demo.repository.StudentRep;
20
21@RestController
22@RequestMapping(value = "/webapi")
23public class StudentResource {
24
25 @Autowired
26 private StudentRep studentRep;
27
28 @RequestMapping(method = RequestMethod.GET, value = "/getstudents")
29 public List<Student> getStudents() {
30
31 return studentRep.findAll();
32
33 }
34
35 @RequestMapping(method = RequestMethod.POST, value = "/addstudent")
36 public String addStudent(@RequestBody Student student) {
37
38 studentRep.save(student);
39 return "Added Student : "+student.getName();
40
41 }
42
43 @RequestMapping(method = RequestMethod.GET,value = "/getstudents/{id}")
44 public Optional<Student> getStudent(@PathVariable String id) {
45
46 return studentRep.findById(id);
47
48 }
49
50 @RequestMapping(method = RequestMethod.POST, value = "/deletestudent/{id}")
51 public String deleteStudent(@PathVariable String id) {
52
53 studentRep.deleteById(id);
54
55 return "Record Deleted :"+ id;
56
57 }
58
59 @RequestMapping(method = RequestMethod.PUT, value = "/updatestudent")
60 public String updateStudent(@RequestBody Student student) {
61
62 studentRep.findById(student.getId());
63
64 if(studentRep.existsById(student.getId())) {
65 studentRep.save(student);
66 return "Record updated";
67 }
68 else {
69 return "No record";
70 }
71
72
73 }
74
75
76}
77