This commit is contained in:
1yuee 2023-06-19 23:27:36 +04:00
parent fcf22c2a4d
commit 048a00a4a4
9 changed files with 123 additions and 5 deletions

BIN
data.mv.db Normal file

Binary file not shown.

View File

@ -53,7 +53,6 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling().authenticationEntryPoint(delegatingEntryPoint());
http.headers().frameOptions().sameOrigin().and()
.cors().and()

View File

@ -29,7 +29,9 @@ public class ShopDto {
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public List<ProductDto> getProducts() {
return Products;
}

View File

@ -0,0 +1,57 @@
package com.webproglabs.lab1.lab34.mvc;
import com.webproglabs.lab1.lab34.dto.ShopDto;
import com.webproglabs.lab1.lab34.model.Shop;
import com.webproglabs.lab1.lab34.model.enums.UserRole;
import com.webproglabs.lab1.lab34.services.ShopService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/shops")
public class ShopMvcController {
private final ShopService shopService;
public ShopMvcController(ShopService shopService) {
this.shopService = shopService;
}
@GetMapping
@Secured({UserRole.AsString.ADMIN})
public String getShops(Model model) {
model.addAttribute("shops", shopService.findAllShops().stream().map(ShopDto::new).toList());
model.addAttribute("shopDto", new ShopDto());
return "shops";
}
@GetMapping(value = {"/edit/{Id}"})
@Secured({UserRole.AsString.ADMIN})
public String getShopEdit(@PathVariable Long Id, Model model) {
model.addAttribute("shop", shopService.findShopById(Id));
model.addAttribute("shopDto", new ShopDto());
return "shopEdit";
}
@PostMapping(value = {"/edit/{Id}"})
@Secured({UserRole.AsString.ADMIN})
public String editShop(@PathVariable Long Id, @ModelAttribute ShopDto shopDto) {
shopService.updateShop(Id, shopDto.getName());
return "redirect:/shops";
}
@PostMapping(value = {"/create"})
@Secured({UserRole.AsString.ADMIN})
public String createShop(@ModelAttribute ShopDto shopDto) {
shopService.addShop(shopDto.getName());
return "redirect:/shops";
}
@PostMapping(value = {"/delete/{Id}"})
@Secured({UserRole.AsString.ADMIN})
public String deleteShop(@PathVariable Long Id) {
shopService.deleteShop(Id);
return "redirect:/shops";
}
}

View File

@ -22,8 +22,6 @@
</div>
<div>
<p class='h4 text-center'>
<a sec:authorize="hasRole('ROLE_ADMIN')" href="/profiles" class="text-decoration-none m-3">Профили</a>
<a sec:authorize="isAuthenticated()" href="/feed/" class="text-decoration-none m-3">Лента</a>
<a sec:authorize="isAuthenticated()" href="/logout" class="text-decoration-none m-3">
Выход
</a>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<body>
<div class="container" layout:fragment="content">
<div class="alert alert-danger">
<span th:text="${error}"></span>
</div>
<a href="/">На главную</a>
</div>
</body>
</html>

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}"
xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div class="container container-padding" layout:fragment="content">
<div>Name</div>
<div th:text="${shop.name}"></div>
<form action="#" th:action="@{/shops/edit/{id} (id=${shop.id})}" th:object="${shopDto}" method="post">
<p>New Name:</p>
<input th:field="${shopDto.name}" type="text" class="mb-2 form-control" required="true" />
<button type="submit" class="" >Edit</button>
</form>
<div th:each="product: ${shop.products}">
<p th:text="${product.name}"></p>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}"
xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div class="container container-padding" layout:fragment="content">
<div th:each="shop: ${shops}">
<div th:text="${'Name: ' + shop.name}" class="m-3"></div>
<form action="#" th:action="@{/shops/delete/{id} (id=${shop.id})}" method="post">
<button type="submit" class="" >Delete</button>
</form>
<form action="#" th:action="@{/shops/edit/{id} (id=${shop.id})}" method="get">
<button type="submit" class="">Edit</button>
</form>
</div>
<form action="#" th:action="@{/shops/create}" th:object="${shopDto}" method="post">
<p>Name:</p>
<input th:field="${shopDto.name}" type="text" class="mb-2 form-control" required="true" />
<button type="submit" class="" >Add</button>
</form>
</div>
</body>
</html>

View File

@ -1,7 +1,8 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
layout:decorate="~{default}"
>
<body>
<div class="container container-padding" layout:fragment="content">
<div th:if="${errors}" th:text="${errors}" class="margin-bottom alert alert-danger"></div>