Viewcontroller - srujanabala/springboot-couchbase GitHub Wiki
Viewcontroller
package org.axp.springbootcouchbase.mvc.service;
import java.util.List; import java.util.Optional;
import org.axp.springbootcouchbase.mvc.model.Student; import org.axp.springbootcouchbase.mvc.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; 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;
@RestController @RequestMapping(produces = "application/json") public class ViewController {
@Autowired
private UserRepository userRepo;
@RequestMapping(path = "/add", method = RequestMethod.GET)
public void 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");
}
@RequestMapping(path = "/del", method = RequestMethod.DELETE)
public void del() {
userRepo.deleteById("101");
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", method = RequestMethod.GET, produces = "application/json")
public List<Student> fetchByQuery() { return userRepo.findByTheQuery("Abhishek"); }
@RequestMapping(path = "/custom-query", method = RequestMethod.GET, produces = "application/json")
public List<Student> fetchCustom() { return userRepo.getAllOrderAndGroup(); }
@RequestMapping(path = "/ip-from-hostname/{ifh}", method = RequestMethod.GET, produces = "application/json")
public String ipFromHostname(@PathVariable("ifh") String ifh) {
long a = System.currentTimeMillis();
JavaLookup.lookup(ifh);
return System.currentTimeMillis() - a + "";
}
@RequestMapping(path = "/hostname-from-ip/{hfi}", method = RequestMethod.GET, produces = "application/json")
public String hostnameFromIp(@PathVariable("hfi") String hfi) {
long a = System.currentTimeMillis();
JavaLookup.lookup(hfi);
return System.currentTimeMillis() - a + "";
}
//@PostMapping(path = "/students",consumes = "application/json")
@RequestMapping(path = "/students", method = RequestMethod.POST, consumes = "application/json")
public Student addStudent(@RequestBody Student student) {
userRepo.save(student);
return student;
}
//@PutMapping
@RequestMapping(path = "/students/{id}", method = RequestMethod.PUT, 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("DataNot found");
return null;
}
}
}