Skip to content

Commit

Permalink
Merge pull request #291 from vianneynara/feature/284-ensure-program-t…
Browse files Browse the repository at this point in the history
…o-start-with-unseeded-settings

Feature/284 ensure program to start with unseeded settings
  • Loading branch information
vianneynara authored Dec 18, 2024
2 parents c4ead50 + 36f3fad commit e7c186b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.kons.kuenyawz.boostrappers;

import dev.kons.kuenyawz.configurations.ApplicationProperties;
import dev.kons.kuenyawz.dtos.account.AccountRegistrationDto;
import dev.kons.kuenyawz.entities.Account;
import dev.kons.kuenyawz.repositories.AccountRepository;
import dev.kons.kuenyawz.repositories.ProductRepository;
import dev.kons.kuenyawz.services.entity.AccountService;
import dev.kons.kuenyawz.services.logic.AccountCsvService;
import dev.kons.kuenyawz.services.logic.ProductCsvService;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +26,7 @@ public class DatabaseBootstrapper implements CommandLineRunner {
private final ProductRepository productRepository;
private final AccountCsvService accountCsvService;
private final AccountRepository accountRepository;
private final AccountService accountService;

private static final String PATH_TO_PRODUCT_SEEDER = "seeders/Products.csv";
private static final String PATH_TO_ACCOUNT_SEEDER = "seeders/Accounts.csv";
Expand Down Expand Up @@ -71,7 +74,21 @@ private void start() {
if (properties.seeder().getSeedAccounts()) {
log.info("Injecting accounts...");
injectAccounts();
} else {
log.info("Injecting an admin account...");
final AccountRegistrationDto accountRegistrationDto = AccountRegistrationDto.builder()
.fullName("Admin")
.phone("81100001")
.password("admin")
.build();
try {
accountService.createAccount(accountRegistrationDto);
log.info("Admin account created (id: {})", accountRegistrationDto.getPhone());
} catch (Exception e) {
log.error("Admin account already exists (id: {})", accountRegistrationDto.getPhone());
}
}

if (properties.seeder().getSeedProducts()) {
log.info("Injecting products...");
injectProducts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public static class Frontend {
@Getter
@Setter
public static class Seeder {
private Boolean seedAccounts = false;
private Boolean seedProducts = false;
private Boolean seedAccounts;
private Boolean seedProducts;
}

@Getter
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ application:
accepted-image-extensions: png,jpg,jpeg,webp
max-variant-quantity: 250
otp-format: numeric
seeder:
seed-accounts: true
seed-products: true

server:
port: 8081
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.kons.kuenyawz.dtos.product.VariantPostDto;
import dev.kons.kuenyawz.repositories.AccountRepository;
import dev.kons.kuenyawz.repositories.ProductRepository;
import dev.kons.kuenyawz.services.entity.AccountService;
import dev.kons.kuenyawz.services.entity.ProductService;
import dev.kons.kuenyawz.services.logic.AccountCsvService;
import dev.kons.kuenyawz.services.logic.ProductCsvService;
Expand Down Expand Up @@ -43,12 +44,16 @@ class DatabaseBootstrapperTest {
private AccountRepository accountRepository;

DatabaseBootstrapper databaseBootstrapper;

@Autowired
private ProductService productService;

@Autowired
private AccountService accountService;

@BeforeEach
void setUp() {
databaseBootstrapper = new DatabaseBootstrapper(properties, productCsvService, productRepository, accountCsvService, accountRepository);
databaseBootstrapper = new DatabaseBootstrapper(properties, productCsvService, productRepository, accountCsvService, accountRepository, accountService);
}

@Test
Expand Down

0 comments on commit e7c186b

Please sign in to comment.