Created
October 19, 2019 21:56
-
-
Save singh1114/5f21641afd022ce975f9ba38e5601f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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