-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
36 changes: 18 additions & 18 deletions
36
src/main/java/dev/alex96jvm/selmag/manager/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
package dev.alex96jvm.selmag.manager.controller; | ||
|
||
import dev.alex96jvm.selmag.manager.controller.payload.NewProductPayload; | ||
import dev.alex96jvm.selmag.manager.service.ProductService; | ||
import dev.alex96jvm.selmag.manager.controller.payload.UpdateProductPayload; | ||
import dev.alex96jvm.selmag.manager.entity.Product; | ||
import dev.alex96jvm.selmag.manager.service.ProductService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Controller | ||
@RequestMapping("catalogue/products/{productId:\\d+}") | ||
@RequiredArgsConstructor | ||
@RequestMapping("catalogue/products") | ||
public class ProductController { | ||
|
||
private final ProductService productService; | ||
|
||
@RequestMapping(value = "list", method = RequestMethod.GET) | ||
public String getProductList(Model model){ | ||
model.addAttribute("products", this.productService.findAllProducts()); | ||
return "catalogue/products/list"; | ||
@ModelAttribute("product") | ||
public Product product(@PathVariable("productId") int productId){ | ||
return this.productService.findProduct(productId).orElseThrow(); | ||
} | ||
|
||
@GetMapping("create") | ||
public String getNewProductPage(){ | ||
return "catalogue/products/new_product"; | ||
@GetMapping() | ||
public String getProduct(){ | ||
return "/catalogue/products/product"; | ||
} | ||
|
||
@PostMapping("create") | ||
public String createProduct(NewProductPayload payload){ | ||
Product product = this.productService.createProduct(payload.title(), payload.details()); | ||
return "redirect:/catalogue/products/%d".formatted(product.getId()); | ||
@GetMapping("edit") | ||
public String getProductEditPage(){ | ||
return "/catalogue/products/edit"; | ||
} | ||
|
||
@GetMapping("{productId:\\d+}") | ||
public String getProduct(@PathVariable("productId") int productId, Model model){ | ||
model.addAttribute("product", this.productService.findProduct(productId) | ||
.orElseThrow()); | ||
return "/catalogue/products/product"; | ||
@PostMapping("edit") | ||
public String updateProduct(@ModelAttribute("product") Product product, | ||
UpdateProductPayload payload){ | ||
this.productService.updateProduct(product.getId(), payload.title(), payload.details()); | ||
return "redirect:/catalogue/products/%d".formatted(product.getId()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/dev/alex96jvm/selmag/manager/controller/ProductsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.alex96jvm.selmag.manager.controller; | ||
|
||
import dev.alex96jvm.selmag.manager.controller.payload.NewProductPayload; | ||
import dev.alex96jvm.selmag.manager.service.ProductService; | ||
import dev.alex96jvm.selmag.manager.entity.Product; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("catalogue/products") | ||
public class ProductsController { | ||
|
||
private final ProductService productService; | ||
|
||
@RequestMapping(value = "list", method = RequestMethod.GET) | ||
public String getProductList(Model model){ | ||
model.addAttribute("products", this.productService.findAllProducts()); | ||
return "catalogue/products/list"; | ||
} | ||
|
||
@GetMapping("create") | ||
public String getNewProductPage(){ | ||
return "catalogue/products/new_product"; | ||
} | ||
|
||
@PostMapping("create") | ||
public String createProduct(NewProductPayload payload){ | ||
Product product = this.productService.createProduct(payload.title(), payload.details()); | ||
return "redirect:/catalogue/products/%d".formatted(product.getId()); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/dev/alex96jvm/selmag/manager/controller/payload/UpdateProductPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dev.alex96jvm.selmag.manager.controller.payload; | ||
|
||
public record UpdateProductPayload(String title, String details) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Редактирвование товара «[[${product.title}]]« – Онлайн-магазин</title> | ||
</head> | ||
<body> | ||
<a data-th-href="@{/catalogue/products/{productId}(productId=${product.id})}">← К товару</a> | ||
<h1>Редактирвование товара «[[${product.title}]]</h1> | ||
<form method="post" data-th-action="@{/catalogue/products/{productId}/edit(productId=${product.id})}"> | ||
<label> | ||
Название:<br> | ||
<input type="text" name="title" data-th-value="${product.title}"> | ||
</label><br> | ||
<label> | ||
Описание:<br> | ||
<textarea name="details" data-th-text="${product.details}"></textarea> | ||
</label><br> | ||
<button type="submit">Изменить</button> | ||
|
||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters