From 9681dceb8bc02e39b9d252e05902b8e488a8fdc2 Mon Sep 17 00:00:00 2001
From: Seil0 <seil0@mosad.xyz>
Date: Mon, 20 Jan 2020 20:22:37 +0100
Subject: [PATCH] reworked login/register, GetMapping fixes

---
 .../hso/ecommerce/app/RequestController.java  | 40 ++++-------
 .../org/hso/ecommerce/contoller/Login.java    | 41 ++++++++++++
 .../src/main/resources/static/css/dialog.css  | 19 ------
 .../accountSettings.html}                     | 24 +++----
 .../resources/templates/fragments/header.html |  2 +-
 .../main/resources/templates/greeting.html    | 10 ---
 .../src/main/resources/templates/home.html    |  2 +-
 .../src/main/resources/templates/login.html   | 66 ++++++++++++-------
 .../main/resources/templates/register.html    | 50 +++++++++++---
 9 files changed, 151 insertions(+), 103 deletions(-)
 create mode 100644 prototype/src/main/java/org/hso/ecommerce/contoller/Login.java
 delete mode 100644 prototype/src/main/resources/static/css/dialog.css
 rename prototype/src/main/resources/templates/{customerAccountSettings.html => customer/accountSettings.html} (79%)
 delete mode 100644 prototype/src/main/resources/templates/greeting.html

diff --git a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java
index f487afb..629ff60 100644
--- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java
+++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java
@@ -1,5 +1,6 @@
 package org.hso.ecommerce.app;
 
+import org.hso.ecommerce.contoller.Login;
 import org.hso.ecommerce.db.CustomerRepository;
 import org.hso.ecommerce.entities.Customer;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -38,12 +39,6 @@ public class RequestController {
         return "home";
     }
 
-    @GetMapping("/greeting")
-    public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
-        model.addAttribute("name", name);
-        return "greeting";
-    }
-
     @GetMapping("/articledetail")
     public String articledetail() {
         return "articledetail";
@@ -51,12 +46,12 @@ public class RequestController {
 
     @GetMapping("/searchresults")
     public String searchresults() {
-        return "searchresults";
+        return "searchResults";
     }
 
     @GetMapping("/shoppingcart")
     public String shoppingcart() {
-        return "shoppingcart";
+        return "shoppingCart";
     }
 
     @GetMapping("/intern/customerdetail")
@@ -133,7 +128,7 @@ public class RequestController {
         model.addAttribute("zipcode", "12345");
         model.addAttribute("country", "Musterland");
 
-        return "customerAccountSettings";
+        return "/customer/accountSettings";
     }
 
     @RequestMapping(value="/updateAccountSettings", method=RequestMethod.POST, params="action=updateAccountSettings")
@@ -142,7 +137,7 @@ public class RequestController {
         System.out.println(customer.username);
         System.out.println(customer.password);
 
-        return "redirect:/customer/accountsettings";
+        return "/customer/accountsettings";
     }
 
     @GetMapping("/login")
@@ -154,26 +149,13 @@ public class RequestController {
     }
     @RequestMapping(value="/login", method=RequestMethod.POST, params="action=login")
     public String loginAction(@ModelAttribute Customer customer, HttpServletResponse response) {
-        // do the login magic and get a loginToken
-        System.out.println(customer.username);
-        System.out.println(customer.password);
-
-        List<Customer> customers = customerRepo.findByUsername(customer.username);
-
-        if (customers.size() == 1 && (customers.get(0).username.equals(customer.username) && customers.get(0).password.equals(customer.password))) {
-            System.out.println("The login data is valid");
-
-            String loginToken = UUID.randomUUID().toString();
-
-            // set the loginToken as session cookie
-            Cookie cookie = new Cookie("loginToken", loginToken);
+        Cookie cookie = new Login(customerRepo).getLoginToken(customer);
+        if (cookie != null) {
             response.addCookie(cookie);
+            return "redirect:home";
         } else {
-            System.out.println("The login data is invalid!");
-            return "redirect:/login"; // redirect so the input files get cleared, otherwise only pwd gets cleared
+            return "redirect:login"; // redirect so the input files get cleared, otherwise only pwd gets cleared
         }
-
-        return "redirect:/home";
     }
 
     @GetMapping("/register")
@@ -195,7 +177,7 @@ public class RequestController {
         if (customerRepo.findByUsername(customer.username).size() != 0) {
             // TODO
             System.out.println("The customer exists already");
-            return "register";
+            return "redirect:register";
         } else {
             customerRepo.save(customer);
             System.out.println(customerRepo.findByUsername(customer.username).size());
@@ -207,7 +189,7 @@ public class RequestController {
         // set the loginToken as session cookie
         Cookie cookie = new Cookie("loginToken", loginToken);
         response.addCookie(cookie);
-        return "redirect:/home";
+        return "home";
     }
 
     @GetMapping("/about")
diff --git a/prototype/src/main/java/org/hso/ecommerce/contoller/Login.java b/prototype/src/main/java/org/hso/ecommerce/contoller/Login.java
new file mode 100644
index 0000000..e545de8
--- /dev/null
+++ b/prototype/src/main/java/org/hso/ecommerce/contoller/Login.java
@@ -0,0 +1,41 @@
+package org.hso.ecommerce.contoller;
+
+import org.hso.ecommerce.db.CustomerRepository;
+import org.hso.ecommerce.entities.Customer;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.servlet.http.Cookie;
+import java.util.List;
+import java.util.UUID;
+
+public class Login {
+
+    private final CustomerRepository customerRepo;
+
+    @Autowired
+    public Login(CustomerRepository customerRepo) {
+        this.customerRepo = customerRepo;
+    }
+
+    public Cookie getLoginToken(Customer customer) {
+        // do the login magic and get a loginToken
+        System.out.println(customer.username);
+        System.out.println(customer.password);
+
+        List<Customer> customers = customerRepo.findByUsername(customer.username);
+
+        if (customers.size() == 1 && (customers.get(0).username.equals(customer.username) && customers.get(0).password.equals(customer.password))) {
+            System.out.println("The login data is valid");
+
+            String loginToken = UUID.randomUUID().toString();
+
+            // set the loginToken as session cookie
+            return new Cookie("loginToken", loginToken);
+        } else {
+            System.out.println("The login data is invalid!");
+            return null; // redirect so the input files get cleared, otherwise only pwd gets cleared
+        }
+
+
+    }
+}
diff --git a/prototype/src/main/resources/static/css/dialog.css b/prototype/src/main/resources/static/css/dialog.css
deleted file mode 100644
index 114eb80..0000000
--- a/prototype/src/main/resources/static/css/dialog.css
+++ /dev/null
@@ -1,19 +0,0 @@
-.dialog {
-    display: none; /* Hidden by default */
-    position: fixed; /* Stay in place */
-    z-index: 1;
-    left: 0;
-    top: 0;
-    width: 100%; /* Full width */
-    height: 100%; /* Full height */
-    overflow: auto; /* Enable scroll if needed */
-    padding-top: 60px;
-}
-
-.dialog-content {
-    background-color: var(--c-black);
-    margin: 5% auto 15% auto; /* 5% from the top, 15% from the bottom and centered */
-    border: 1px solid #888;
-    width: 50%; /* Could be more or less, depending on screen size */
-
-}
\ No newline at end of file
diff --git a/prototype/src/main/resources/templates/customerAccountSettings.html b/prototype/src/main/resources/templates/customer/accountSettings.html
similarity index 79%
rename from prototype/src/main/resources/templates/customerAccountSettings.html
rename to prototype/src/main/resources/templates/customer/accountSettings.html
index 7577315..d891923 100644
--- a/prototype/src/main/resources/templates/customerAccountSettings.html
+++ b/prototype/src/main/resources/templates/customer/accountSettings.html
@@ -10,57 +10,57 @@
     <nav th:replace="fragments/header :: header">Header</nav>
     <main class='content-width'>
         <div class="content-width">
-            <h1> Account Settings </h1>
+            <h1> Account Einstellungen </h1>
 
 
             <form class="detailgrid" th:action="@{/updateAccountSettings}" th:object="${customer}" method="post">
 
                 <div class="l">
-                    <h2>General Settings</h2>
+                    <h2>Einstellungen</h2>
                     <p class="l">
-                        <label for="username">Username *</label>
+                        <label for="username">Benutzername *</label>
                         <input class="linestyle full-width" type="text" id="username" th:field="*{username}"  th:value="${username}" />
                     </p>
                     <p class="l">
-                        <label for="fullname">Full Name *</label>
+                        <label for="fullname">Name *</label>
                         <input class="linestyle full-width" type="text" id="fullname" th:value="${fullname}" />
                     </p>
                     <p class="l">
-                        <label for="email">Email Address *</label>
+                        <label for="email">Email Addresse *</label>
                         <input class="linestyle full-width" type="text" id="email" th:value="${email}" />
                     </p>
                     <p class="l">
-                        <label for="password">Password *</label>
+                        <label for="password">Passwort *</label>
                         <input class="linestyle full-width" type="password" id="password" th:field="*{password}" th:value="${password}" />
                     </p>
                 </div>
 
                 <div class="l">
-                    <h2>Shipment Settings</h2>
+                    <h2>Versand</h2>
                     <p class="l">
-                        <label for="street">Street + Hous No. *</label>
+                        <label for="street">Straße und Hausnummer *</label>
                         <input class="linestyle full-width" type="text" id="street" th:value="${street}" />
                     </p>
 
                     <div class="l">
                         <p class="m">
-                            <label for="city">City *</label>
+                            <label for="city">Stadt *</label>
                             <input class="linestyle full-width" type="text" id="city" th:value="${city}" />
                         </p>
                         <p class="s">
-                            <label for="zipcode">ZIP Code *</label>
+                            <label for="zipcode">Postleitzahl *</label>
                             <input class="linestyle full-width" type="text" id="zipcode" th:value="${zipcode}" />
                         </p>
                     </div>
 
                     <p class="l">
-                        <label for="country">Country *</label>
+                        <label for="country">Land *</label>
                         <input class="linestyle full-width" type="text" id="country" th:value="${country}" />
                     </p>
                 </div>
 
                 <div class="l">
-                    <h2>Payment Settings</h2>
+                    <h2>Bezahlung</h2>
                     <h3>TODO</h3>
                 </div>
 
diff --git a/prototype/src/main/resources/templates/fragments/header.html b/prototype/src/main/resources/templates/fragments/header.html
index 71b8aac..6385960 100644
--- a/prototype/src/main/resources/templates/fragments/header.html
+++ b/prototype/src/main/resources/templates/fragments/header.html
@@ -14,7 +14,7 @@
                 <button>Finden</button>
             </form>
             <a th:unless="${customer}" class="button" th:href="@{/login}">Login</a>
-            <div class="notifications" th:if="${customer}">
+            <div class="notifications" th:if="${customer}" style="display: none;">
                 <input id="messages" type="checkbox"></input>
                 <label for="messages" class="button">
                     <img th:src="@{/img/bell.svg}" width="25" height="28" alt="Nachrichten"/>
diff --git a/prototype/src/main/resources/templates/greeting.html b/prototype/src/main/resources/templates/greeting.html
deleted file mode 100644
index f96bfc9..0000000
--- a/prototype/src/main/resources/templates/greeting.html
+++ /dev/null
@@ -1,10 +0,0 @@
-<!DOCTYPE HTML>
-<html xmlns:th="http://www.thymeleaf.org">
-<head> 
-    <title>This is a simple Spring Boot Web App</title>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-</head>
-<body>
-    <p th:text="'Hello, ' + ${name} + '!'" />
-</body>
-</html>
diff --git a/prototype/src/main/resources/templates/home.html b/prototype/src/main/resources/templates/home.html
index af1a385..69cf445 100644
--- a/prototype/src/main/resources/templates/home.html
+++ b/prototype/src/main/resources/templates/home.html
@@ -70,7 +70,7 @@
                      <h2>Werde jetzt Kunde</h2>
                      <p> Jetzt Kunde werden und viele Vorteile sichern,
                          wie  z.B. personalisierte Empfehlungen. </p>
-                     <button>Registieren</button>
+                     <a class="button" href="/register">Registieren</a>
                   </div>
                </div>
             </div>
diff --git a/prototype/src/main/resources/templates/login.html b/prototype/src/main/resources/templates/login.html
index 326c0d5..b4cb8f5 100644
--- a/prototype/src/main/resources/templates/login.html
+++ b/prototype/src/main/resources/templates/login.html
@@ -3,33 +3,53 @@
 <head>
     <meta charset="utf-8">
     <title>e-commerce</title>
-    <link href="../static/css/ecom.css" rel="stylesheet" th:href="@{/css/ecom.css}"/>
+    <link rel="stylesheet" th:href="@{/css/ecom.css}"/>
 </head>
 <body>
-    <nav th:replace="fragments/header :: header">Header</nav>
-    <div class='hero'>
-        <main>
-            <form class="content-width" th:action="@{/login}" th:object="${customer}" method="post">
-                <div class="container">
-                    <label><b>Username</b></label>
-                    <input type="text" th:field="*{username}" placeholder="Enter Username" name="uname" required>
+<nav th:replace="fragments/header :: header">Header</nav>
+    <main>
 
-                    <label><b>Password</b></label>
-                    <input type="password" th:field="*{password}" placeholder="Enter Password" name="pwd" required>
+        <div class="content-width">
+            <div class="grid center">
 
-                    <button type="submit" name="action" value="login">Login</button>
-                    <label>
-                        <input type="checkbox" checked="checked" name="remember"> Remember me
-                    </label>
-                </div>
+                <form class="detailgrid" th:action="@{/login}" th:object="${customer}" method="post" style="background-color:lightgray;">
 
-                <div class="container">
-                    <button type="button" onclick="document.getElementById('login').style.display='none'" class="cancelbtn">Cancel</button>
-                    <span class="psw">Forgot <a href="#">password?</a></span>
-                </div>
-            </form>
-        </main>
-    </div>
-    <footer th:replace="fragments/footer :: footer"></footer>
+                    <div class="grid m">
+                        <div class="grid s"></div>
+                        <label for="username">Benutzername</label>
+                        <input type="text" th:field="*{username}" placeholder="Benutzername" id="username" required>
+                        <div class="grid s"></div>
+                    </div>
+
+                    <div class="grid m">
+                        <div class="grid s"></div>
+                        <label for="password">Passwort</label>
+                        <input type="password" th:field="*{password}" placeholder="Passwort" id="password" required>
+                        <div class="grid s"></div>
+                    </div>
+
+                    <div class="grid m">
+                        <div class="grid s"></div>
+                        <label></label>
+                        <label>
+                            <input type="checkbox" checked="checked" name="remember"> Login speichern
+                        </label>
+                        <div class="grid s"></div>
+                    </div>
+
+                    <div class="grid m">
+                        <label></label>
+                        <button type="submit" name="action" value="login">Login</button>
+                        <a href="#">Passwort vergessen?</a>
+                        <div class="grid s"></div>
+                    </div>
+
+                </form>
+
+            </div>
+        </div>
+
+    </main>
+<footer th:replace="fragments/footer :: footer"></footer>
 </body>
 </html>
diff --git a/prototype/src/main/resources/templates/register.html b/prototype/src/main/resources/templates/register.html
index 1d1a78d..a72bdf9 100644
--- a/prototype/src/main/resources/templates/register.html
+++ b/prototype/src/main/resources/templates/register.html
@@ -9,17 +9,51 @@
 <body>
     <nav th:replace="fragments/header :: header">Header</nav>
     <main>
-        <form class="dialog-content" th:action="@{/register}" th:object="${customer}" method="post">
-            <div class="container">
-                <label for="username">Username</label>
-                <input type="text" th:field="*{username}" placeholder="Enter Username" id="username" required>
 
-                <label><b>Password</b></label>
-                <input type="password" th:field="*{password}" placeholder="Enter Password" name="pwd" required>
+        <div class="content-width">
+            <div class="grid center">
+
+                <form class="detailgrid" th:action="@{/register}" th:object="${customer}" method="post" style="background-color:lightgray;">
+
+                    <div class="l">
+                        <label for="username">Benutzername *</label>
+                        <input class="linestyle full-width" type="text" id="username" th:field="*{username}"  th:value="${username}" required/>
+                    </div>
+
+                    <div class="l">
+                        <label for="fullname">Name *</label>
+                        <input class="linestyle full-width" type="text" id="fullname" th:value="${fullname}" required/>
+                    </div>
+
+                    <div class="l">
+                        <label for="email">Email Addresse *</label>
+                        <input class="linestyle full-width" type="text" id="email" th:value="${email}" required/>
+                    </div>
+
+                    <div class="l">
+                        <label for="password">Passwort *</label>
+                        <input class="linestyle full-width" type="password" id="password" th:field="*{password}" th:value="${password}" required/>
+                    </div>
+
+                    <div class="s">
+                        <label>
+                            <input type="checkbox" name="agb" required> Ich stimme den AGB's zu
+                        </label>
+                        <label>
+                            <input type="checkbox" name="privacy" required> Ich habe die Datneschutzerklärung gelesen
+                        </label>
+
+                    </div>
+
+                    <div class="m">
+                        <button type="submit" name="action" value="register">Registrieren</button>
+                    </div>
+
+                </form>
 
-                <button type="submit" name="action" value="register">Login</button>
             </div>
-        </form>
+        </div>
+
     </main>
     <footer th:replace="fragments/footer :: footer"></footer>
 </body>