This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/web_backend/src/main/java/org/hso/ecommerce/components/AdminInitializer.java

37 lines
1.1 KiB
Java
Raw Normal View History

2020-06-15 10:43:16 +02:00
package org.hso.ecommerce.components;
import org.hso.ecommerce.entities.shop.Address;
2020-06-15 10:43:16 +02:00
import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
2020-06-15 10:43:16 +02:00
import java.sql.Timestamp;
import java.util.Optional;
@Component
public class AdminInitializer {
@Autowired
private final UserRepository userRepository = null;
@PostConstruct
public void init() {
Optional<Integer> numberOfEmployees = userRepository.numberOfEmployees();
if (numberOfEmployees.orElse(0) == 0) {
// create first admin user
User firstAdmin = new User();
firstAdmin.created = new Timestamp(System.currentTimeMillis());
firstAdmin.defaultDeliveryAddress = new Address();
firstAdmin.defaultDeliveryAddress.name = "admin";
2020-06-25 11:46:01 +02:00
firstAdmin.defaultPayment = null;
2020-06-15 10:43:16 +02:00
firstAdmin.email = "admin";
firstAdmin.isActive = true;
firstAdmin.isEmployee = true;
firstAdmin.setPassword("admin");
userRepository.save(firstAdmin); //save to DB
}
}
}