feature/create_admin #73
@ -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
 | 
			
		||||
public class SlotInitializer {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private final SlotRepository slotRepository = null;
 | 
			
		||||
	@Autowired
 | 
			
		||||
	private final SlotRepository slotRepository = null;
 | 
			
		||||
 | 
			
		||||
    // TODO: use values form cfg.
 | 
			
		||||
    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");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
	// TODO: use values form cfg.
 | 
			
		||||
	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");
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -26,8 +26,6 @@ public class RegisterController {
 | 
			
		||||
			@RequestParam("username") String username, @RequestParam("password") String password,
 | 
			
		||||
			@RequestParam("password2") String password2, @RequestParam("salutation") String salutation,
 | 
			
		||||
			@RequestParam("name") String name, @RequestParam("address") String address,
 | 
			
		||||
			@RequestParam("type") String type, // TODO store
 | 
			
		||||
			@RequestParam("ad") String ad, // TODO store
 | 
			
		||||
			HttpSession session) {
 | 
			
		||||
		Optional<User> user = userRepository.findByEmail(username);
 | 
			
		||||
		if (user.isPresent()) {
 | 
			
		||||
 | 
			
		||||
@ -10,9 +10,9 @@ import java.util.Optional;
 | 
			
		||||
@Repository
 | 
			
		||||
public interface UserRepository extends JpaRepository<User, Long> {
 | 
			
		||||
 | 
			
		||||
    @Query("SELECT c FROM User c WHERE c.email = :email")
 | 
			
		||||
    Optional<User> findByEmail(String email);
 | 
			
		||||
	@Query("SELECT c FROM User c WHERE c.email = :email")
 | 
			
		||||
	Optional<User> findByEmail(String email);
 | 
			
		||||
 | 
			
		||||
	@Query("SELECT count(*) FROM User WHERE isEmployee = true")
 | 
			
		||||
	Optional<Integer> numberOfEmployees();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -54,23 +54,6 @@
 | 
			
		||||
            <textarea rows="5" class="full-width" type="text" name="address" id="address"
 | 
			
		||||
               placeholder="Optional: Zusatz
Optional: Unternehmen
Straße Hausnummer
Postleitzeit Ort
Land"></textarea>
 | 
			
		||||
            </div>
 | 
			
		||||
            <fieldset>
 | 
			
		||||
            <input type="radio" id="type-priv" name="type" value="priv">
 | 
			
		||||
            <label for="type-priv">Ich bin Privatkunde</label><br>
 | 
			
		||||
            <input type="radio" id="type-bus" name="type" value="bus">
 | 
			
		||||
            <label for="type-bus">Ich bin Geschäftskunde</label><br>
 | 
			
		||||
            </fieldset>
 | 
			
		||||
            <div>
 | 
			
		||||
            <h2> Werbung </h2>
 | 
			
		||||
            </div>
 | 
			
		||||
            <div>
 | 
			
		||||
            <fieldset>
 | 
			
		||||
            <input type="radio" id="ad-y" name="ad" value="y">
 | 
			
		||||
            <label for="ad-y">Ich möchte Werbung erhalten.</label><br>
 | 
			
		||||
            <input type="radio" id="ad-n" name="ad" value="n">
 | 
			
		||||
            <label for="ad-n">Ich möchte keine Werbung erhalten.</label><br>  
 | 
			
		||||
            </fieldset>
 | 
			
		||||
            </div>
 | 
			
		||||
            <div>
 | 
			
		||||
            <button class="full-width" type="submit" name="action" value="login">Registeren</button>
 | 
			
		||||
            <a th:href="@{/terms}">
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user