Skip to content

Commit

Permalink
Add editing of product
Browse files Browse the repository at this point in the history
  • Loading branch information
alex96jvm committed Aug 29, 2024
1 parent af531c1 commit f20f48b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
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());
}
}
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());
}

}
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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Service
Expand All @@ -27,4 +28,15 @@ public Product createProduct(String title, String details) {
public Optional<Product> findProduct(int productId) {
return this.productRepository.findById(productId);
}

@Override
public void updateProduct(Integer id, String title, String details) {
this.productRepository.findById(id)
.ifPresentOrElse(product -> {
product.setTitle(title);
product.setDetails(details);
}, () -> {
throw new NoSuchElementException();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ProductService {
Product createProduct(String title, String details);

Optional<Product> findProduct(int productId);

void updateProduct(Integer id, String title, String details);
}
23 changes: 23 additions & 0 deletions src/main/resources/templates/catalogue/products/edit.html
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>Редактирвование товара &laquo;[[${product.title}]]&laquo; &ndash; Онлайн-магазин</title>
</head>
<body>
<a data-th-href="@{/catalogue/products/{productId}(productId=${product.id})}">&larr; К товару</a>
<h1>Редактирвование товара &laquo;[[${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>
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ <h1 data-th-text="${product.title}"></h1>
<li><strong>Название</strong>: <span data-th-text="${product.title}"></span></li>
<li><strong>Описание</strong>: <span data-th-text="${product.details}"></span></li>
</ul>
<a data-th-href="@{/catalogue/products/{productId}/edit(productId=${product.id})}">Изменить</a>
</body>
</html>

0 comments on commit f20f48b

Please sign in to comment.