Skip to content

Commit

Permalink
Creating Services
Browse files Browse the repository at this point in the history
  • Loading branch information
fawad1997 committed Apr 15, 2020
1 parent 6c1d0b6 commit 362bbd5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ Now create an entity **Country**, annotate class with annotations **@Entity**, *

##### Creating Repositories
same as above

##### Creating Services
same as above
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.restfulspring.apiexample.service;

import com.restfulspring.apiexample.entity.City;
import com.restfulspring.apiexample.entity.Country;
import com.restfulspring.apiexample.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityService {
@Autowired
private CityRepository cityRepository;
public List<City> getCities(){
return cityRepository.findAll();
}
public City getCity(int id){
return cityRepository.findById(id).orElse(null);
}
public City addCity(City city){
return cityRepository.save(city);
}
public City updateCity(int id,City city){
city.setCityId(id);
return cityRepository.save(city);
}
public void deleteCity(int id){
cityRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.restfulspring.apiexample.service;

import com.restfulspring.apiexample.entity.Country;
import com.restfulspring.apiexample.repository.CountryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CountryService {
@Autowired
private CountryRepository countryRepository;

public List<Country> getCountries(){
return countryRepository.findAll();
}
public Country getCountry(int id){
return countryRepository.findById(id).orElse(null);
}
public Country addCountry(Country country){
return countryRepository.save(country);
}
public Country updateCountry(int id,Country country){
country.setCountryId(id);
return countryRepository.save(country);
}
public void deleteCountry(int id){
countryRepository.deleteById(id);
}
}

0 comments on commit 362bbd5

Please sign in to comment.