view controller1 24 - srujanabala/springboot-couchbase GitHub Wiki
package com.example.rest;
import java.util.List; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import com.example.model.Student; import com.example.repository.UserRepository;
@RestController public class ViewController {
@Autowired
private UserRepository userRepo;
@RequestMapping(path = "/add", method = RequestMethod.GET, produces = "application/json")
public Iterable<Student> addNew() {
userRepo.deleteAll();
Student s = new Student("8", "Abhishek", 30);
Student s2 = new Student("64", "Alex", 35);
Student s3 = new Student("80", "John", 40);
Student s4 = new Student("88", "Shankle", 44);
Student s5 = new Student("45", "jackie", 45);
userRepo.save(s);
userRepo.save(s2);
userRepo.save(s3);
userRepo.save(s4);
userRepo.save(s5);
System.out.println("finished ass");
return userRepo.findAll();
}
@GetMapping(path = "/test", produces = "application/json")
public String testApi() {
return "Response from API";
}
@RequestMapping(path = "/del", method = RequestMethod.DELETE)
public void del() {
userRepo.deleteAll();
}
@RequestMapping(path = "/get", method = RequestMethod.GET, produces = "application/json")
public Iterable<Student> fetchRecords() {
System.out.println("From controller");
userRepo.findAll().forEach(System.out::println);
return userRepo.findAll();
}
@RequestMapping(path = "/get/{name}", method = RequestMethod.GET, produces = "application/json")
public List<Student> fetchByName(@PathVariable("name") String name) {
System.out.println("inside get");
return userRepo.findByName(name);
}
@RequestMapping(path = "/fetch-by-query/{name}", method = RequestMethod.GET, produces = "application/json")
public List<Student> fetchCustom(@PathVariable("name")String name) {
return userRepo.findByTheQuery(name);
}
@RequestMapping(path = "/fetch-by-query/order", method = RequestMethod.GET, produces = "application/json")
public List<Student> fetchCustom1() {
return userRepo.getAllOrderAndGroup();
}
@GetMapping("/meta")
public List getMeta() {
return userRepo.getMetaData();
}
@PostMapping(path = "/students",consumes = "application/json")
public ResponseEntity<Student> addStudent(@RequestBody Student student) {
userRepo.save(student);
return new ResponseEntity<>(student, HttpStatus.CREATED);
}
@PutMapping(path = "/students/{id}", consumes = "application/json")
public Student updateStudent(@PathVariable("id") String id) {
Optional<Student> student = userRepo.findById(id);
if (student.isPresent()) {
Student stu = student.get();
stu.setName("updatedName");
userRepo.save(stu);
return stu;
} else {
System.out.println("Data Not found");
return null;
}
}
@RequestMapping(path = "/count", method = RequestMethod.GET, produces = "application/json")
public long getCount() {
System.out.println("Getting total count...");
return userRepo.count();
}
@GetMapping(path = "/query", produces = "application/json")
public List<Student> getAllByQuery() {
return userRepo.getAllByQuery();
}
@GetMapping(path = "/agetest")
public String getSecondHighestAgeStudent() {
System.out.println("Second Highest Age is : " + userRepo.getSecondHighestAgeStudent());
return "Second Highest Age is : " + userRepo.getSecondHighestAgeStudent();
}
}