Skip to content
Open

Db #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
.idea
*.iml
.gradle
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Or follow these steps, after installing the [Heroku Toolbelt](https://toolbelt.h
```sh-session
$ git clone https://github.com/kissaten/spring-boot-android-service
$ cd spring-boot-android-service
$ heroku create
$ git checkout -t origin/db
$ heroku create --addons heroku-postgresql
$ git push heroku master
$ heroku open
```
3 changes: 2 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "Spring Boot Demo for Android",
"description": "This is a small demo application for showing how to run a Spring Boot application on Heroku.",
"repository": "https://github.com/jkutner/spring-boot-android-service"
"repository": "https://github.com/jkutner/spring-boot-android-service",
"addons" : [ "heroku-postgresql" ]
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ repositories {

dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.postgresql:postgresql:9.4-1203-jdbc42')
compile('javax.validation:validation-api:1.1.0.Final')
compile('org.liquibase:liquibase-core:3.4.1')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Fri Aug 19 07:43:28 CDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
Empty file modified gradlew
100644 → 100755
Empty file.
32 changes: 32 additions & 0 deletions src/main/java/com/example/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example;

import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotEmpty
private String isbn;

public Long getId() {
return id;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/example/BookController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

private BookRepository repository;

@Autowired
public BookController(BookRepository repository) {
this.repository = repository;
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Book> get(@PathVariable("id") Long id) {
Book book = repository.findOne(id);
if (null == book) {
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Book>(book, HttpStatus.OK);
}

@RequestMapping(value = "/new", method = RequestMethod.POST)
public ResponseEntity<Book> update(@RequestBody Book book) {
repository.save(book);
return get(book.getId());
}

@RequestMapping
public List<Book> all() {
return repository.findAll();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/BookRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/DatabaseConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new org.apache.tomcat.jdbc.pool.DataSource();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DemoApplication {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello! This is a demo application linked to this tutorial: ";
return "Hello! This is a demo application linked to this tutorial: http://jkutner.github.io/2016/08/18/android-backend-api-heroku-retrofit.html";
}

@RequestMapping("/hello")
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
20 changes: 20 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
databaseChangeLog:
- changeSet:
id: 1
author: jkutner
changes:
- createTable:
tableName: book
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: isbn
type: varchar(255)
constraints:
nullable: false