Compare commits

...

1 Commits
main ... lab4_5

Author SHA1 Message Date
a.puchkina
0c25c297f7 статистика! 2024-06-07 00:37:51 +04:00
34 changed files with 65 additions and 89 deletions

View File

@ -13,6 +13,17 @@
<div class = "container"> <div class = "container">
<h2>Товары</h2> <h2>Товары</h2>
<ul class="nav nav-underline justify-content-center" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="pill" href="#products">Позиции</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#stats">Статистика</a>
</li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane container active" id="products">
<div class="d-flex align-items-center justify-content-between"> <div class="d-flex align-items-center justify-content-between">
<a href="/admin/product/edit/" class="btn btn-secondary">Добавить товар</a> <a href="/admin/product/edit/" class="btn btn-secondary">Добавить товар</a>
<form class="search-form" th:action="@{/admin/product}" method="get" style="display: flex;"> <form class="search-form" th:action="@{/admin/product}" method="get" style="display: flex;">
@ -62,9 +73,14 @@
currentPage=${currentPage}) }" /> currentPage=${currentPage}) }" />
</th:block> </th:block>
</div> </div>
<!-- <div class="tab-pane container fade" id="stats"> <div class="tab-pane container fade" id="stats">
<ul class="list-group mb-2">
</div> --> <li th:each="stat : ${stats}" class="list-group-item">
<strong>[[${stat.typeName}]]</strong>:
[[${#numbers.formatDecimal(stat.totalPrice, 1, 2)}]] &#8381;
</li>
</ul>
</div>
</div> </div>
</div> </div>
</th:block> </th:block>
@ -73,35 +89,3 @@
</body> </body>
</html> </html>
<!-- <ul class="nav nav-underline justify-content-center" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="pill" href="#products">Позиции</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#stats">Статистика</a>
</li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane container active" id="products"> -->

Binary file not shown.

View File

@ -18,6 +18,7 @@ import com.example.demo.core.api.PageAttributesMapper;
import com.example.demo.core.configuration.Constants; import com.example.demo.core.configuration.Constants;
import com.example.demo.core.security.UserPrincipal; import com.example.demo.core.security.UserPrincipal;
import com.example.demo.products.model.ProductEntity; import com.example.demo.products.model.ProductEntity;
import com.example.demo.products.model.ProductGrouped;
//import com.example.demo.products.model.ProductGrouped; //import com.example.demo.products.model.ProductGrouped;
import com.example.demo.products.service.ProductService; import com.example.demo.products.service.ProductService;
import com.example.demo.types.api.TypeDto; import com.example.demo.types.api.TypeDto;
@ -34,6 +35,7 @@ public class ProductController {
public static final String PRODUCT_EDIT_VIEW = "product-edit"; public static final String PRODUCT_EDIT_VIEW = "product-edit";
public static final String PRODUCT_ATTRIBUTE = "product"; public static final String PRODUCT_ATTRIBUTE = "product";
private static final String PAGE_ATTRIBUTE = "page"; private static final String PAGE_ATTRIBUTE = "page";
private static final String TYPEID_ATTRIBUTE = "typeId";
private final ProductService productService; private final ProductService productService;
private final TypeService typeService; private final TypeService typeService;
@ -50,13 +52,13 @@ public class ProductController {
return modelMapper.map(entity, ProductDto.class); return modelMapper.map(entity, ProductDto.class);
} }
/*private ProductGroupedDto toGroupedDto(ProductGrouped entity) { private ProductGroupedDto toGroupedDto(ProductGrouped entity) {
return modelMapper.map(entity, ProductGroupedDto.class); return modelMapper.map(entity, ProductGroupedDto.class);
}*/ }
private ExpandedProductDto toExpandedDto(ProductEntity entity, Long userId) { private ExpandedProductDto toExpandedDto(ProductEntity entity, Long userId) {
ExpandedProductDto dto = modelMapper.map(entity, ExpandedProductDto.class); ExpandedProductDto dto = modelMapper.map(entity, ExpandedProductDto.class);
dto.setTypeName(typeService.get(dto.getTypeId()).getName()); dto.setTypeName(typeService.get(dto.getTypeName()).getName());
return dto; return dto;
} }
@ -66,7 +68,7 @@ public class ProductController {
private ProductEntity toEntity(ProductDto dto) { private ProductEntity toEntity(ProductDto dto) {
final ProductEntity entity = modelMapper.map(dto, ProductEntity.class); final ProductEntity entity = modelMapper.map(dto, ProductEntity.class);
entity.setType(typeService.get(dto.getTypeId())); entity.setType(typeService.get(dto.getTypeName()));
return entity; return entity;
} }
@ -74,7 +76,7 @@ public class ProductController {
public String getAll( public String getAll(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name = "query", required = false) String query, @RequestParam(name = "query", required = false) String query,
//@RequestParam(name = TYPEID_ATTRIBUTE, defaultValue = "0") int typeId, @RequestParam(name = TYPEID_ATTRIBUTE, defaultValue = "0") int typeId,
Model model, Model model,
@AuthenticationPrincipal UserPrincipal principal) { @AuthenticationPrincipal UserPrincipal principal) {
@ -92,13 +94,13 @@ public class ProductController {
model.addAllAttributes(attributes); model.addAllAttributes(attributes);
} }
/*model.addAllAttributes(PageAttributesMapper.toAttributes( model.addAllAttributes(PageAttributesMapper.toAttributes(
productService.getAll(typeId, page, Constants.DEFUALT_PAGE_SIZE), productService.getAll(typeId, page, Constants.DEFUALT_PAGE_SIZE),
this::toDto)); this::toDto));
model.addAttribute("stats", model.addAttribute("stats",
productService.getTotal().stream() productService.getTotal().stream()
.map(this::toGroupedDto) .map(this::toGroupedDto)
.toList()); */ .toList());
model.addAttribute("query", query); model.addAttribute("query", query);

View File

@ -14,7 +14,7 @@ public class ProductDto {
@NotNull @NotNull
@Min(1) @Min(1)
private Long typeId; private String typeName;
@NotNull @NotNull
@Min(1) @Min(1)
@ -43,12 +43,12 @@ public class ProductDto {
this.name = name; this.name = name;
} }
public Long getTypeId() { public String getTypeName() {
return typeId; return typeName;
} }
public void setTypeId(Long typeId) { public void setTypeName(String typeName) {
this.typeId = typeId; this.typeName = typeName;
} }
public Double getPrice() { public Double getPrice() {

View File

@ -1,15 +1,15 @@
package com.example.demo.products.api; package com.example.demo.products.api;
public class ProductGroupedDto { public class ProductGroupedDto {
private Long typeId; private String typeName;
private Long totalPrice; private Long totalPrice;
public Long getTypeId() { public String getTypeName() {
return typeId; return typeName;
} }
public void setTypeId(Long typeId) { public void setTypeName(String typeName) {
this.typeId = typeId; this.typeName = typeName;
} }
public Long getTotalPrice() { public Long getTotalPrice() {

View File

@ -49,6 +49,12 @@ public class TypeService {
.orElseThrow(() -> new NotFoundException(TypeEntity.class, id)); .orElseThrow(() -> new NotFoundException(TypeEntity.class, id));
} }
@Transactional(readOnly = true)
public TypeEntity get(String name) {
return repository.findByNameIgnoreCase(name)
.orElseThrow(() -> new IllegalArgumentException("Invalid typeName"));
}
@Transactional @Transactional
public TypeEntity create(TypeEntity entity) { public TypeEntity create(TypeEntity entity) {
if (entity == null) { if (entity == null) {

View File

@ -13,6 +13,17 @@
<div class = "container"> <div class = "container">
<h2>Товары</h2> <h2>Товары</h2>
<ul class="nav nav-underline justify-content-center" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="pill" href="#products">Позиции</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#stats">Статистика</a>
</li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane container active" id="products">
<div class="d-flex align-items-center justify-content-between"> <div class="d-flex align-items-center justify-content-between">
<a href="/admin/product/edit/" class="btn btn-secondary">Добавить товар</a> <a href="/admin/product/edit/" class="btn btn-secondary">Добавить товар</a>
<form class="search-form" th:action="@{/admin/product}" method="get" style="display: flex;"> <form class="search-form" th:action="@{/admin/product}" method="get" style="display: flex;">
@ -62,9 +73,14 @@
currentPage=${currentPage}) }" /> currentPage=${currentPage}) }" />
</th:block> </th:block>
</div> </div>
<!-- <div class="tab-pane container fade" id="stats"> <div class="tab-pane container fade" id="stats">
<ul class="list-group mb-2">
</div> --> <li th:each="stat : ${stats}" class="list-group-item">
<strong>[[${stat.typeName}]]</strong>:
[[${#numbers.formatDecimal(stat.totalPrice, 1, 2)}]] &#8381;
</li>
</ul>
</div>
</div> </div>
</div> </div>
</th:block> </th:block>
@ -73,35 +89,3 @@
</body> </body>
</html> </html>
<!-- <ul class="nav nav-underline justify-content-center" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="pill" href="#products">Позиции</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="pill" href="#stats">Статистика</a>
</li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane container active" id="products"> -->