Fix showing CustomerOrder all CustomerOrders in my orders. fix displaying orders in random order #108
@ -3,7 +3,6 @@ package org.hso.ecommerce.controller;
 | 
			
		||||
import org.hso.ecommerce.action.user.CreateDeliveryData;
 | 
			
		||||
import org.hso.ecommerce.action.user.UpdateUserSettingsAction;
 | 
			
		||||
import org.hso.ecommerce.api.RestServiceForDelivery;
 | 
			
		||||
import org.hso.ecommerce.entities.shop.Address;
 | 
			
		||||
import org.hso.ecommerce.entities.shop.CustomerOrder;
 | 
			
		||||
import org.hso.ecommerce.entities.user.User;
 | 
			
		||||
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
 | 
			
		||||
@ -12,16 +11,11 @@ import org.hso.ecommerce.uimodel.DeliveryData;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Controller;
 | 
			
		||||
import org.springframework.ui.Model;
 | 
			
		||||
import org.springframework.web.bind.annotation.GetMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.PostMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestParam;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import javax.servlet.http.HttpSession;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@Controller
 | 
			
		||||
@ -44,32 +38,48 @@ public class UserController {
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/settings")
 | 
			
		||||
    public String userSettings(Model model,
 | 
			
		||||
                               HttpSession session
 | 
			
		||||
                               @RequestAttribute("user") User user
 | 
			
		||||
    ) {
 | 
			
		||||
        long userId = (long) session.getAttribute("userId");
 | 
			
		||||
        User user = userRepository.findById(userId).get();
 | 
			
		||||
        if(user.defaultDeliveryAddress == null){
 | 
			
		||||
            user.defaultDeliveryAddress = new Address();
 | 
			
		||||
        }
 | 
			
		||||
        model.addAttribute("user", user);
 | 
			
		||||
 | 
			
		||||
        return "user/settings";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/orders/")
 | 
			
		||||
    public String userOrders(HttpSession session,
 | 
			
		||||
                             Model model
 | 
			
		||||
    public String userOrders(
 | 
			
		||||
            @RequestAttribute("user") User user,
 | 
			
		||||
            Model model
 | 
			
		||||
    ) {
 | 
			
		||||
        List<CustomerOrder> orders = customerOrderRepository.getAllOrders();
 | 
			
		||||
        List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId(user.id);
 | 
			
		||||
 | 
			
		||||
        Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().
 | 
			
		||||
                collect(Collectors.toMap(Function.identity(), c -> CreateDeliveryData.getDeliveryDataFromCustomerOrder(c, customerOrderRepository, restServiceForDelivery)));
 | 
			
		||||
        List<CustomerOrderDelivery> customerOrderDeliveryDataMap = orders
 | 
			
		||||
                .stream()
 | 
			
		||||
                .map(o -> new CustomerOrderDelivery(o, CreateDeliveryData.getDeliveryDataFromCustomerOrder(o, customerOrderRepository, restServiceForDelivery)))
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
        model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
 | 
			
		||||
 | 
			
		||||
        return "user/orders/index";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class CustomerOrderDelivery {
 | 
			
		||||
        private CustomerOrder customerOrder;
 | 
			
		||||
        private DeliveryData deliveryData;
 | 
			
		||||
 | 
			
		||||
        public CustomerOrderDelivery(CustomerOrder customerOrder, DeliveryData deliveryData) {
 | 
			
		||||
            this.customerOrder = customerOrder;
 | 
			
		||||
            this.deliveryData = deliveryData;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public CustomerOrder getCustomerOrder() {
 | 
			
		||||
            return customerOrder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public DeliveryData getDeliveryData() {
 | 
			
		||||
            return deliveryData;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/settings/changeMail")
 | 
			
		||||
    public String changeMail(HttpSession session,
 | 
			
		||||
                             @RequestParam("email") String email,
 | 
			
		||||
 | 
			
		||||
@ -12,8 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@Controller
 | 
			
		||||
@ -27,22 +26,42 @@ public class CustomerOrderController {
 | 
			
		||||
 | 
			
		||||
    @GetMapping("")
 | 
			
		||||
    public String internCustomerOrder(Model model) {
 | 
			
		||||
        List<CustomerOrder> orders =  customerOrderRepository.getAllOrders();
 | 
			
		||||
        List<CustomerOrder> orders = customerOrderRepository.getAllOrders();
 | 
			
		||||
 | 
			
		||||
        Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().collect
 | 
			
		||||
                (Collectors.toMap
 | 
			
		||||
                (Function.identity(), c-> CreateDeliveryData.getDeliveryDataFromCustomerOrder(c,customerOrderRepository,restServiceForDelivery)));
 | 
			
		||||
        List<CustomerOrderDelivery> customerOrderDeliveryDataMap = orders
 | 
			
		||||
                .stream()
 | 
			
		||||
                .map(o -> new CustomerOrderDelivery(o, CreateDeliveryData.getDeliveryDataFromCustomerOrder(o, customerOrderRepository, restServiceForDelivery)))
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
        model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
 | 
			
		||||
 | 
			
		||||
        return "intern/customerOrders/index";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class CustomerOrderDelivery {
 | 
			
		||||
        private CustomerOrder customerOrder;
 | 
			
		||||
        private DeliveryData deliveryData;
 | 
			
		||||
 | 
			
		||||
        public CustomerOrderDelivery(CustomerOrder customerOrder, DeliveryData deliveryData) {
 | 
			
		||||
            this.customerOrder = customerOrder;
 | 
			
		||||
            this.deliveryData = deliveryData;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public CustomerOrder getCustomerOrder() {
 | 
			
		||||
            return customerOrder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public DeliveryData getDeliveryData() {
 | 
			
		||||
            return deliveryData;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/{id}")
 | 
			
		||||
    public String internCustomerOrdersId(Model model,
 | 
			
		||||
                                         @PathVariable("id") String id
 | 
			
		||||
    ) {
 | 
			
		||||
        CustomerOrder order = customerOrderRepository.findById(Long.parseLong(id)).get();
 | 
			
		||||
        DeliveryData deliveryData = CreateDeliveryData.getDeliveryDataFromCustomerOrder(order, customerOrderRepository,restServiceForDelivery);
 | 
			
		||||
        DeliveryData deliveryData = CreateDeliveryData.getDeliveryDataFromCustomerOrder(order, customerOrderRepository, restServiceForDelivery);
 | 
			
		||||
 | 
			
		||||
        model.addAttribute("order", order);
 | 
			
		||||
        model.addAttribute("deliveryData", deliveryData);
 | 
			
		||||
 | 
			
		||||
@ -41,14 +41,16 @@
 | 
			
		||||
                <th></th>
 | 
			
		||||
            </tr>
 | 
			
		||||
            <tr th:each="order: ${orderDeliveryDataMap}">
 | 
			
		||||
                <td><a th:href="@{/intern/customers/{id}(id=${order.getKey().customer.id})}" th:text="${order.getKey().customer.id}">101</a></td>
 | 
			
		||||
                <td th:text="${order.getKey().id}"></td>
 | 
			
		||||
                <td th:text="${order.getKey().created.toString().substring(0, 10)}"></td>
 | 
			
		||||
                <td th:text="${#numbers.formatDecimal(order.getKey().totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"></td>
 | 
			
		||||
                <td th:if="${order.getValue().allOk()}"><span th:text="${order.getValue().getStatus()}" /></td>
 | 
			
		||||
                <td th:if="${order.getValue().noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
 | 
			
		||||
                <td th:if="${order.getValue().noData()}">DHL-Server ist gerade nicht erreichbar</td>
 | 
			
		||||
                <td><a th:href="@{/intern/customerOrders/{id}(id=${order.getKey().id})}" class="button smaller">Details</a></td>
 | 
			
		||||
                <td><a th:href="@{/intern/customers/{id}(id=${order.customerOrder.customer.id})}"
 | 
			
		||||
                       th:text="${order.customerOrder.customer.id}">101</a></td>
 | 
			
		||||
                <td th:text="${order.customerOrder.id}"></td>
 | 
			
		||||
                <td th:text="${order.customerOrder.created.toString().substring(0, 10)}"></td>
 | 
			
		||||
                <td th:text="${#numbers.formatDecimal(order.customerOrder.totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"></td>
 | 
			
		||||
                <td th:if="${order.deliveryData.allOk()}"><span th:text="${order.deliveryData.getStatus()}"/></td>
 | 
			
		||||
                <td th:if="${order.deliveryData.noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
 | 
			
		||||
                <td th:if="${order.deliveryData.noData()}">DHL-Server ist gerade nicht erreichbar</td>
 | 
			
		||||
                <td><a th:href="@{/intern/customerOrders/{id}(id=${order.customerOrder.id})}" class="button smaller">Details</a>
 | 
			
		||||
                </td>
 | 
			
		||||
            </tr>
 | 
			
		||||
        </table>
 | 
			
		||||
        </p>
 | 
			
		||||
 | 
			
		||||
@ -29,23 +29,30 @@
 | 
			
		||||
                <table class="key-value">
 | 
			
		||||
                    <tr>
 | 
			
		||||
                        <th>Lieferstatus</th>
 | 
			
		||||
                        <td th:if="${order.getValue().allOk()}"><span th:text="${order.getValue().getStatus()}" /></td>
 | 
			
		||||
                        <td th:if="${order.getValue().noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
 | 
			
		||||
                        <td th:if="${order.getValue().noData()}">DHL-Server ist gerade nicht erreichbar</td>
 | 
			
		||||
                        <td th:if="${order.deliveryData.allOk()}"><span th:text="${order.deliveryData.getStatus()}"/>
 | 
			
		||||
                        </td>
 | 
			
		||||
                        <td th:if="${order.deliveryData.noTrackingID()}">Bestellung wurde elektronisch angekündigt</td>
 | 
			
		||||
                        <td th:if="${order.deliveryData.noData()}">DHL-Server ist gerade nicht erreichbar</td>
 | 
			
		||||
                    </tr>
 | 
			
		||||
                    <tr>
 | 
			
		||||
                        <td></td>
 | 
			
		||||
                        <td  th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null && !order.getValue().noData()}">Vorraussichtliche Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
 | 
			
		||||
                        <td  th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null && order.getValue().noData()}">Vorraussichtliche Ankunft: --:--:----</td>
 | 
			
		||||
                        <td  th:if="${order.getKey().deliveredAt != null && order.getKey().trackingId!=null}"><b>Angekommen</b>  Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
 | 
			
		||||
                        <td th:if="${order.customerOrder.deliveredAt == null && order.customerOrder.trackingId!=null && !order.deliveryData.noData()}">
 | 
			
		||||
                            Vorraussichtliche Ankunft: <span th:text="${order.deliveryData.getEstimatedArrival()}"/>
 | 
			
		||||
                        </td>
 | 
			
		||||
                        <td th:if="${order.customerOrder.deliveredAt == null && order.customerOrder.trackingId!=null && order.deliveryData.noData()}">
 | 
			
		||||
                            Vorraussichtliche Ankunft: --:--:----
 | 
			
		||||
                        </td>
 | 
			
		||||
                        <td th:if="${order.customerOrder.deliveredAt != null && order.customerOrder.trackingId!=null}">
 | 
			
		||||
                            <b>Angekommen</b> Ankunft: <span th:text="${order.deliveryData.getEstimatedArrival()}"/>
 | 
			
		||||
                        </td>
 | 
			
		||||
                    </tr>
 | 
			
		||||
                    <tr>
 | 
			
		||||
                        <th>Sendeverfolgungsnummer</th>
 | 
			
		||||
                        <td th:text="${order.getKey().trackingId!=null} ? ${order.getKey().trackingId} : 'Es wurde noch keine Sendungsnummer vergeben'"></td>
 | 
			
		||||
                        <td th:text="${order.customerOrder.trackingId!=null} ? ${order.customerOrder.trackingId} : 'Es wurde noch keine Sendungsnummer vergeben'"></td>
 | 
			
		||||
                    </tr>
 | 
			
		||||
                    <tr>
 | 
			
		||||
                        <th></th>
 | 
			
		||||
                        <td th:text="${order.getKey().destination.toString()}" />
 | 
			
		||||
                        <td th:text="${order.customerOrder.destination.toString()}"/>
 | 
			
		||||
                    </tr>
 | 
			
		||||
                </table>
 | 
			
		||||
            </div>
 | 
			
		||||
@ -56,11 +63,13 @@
 | 
			
		||||
                    <th>Menge</th>
 | 
			
		||||
                    <th>Preis (Brutto)</th>
 | 
			
		||||
                </tr>
 | 
			
		||||
                <tr th:each="position: ${order.getKey().positions}">
 | 
			
		||||
                    <td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}"><img th:src="@{/shop/articles/{id}/image.jpg(id=${position.article.id})}" class="s"/></a></td>
 | 
			
		||||
                    <td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}" th:text="${position.article.title}" class="s"></a></td>
 | 
			
		||||
                    <td th:text="${position.quantity}" />
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(position.getSumPrice() * 0.01, 1, 'POINT', 2, 'COMMA')}" />
 | 
			
		||||
                <tr th:each="position: ${order.customerOrder.positions}">
 | 
			
		||||
                    <td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}"><img
 | 
			
		||||
                            th:src="@{/shop/articles/{id}/image.jpg(id=${position.article.id})}" class="s"/></a></td>
 | 
			
		||||
                    <td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}"
 | 
			
		||||
                           th:text="${position.article.title}" class="s"></a></td>
 | 
			
		||||
                    <td th:text="${position.quantity}"/>
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(position.getSumPrice() * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
 | 
			
		||||
                </tr>
 | 
			
		||||
                <tr>
 | 
			
		||||
                    <th></th>
 | 
			
		||||
@ -72,13 +81,13 @@
 | 
			
		||||
                    <td></td>
 | 
			
		||||
                    <td></td>
 | 
			
		||||
                    <td>Artikel (Netto)</td>
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(order.getKey().totalNetCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(order.customerOrder.totalNetCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
 | 
			
		||||
                </tr>
 | 
			
		||||
                <tr>
 | 
			
		||||
                    <td></td>
 | 
			
		||||
                    <td></td>
 | 
			
		||||
                    <td>Umsatzsteuer</td>
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(order.getKey().totalVatCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
 | 
			
		||||
                    <td th:text="${#numbers.formatDecimal(order.customerOrder.totalVatCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
 | 
			
		||||
                </tr>
 | 
			
		||||
                <tr>
 | 
			
		||||
                    <td></td>
 | 
			
		||||
@ -87,7 +96,7 @@
 | 
			
		||||
                        <h3>Gesammtpreis</h3>
 | 
			
		||||
                    </td>
 | 
			
		||||
                    <td>
 | 
			
		||||
                        <h3 th:text="${#numbers.formatDecimal(order.getKey().totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
 | 
			
		||||
                        <h3 th:text="${#numbers.formatDecimal(order.customerOrder.totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
 | 
			
		||||
                    </td>
 | 
			
		||||
                </tr>
 | 
			
		||||
            </table>
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user