This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aa3f40f
Showing
11 changed files
with
43,036 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.idea | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.settings | ||
.classpath | ||
.project | ||
.springbeans | ||
.gradle | ||
target | ||
out | ||
build |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
maven { url "http://repo.spring.io/milestone" } | ||
} | ||
dependencies { | ||
classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M5") | ||
} | ||
} | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'spring-boot' | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url "http://repo.spring.io/milestone" } | ||
} | ||
|
||
dependencies { | ||
compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M5") | ||
compile("org.springframework.boot:spring-boot-starter-data-jpa:0.5.0.M5") | ||
|
||
runtime("org.hsqldb:hsqldb:2.2.9") | ||
|
||
testCompile("junit:junit:4.11") | ||
} | ||
|
||
task wrapper(type: Wrapper) { | ||
gradleVersion = '1.8' | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
applications: | ||
- name: spring-boot-cities | ||
memory: 512M | ||
instances: 1 | ||
host: cities-${random-word} | ||
domain: cfapps.io | ||
path: build/libs/spring-boot-cities.jar |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.cities; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.web.config.EnableSpringDataWebSupport; | ||
|
||
@Configuration | ||
@ComponentScan | ||
@EnableAutoConfiguration | ||
@EnableSpringDataWebSupport | ||
public class CitiesApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(CitiesApplication.class, args); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.example.cities.domain; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import java.io.Serializable; | ||
|
||
@Entity | ||
@Table(name="city") | ||
public class City implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private String county; | ||
|
||
@Column(nullable = false) | ||
private String stateCode; | ||
|
||
@Column(nullable = false) | ||
private String postalCode; | ||
|
||
@Column | ||
private String latitude; | ||
|
||
@Column | ||
private String longitude; | ||
|
||
public City() { | ||
} | ||
|
||
public City(String name, String stateCode, String postalCode) { | ||
this.name = name; | ||
this.stateCode = stateCode; | ||
this.postalCode = postalCode; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPostalCode() { | ||
return postalCode; | ||
} | ||
|
||
public void setPostalCode(String postalCode) { | ||
this.postalCode = postalCode; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getStateCode() { | ||
return stateCode; | ||
} | ||
|
||
public void setStateCode(String stateCode) { | ||
this.stateCode = stateCode; | ||
} | ||
|
||
public String getCounty() { | ||
return county; | ||
} | ||
|
||
public void setCounty(String county) { | ||
this.county = county; | ||
} | ||
|
||
public String getLatitude() { | ||
return latitude; | ||
} | ||
|
||
public void setLatitude(String latitude) { | ||
this.latitude = latitude; | ||
} | ||
|
||
public String getLongitude() { | ||
return longitude; | ||
} | ||
|
||
public void setLongitude(String longitude) { | ||
this.longitude = longitude; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/cities/repositories/CityRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.cities.repositories; | ||
|
||
import com.example.cities.domain.City; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.repository.Repository; | ||
|
||
public interface CityRepository extends Repository<City, Long> { | ||
Page<City> findAll(Pageable pageable); | ||
|
||
City findById(Long id); | ||
|
||
Page<City> findByStateCodeIgnoreCase(String stateCode, Pageable pageable); | ||
|
||
Page<City> findByNameIgnoreCase(String part, Pageable pageable); | ||
|
||
Page<City> findByPostalCode(String postalCode, Pageable pageable); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/cities/services/CityService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.cities.services; | ||
|
||
import com.example.cities.domain.City; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CityService { | ||
Page<City> findAllCities(Pageable pageable); | ||
|
||
City findById(Long id); | ||
|
||
Page<City> findCities(String query, Pageable pageable); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/example/cities/services/RepositoryCityService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.example.cities.services; | ||
|
||
import com.example.cities.domain.City; | ||
import com.example.cities.repositories.CityRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Service | ||
public class RepositoryCityService implements CityService { | ||
|
||
private CityRepository repository; | ||
|
||
@Autowired | ||
public RepositoryCityService(CityRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@Override | ||
public Page<City> findAllCities(Pageable pageable) { | ||
return repository.findAll(pageable); | ||
} | ||
|
||
@Override | ||
public City findById(Long id) { | ||
return repository.findById(id); | ||
} | ||
|
||
@Override | ||
public Page<City> findCities(String query, Pageable pageable) { | ||
if (StringUtils.isEmpty(query)) { | ||
return findAllCities(pageable); | ||
} | ||
|
||
String[] parts = query.split("="); | ||
if (parts[0].equalsIgnoreCase("name")) { | ||
return repository.findByNameIgnoreCase(parts[1], pageable); | ||
} | ||
if (parts[0].equalsIgnoreCase("state")) { | ||
return repository.findByStateCodeIgnoreCase(parts[1], pageable); | ||
} | ||
if (parts[0].equalsIgnoreCase("postalCode")) { | ||
return repository.findByPostalCode(parts[1], pageable); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.cities.web; | ||
|
||
import com.example.cities.domain.City; | ||
import com.example.cities.services.CityService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestController | ||
public class HomeController { | ||
|
||
@Autowired | ||
private CityService cityService; | ||
|
||
@RequestMapping("/") | ||
public Map<String, Object> home(Pageable pageable) { | ||
Page<City> allCities = cityService.findAllCities(pageable); | ||
|
||
Map<String, Object> response = new HashMap<String, Object>(); | ||
response.put("cities", allCities); | ||
return response; | ||
} | ||
|
||
@RequestMapping("/city/{id}") | ||
public City cityById(@PathVariable Long id) { | ||
return cityService.findById(id); | ||
} | ||
|
||
@RequestMapping("/search") | ||
public Page<City> search(@RequestParam("q") String query, Pageable pageable) { | ||
return cityService.findCities(query, pageable); | ||
} | ||
} |
Oops, something went wrong.