Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Oct 17, 2013
0 parents commit aa3f40f
Show file tree
Hide file tree
Showing 11 changed files with 43,036 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
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
31 changes: 31 additions & 0 deletions build.gradle
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'
}

8 changes: 8 additions & 0 deletions manifest.yml
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
17 changes: 17 additions & 0 deletions src/main/java/com/example/cities/CitiesApplication.java
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);
}
}
101 changes: 101 additions & 0 deletions src/main/java/com/example/cities/domain/City.java
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 src/main/java/com/example/cities/repositories/CityRepository.java
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 src/main/java/com/example/cities/services/CityService.java
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);
}
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;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/example/cities/web/HomeController.java
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);
}
}
Loading

0 comments on commit aa3f40f

Please sign in to comment.