Skip to content

Instantly share code, notes, and snippets.

@singh1114
Created October 19, 2019 21:56
Show Gist options
  • Save singh1114/5f21641afd022ce975f9ba38e5601f1d to your computer and use it in GitHub Desktop.
Save singh1114/5f21641afd022ce975f9ba38e5601f1d to your computer and use it in GitHub Desktop.
package io.singh1114.springboottut.school;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SchoolController {
@Autowired
private SchoolService schoolService;
@RequestMapping("/schools")
public List<School> getAllSchools() {
return schoolService.getAllSchools();
}
@RequestMapping(method = RequestMethod.POST, value = "/school")
public void addSchool(@RequestBody School school) {
schoolService.addSchool(school);
}
}
package io.singh1114.springboottut.school;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class SchoolService {
@Autowired
private SchoolRepository schoolRepository;
public List<School> getAllSchools() {
List<School> schools = new ArrayList<>();
schoolRepository.findAll()
.forEach(schools::add);
return schools;
}
public void addSchool(School school) {
schoolRepository.save(school);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment