diff --git a/src/main/java/np/something/DTO/CustomerDto.java b/src/main/java/np/something/DTO/CustomerDto.java index 5fb7b6b..0ad8ebf 100644 --- a/src/main/java/np/something/DTO/CustomerDto.java +++ b/src/main/java/np/something/DTO/CustomerDto.java @@ -34,10 +34,18 @@ public class CustomerDto { return username; } + public void setUsername(String username) { + this.username = username; + } + public String getPassword() { return password; } + public void setPassword(String password) { + this.password = password; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public List getComments() { return comments; diff --git a/src/main/java/np/something/mvc/Customers.java b/src/main/java/np/something/mvc/Customers.java new file mode 100644 index 0000000..9821279 --- /dev/null +++ b/src/main/java/np/something/mvc/Customers.java @@ -0,0 +1,61 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import jakarta.validation.Valid; +import np.something.DTO.CustomerDto; +import np.something.services.CustomerService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/customers") +public class Customers { + private final CustomerService customerService; + + public Customers(CustomerService customerService) { + this.customerService = customerService; + } + + @GetMapping(value = { "/", "/{id}" }) + public String getCustomers(@PathVariable(required = false) Long id, HttpServletRequest request, HttpSession session, Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + if (id == null || id <= 0) { + model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); + } else { + model.addAttribute("customers", new CustomerDto[] { new CustomerDto(customerService.findCustomer(id)) }); + } + + return "customers"; + } + + @PostMapping("/delete/{id}") + public String deleteCustomer(@PathVariable Long id) { + customerService.deleteCustomer(id); + return "redirect:/customers/"; + } + + @PostMapping(value = { "/", "/{id}"}) + public String manipulateCustomer(@PathVariable(required = false) Long id, @ModelAttribute @Valid CustomerDto customerDto, + HttpServletRequest request, HttpSession session, + BindingResult bindingResult, + Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "/customers"; + } + + if (id == null || id <= 0) { + customerService.addCustomer(customerDto.username, customerDto.password); + } else { + customerService.updateCustomer(id, customerDto.username, customerDto.password); + } + + return "redirect:/customers/"; + } +} diff --git a/src/main/java/np/something/mvc/Session.java b/src/main/java/np/something/mvc/Session.java new file mode 100644 index 0000000..bd7fd14 --- /dev/null +++ b/src/main/java/np/something/mvc/Session.java @@ -0,0 +1,16 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpSession; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class Session { + @PostMapping("/update-session") + public ResponseEntity updateSession(@RequestParam("currentCustomerId") int currentCustomerId, HttpSession session) { + session.setAttribute("currentCustomerId", currentCustomerId); + return ResponseEntity.ok().build(); + } +} \ No newline at end of file diff --git a/src/main/resources/templates/customers.html b/src/main/resources/templates/customers.html new file mode 100644 index 0000000..f022039 --- /dev/null +++ b/src/main/resources/templates/customers.html @@ -0,0 +1,104 @@ + + + + + +
+
+
+
+

Профили

+
+
+
+
+ +
+
+
+ +

Список профилей

+
+
+
+
+
+
+
+
+

Комментарии:

+
+
+
+
+
+
+
+

Нет комментариев

+

Посты:

+
+
+
+
+
+
+
+
+

Нет постов

+
+
+ +
+ +
+
+
+
+
+
+ + + + + +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..a9ca43b --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,41 @@ + + + + + + + + + Социальная сеть + + + +
+ +
+ +
+
+ + + + + \ No newline at end of file