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/controller/intern/customers/CustomersIndexController.java

119 lines
4.3 KiB
Java
Raw Normal View History

2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern.customers;
2020-04-28 22:41:29 +02:00
import org.hso.ecommerce.controller.intern.accounting.AccountingController;
import org.hso.ecommerce.controller.intern.accounting.AccountingController.ShortTemplateBookingResult;
2020-06-05 07:49:11 +02:00
import org.hso.ecommerce.entities.booking.Booking;
2020-06-05 12:30:51 +02:00
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.entities.user.User;
2020-06-05 07:49:11 +02:00
import org.hso.ecommerce.repos.booking.BookingRepository;
2020-06-05 12:30:51 +02:00
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
import org.hso.ecommerce.repos.user.UserRepository;
2020-06-05 07:49:11 +02:00
import org.springframework.beans.factory.annotation.Autowired;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller;
2020-06-05 12:30:51 +02:00
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
2020-04-28 22:41:29 +02:00
import javax.servlet.http.HttpServletRequest;
2020-06-05 12:30:51 +02:00
import javax.servlet.http.HttpServletResponse;
import java.util.List;
2020-06-05 12:30:51 +02:00
import java.util.Optional;
2020-06-03 20:26:36 +02:00
2020-04-28 22:41:29 +02:00
@Controller
2020-06-05 07:49:11 +02:00
@RequestMapping("/intern/customers")
2020-05-01 10:48:12 +02:00
public class CustomersIndexController {
2020-06-05 07:49:11 +02:00
@Autowired
2020-06-05 07:49:11 +02:00
private BookingRepository bookingRepository = null;
2020-06-05 07:49:11 +02:00
2020-06-05 07:49:11 +02:00
@Autowired
2020-06-05 12:30:51 +02:00
private final CustomerOrderRepository customerOrderRepository = null;
2020-06-07 13:38:01 +02:00
@Autowired
2020-06-10 20:10:12 +02:00
private final UserRepository userRepository = null;
2020-06-07 13:38:01 +02:00
@Autowired
private AccountingController accountingController = null;
2020-06-05 12:30:51 +02:00
@GetMapping("")
public String internCustomers(Model model) {
2020-06-10 18:45:52 +02:00
List<User> users = userRepository.findAll();
model.addAttribute("users", users);
2020-06-05 07:49:11 +02:00
2020-06-03 20:26:36 +02:00
return "intern/customers/index";
}
2020-06-05 07:49:11 +02:00
2020-06-05 12:30:51 +02:00
@GetMapping("/{id}")
public String internCustomersId(Model model,
@PathVariable("id") Long id,
HttpServletResponse response,
HttpServletRequest request
) {
Optional<User> optUser = userRepository.findById(id);
2020-06-10 18:45:52 +02:00
if (!optUser.isPresent()) {
2020-06-05 12:30:51 +02:00
request.setAttribute("error", "Der User wurde nicht gefunden.");
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return "error/404";
}
User user = optUser.get();
model.addAttribute("user", user);
List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId(id);
model.addAttribute("orders", orders);
2020-06-05 07:49:11 +02:00
2020-06-07 13:38:01 +02:00
List<Booking> bookings = bookingRepository.customerBookingsReverseChronologically(id);
ShortTemplateBookingResult result = accountingController.buildShortTemplate(
bookings,
account -> account.userAccount != null && account.userAccount.id == id);
model.addAttribute("balance", result.balance);
model.addAttribute("bookings", result.bookings);
2020-06-05 07:49:11 +02:00
2020-06-05 07:49:11 +02:00
return "intern/customers/id";
}
2020-06-05 07:49:11 +02:00
2020-06-05 12:30:51 +02:00
@PostMapping("/{id}/changeState")
public String changeState(@PathVariable("id") Long id,
2020-06-05 18:13:10 +02:00
@RequestParam(value = "active", required = false) String active,
2020-06-10 18:45:52 +02:00
@RequestParam(value = "ma", required = false) String ma
) {
2020-06-05 18:13:10 +02:00
User user = userRepository.findById(id).get();
2020-06-05 12:30:51 +02:00
2020-06-10 18:45:52 +02:00
if (active == null)
2020-06-05 18:13:10 +02:00
user.isActive = false;
else
user.isActive = true;
2020-06-05 12:30:51 +02:00
2020-06-10 18:45:52 +02:00
if (ma == null)
2020-06-05 18:13:10 +02:00
user.isEmployee = false;
else
user.isEmployee = true;
userRepository.save(user);
return "redirect:/intern/customers/" + id.toString();
2020-06-05 12:30:51 +02:00
}
@PostMapping("/{id}/resetPassword")
public String resetPassword(@PathVariable("id") Long id,
@RequestParam("password") String password,
2020-06-05 18:13:10 +02:00
@RequestParam("password2") String password2,
HttpServletRequest request
2020-06-10 18:45:52 +02:00
) {
if (!password.equals(password2)) {
2020-06-05 18:13:10 +02:00
request.setAttribute("error", "Passwörter stimmen nicht überein!");
return "intern/customers/id";
2020-06-05 18:13:10 +02:00
}
User user = userRepository.findById(id).get();
2020-06-10 18:45:52 +02:00
if (!user.validatePassword(password)) {
2020-06-05 18:13:10 +02:00
request.setAttribute("error", "Die Passwörter stimmen nicht mit dem Original überein!");
return "intern/customers/id";
2020-06-05 18:13:10 +02:00
}
user.setPassword("12345");
userRepository.save(user);
request.setAttribute("info", "Passwort wurde auf 12345 geändert!");
2020-06-05 12:30:51 +02:00
return "intern/customers/id";
2020-06-05 12:30:51 +02:00
}
2020-04-28 22:41:29 +02:00
}
2020-06-13 21:59:18 +02:00