Skip to content

Commit

Permalink
initializing data via data.sql file closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Jun 8, 2019
1 parent e7c7d8e commit 1e4050d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package guru.springframework.msscbeerservice.bootstrap;

import guru.springframework.msscbeerservice.domain.Beer;
import guru.springframework.msscbeerservice.repositories.BeerRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.UUID;

/**
* Created by jt on 2019-05-17.
*/
@Component
//@Component
public class BeerLoader implements CommandLineRunner {

public static final String BEER_1_UPC = "0631234200036";
public static final String BEER_2_UPC = "0631234300019";
public static final String BEER_3_UPC = "0083783375213";
public static final UUID BEER_1_UUID = UUID.fromString("0a818933-087d-47f2-ad83-2f986ed087eb");
public static final UUID BEER_2_UUID = UUID.fromString("a712d914-61ea-4623-8bd0-32c0f6545bfd");
public static final UUID BEER_3_UUID = UUID.fromString("026cc3c8-3a0c-4083-a05b-e908048c1b08");

private final BeerRepository beerRepository;

Expand All @@ -25,38 +26,7 @@ public BeerLoader(BeerRepository beerRepository) {

@Override
public void run(String... args) throws Exception {
loadBeerObjects();
}

private void loadBeerObjects() {
if(beerRepository.count() == 0){

beerRepository.save(Beer.builder()
.beerName("Mango Bobs")
.beerStyle("IPA")
.quantityToBrew(200)
.minOnHand(12)
.upc(BEER_1_UPC)
.price(new BigDecimal("12.95"))
.build());

beerRepository.save(Beer.builder()
.beerName("Galaxy Cat")
.beerStyle("PALE_ALE")
.quantityToBrew(200)
.minOnHand(12)
.upc(BEER_2_UPC)
.price(new BigDecimal("11.95"))
.build());

beerRepository.save(Beer.builder()
.beerName("No Hammers On The Bar")
.beerStyle("PALE_ALE")
.quantityToBrew(200)
.minOnHand(12)
.upc(BEER_3_UPC)
.price(new BigDecimal("11.95"))
.build());
}
//using data.sql file now
// loadBeerObjects();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

spring.datasource.initialization-mode=EMBEDDED
3 changes: 3 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO beer (id, beer_name, beer_style, created_date, last_modified_date, min_on_hand, quantity_to_brew, price, upc, version ) values ('0a818933-087d-47f2-ad83-2f986ed087eb', 'Mango Bobs', 'IPA', CURRENT_TIMESTAMP , CURRENT_TIMESTAMP , 12, 200, 12.95, '0631234200036', 1);
INSERT INTO beer (id, beer_name, beer_style, created_date, last_modified_date, min_on_hand, quantity_to_brew, price, upc, version ) values ('a712d914-61ea-4623-8bd0-32c0f6545bfd', 'Galaxy Cat', 'PALE_ALE', CURRENT_TIMESTAMP , CURRENT_TIMESTAMP , 12, 200, 12.95, '0631234300019', 1);
INSERT INTO beer (id, beer_name, beer_style, created_date, last_modified_date, min_on_hand, quantity_to_brew, price, upc, version ) values ('026cc3c8-3a0c-4083-a05b-e908048c1b08', 'Pinball Porter', 'PORTER', CURRENT_TIMESTAMP , CURRENT_TIMESTAMP , 12, 200, 12.95, '0083783375213', 1);

0 comments on commit 1e4050d

Please sign in to comment.