Browse Source

Implement basic SQL support with a test; temporarily change some

datatypes
Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
178afc318e
3 changed files with 78 additions and 18 deletions
  1. +26
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java
  2. +39
    -18
      bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java
  3. +13
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java

+ 26
- 0
bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java View File

@ -1,14 +1,40 @@
package com.fjordtek.bookstore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import com.fjordtek.bookstore.model.BookRepository;
import com.fjordtek.bookstore.model.*;
@SpringBootApplication
public class BookstoreApplication extends SpringBootServletInitializer {
private static final Logger commonLogger = LoggerFactory.getLogger(BookstoreApplication.class);
public static void main(String[] args) {
SpringApplication.run(BookstoreApplication.class, args);
}
@Bean
public CommandLineRunner bookDatabaseRunner(BookRepository repository) {
return (args) -> {
commonLogger.info("Add new books to a database");
repository.save(new Book("Book 1 title", "Book 1 author", 2020, "aaa-b2b-c3c-444", 40.00));
repository.save(new Book("Book 2 title", "Book 2 author", 2005, "111-2b2-3c3-ddd", 20.17));
commonLogger.info("New books in the database");
for (Book student : repository.findAll()) {
commonLogger.info(student.toString());
}
};
}
}

+ 39
- 18
bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java View File

@ -8,15 +8,25 @@ import javax.validation.constraints.Size;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.PastOrPresent;
import java.math.BigDecimal;
import java.time.LocalDate;
//import java.sql.Timestamp;
//import javax.validation.constraints.PastOrPresent;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
////////////////////
// Primary key value in database
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
////////////////////
// Attributes with hard-coded constraints
@ -33,9 +43,11 @@ public class Book {
private String author;
@NotNull
@PastOrPresent
@DateTimeFormat(pattern = "yyyy")
private LocalDate year;
//@PastOrPresent
//@DateTimeFormat(pattern = "yyyy")
// TODO: Prefer Timestamp data type
// private Timestamp year;
private int year;
@NotNull
@NotEmpty(message = "Please provide an ISBN code for the book")
@ -46,14 +58,16 @@ public class Book {
@NotNull
@DecimalMin(value = "0.01", message = "Too low price value" )
@DecimalMax(value = "999.99", message = "Too high price value")
// Keep precision
private BigDecimal price;
// TODO: Use BigDecimal to keep exact precision?
private double price;
////////////////////
// Attribute setters
// TODO consider accessibility restrictions?
// Id generated automatically
public void setTitle(String title) {
this.title = title;
}
@ -62,7 +76,7 @@ public class Book {
this.author = author;
}
public void setYear(LocalDate year) {
public void setYear(int year) {
this.year = year;
}
@ -70,7 +84,7 @@ public class Book {
this.isbn = isbn;
}
public void setPrice(BigDecimal price) {
public void setPrice(double price) {
this.price = price;
}
@ -79,6 +93,8 @@ public class Book {
// TODO consider accessibility restrictions?
// Id generated automatically
public String getTitle() {
return title;
}
@ -87,7 +103,7 @@ public class Book {
return author;
}
public LocalDate getYear() {
public int getYear() {
return year;
}
@ -95,25 +111,30 @@ public class Book {
return isbn;
}
public BigDecimal getPrice() {
public double getPrice() {
return price;
}
////////////////////
// Class constructors
public Book() {
public Book() {}
public Book(String title, String author, int year, String isbn, double price) {
// super();
this.title = title;
this.author = author;
this.year = year;
this.isbn = isbn;
this.price = price;
}
// ...
////////////////////
// Class overrides
@Override
public String toString() {
// TODO
return "N/A (placeholder)";
return this.id + ", " + this.title + ", " + this.author + ", " + this.year + ", " + this.isbn + ", " + this.price;
}
// ...


+ 13
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java View File

@ -0,0 +1,13 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.model;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface BookRepository extends CrudRepository<Book, Long> {
List<Book> findById(String title);
}

Loading…
Cancel
Save