add fist admin user after start
This commit is contained in:
parent
0a7722d612
commit
709c80fa37
@ -0,0 +1,37 @@
|
|||||||
|
package org.hso.ecommerce.components;
|
||||||
|
|
||||||
|
import org.hso.ecommerce.entities.booking.PaymentMethod;
|
||||||
|
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 java.sql.Timestamp;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@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.name = "admin";
|
||||||
|
firstAdmin.defaultPayment = new PaymentMethod();
|
||||||
|
firstAdmin.defaultPayment.creditCardNumber = ""; //set empty number
|
||||||
|
firstAdmin.email = "admin";
|
||||||
|
firstAdmin.isActive = true;
|
||||||
|
firstAdmin.isEmployee = true;
|
||||||
|
firstAdmin.setPassword("admin");
|
||||||
|
userRepository.save(firstAdmin); //save to DB
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,23 +10,21 @@ import javax.annotation.PostConstruct;
|
|||||||
@Component
|
@Component
|
||||||
public class SlotInitializer {
|
public class SlotInitializer {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private final SlotRepository slotRepository = null;
|
private final SlotRepository slotRepository = null;
|
||||||
|
|
||||||
// TODO: use values form cfg.
|
// TODO: use values form cfg.
|
||||||
private final int NUM_SLOTS = 50;
|
private final int NUM_SLOTS = 50;
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
for (int i = 1; i <= NUM_SLOTS; i++) {
|
|
||||||
if (!slotRepository.findBySlotNum(i).isPresent()) {
|
|
||||||
Slot slotAdded = new Slot();
|
|
||||||
slotAdded.slotNum = i;
|
|
||||||
slotRepository.save(slotAdded);
|
|
||||||
|
|
||||||
System.out.println("Added Slot " + i + " to DB");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
for (int i = 1; i <= NUM_SLOTS; i++) {
|
||||||
|
if (!slotRepository.findBySlotNum(i).isPresent()) {
|
||||||
|
Slot slotAdded = new Slot();
|
||||||
|
slotAdded.slotNum = i;
|
||||||
|
slotRepository.save(slotAdded);
|
||||||
|
System.out.println("Added Slot " + i + " to DB");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,14 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
|||||||
@Query("SELECT c FROM User c WHERE c.email = :email")
|
@Query("SELECT c FROM User c WHERE c.email = :email")
|
||||||
Optional<User> findByEmail(String email);
|
Optional<User> findByEmail(String email);
|
||||||
|
|
||||||
|
/*SELECT count(*) FROM users WHERE is_employee == 1;
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@Query("SELECT count(*) FROM User WHERE isEmployee = true")
|
||||||
|
Optional<Integer> numberOfEmployees();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user