diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java index 6027614..4fa7bb8 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java @@ -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()); + } + + }; + } + } diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java index 185c0e7..d58c846 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java @@ -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; } // ... diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java new file mode 100644 index 0000000..c59f024 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/BookRepository.java @@ -0,0 +1,13 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + + List findById(String title); + +} \ No newline at end of file