LabWork04 help me
This commit is contained in:
parent
6699eda085
commit
ee3075ed80
@ -19,9 +19,6 @@
|
||||
<td>
|
||||
<button class="btn btn-danger" @click="deleteCategory(categor.id)">Удалить</button>
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn btn-primary mr-2" @click="OpenModelForProducts();getProductsFromCategory(categor.id)">Просмотр продуктов</button>
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@ -98,9 +95,6 @@ export default {
|
||||
const addModal = document.getElementById('editModal');
|
||||
addModal.addEventListener('shown.bs.modal', function () {
|
||||
})
|
||||
const ModelForProducts = document.getElementById('ModelForProducts');
|
||||
addModal.addEventListener('shown.bs.modal', function () {
|
||||
})
|
||||
},
|
||||
|
||||
data() {
|
||||
@ -169,22 +163,6 @@ export default {
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
getProductsFromCategory(category_id){
|
||||
axios.get(this.URL + `group/${category_id}/products`)
|
||||
.then(response => {
|
||||
this.products = response.data;
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
OpenModelForProducts() {
|
||||
document.getElementById("ModelForProducts").style.display = "block";
|
||||
},
|
||||
closeModelForProducts() {
|
||||
document.getElementById("ModelForProducts").style.display = "none";
|
||||
},
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
@ -2,6 +2,7 @@
|
||||
<div class="container mt-4">
|
||||
<h1 class="text-center mb-4">Product Table</h1>
|
||||
<button class="btn btn-primary mr-2" @click="openModal('create')">Добавить</button>
|
||||
<button class="btn btn-primary mr-2" @click="openFiltrModel('create')">Фильтр</button>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -25,12 +26,54 @@
|
||||
<td>
|
||||
<button class="btn btn-danger" @click="deleteProduct(prdct.id)">Удалить</button>
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn btn-primary" @click="product = prdct; openManyToManyModal()">Производители</button>
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- Модальное окно для добавления и изменения-->
|
||||
<!-- Модальное окно для фильтра -->
|
||||
<div class="modal" tabindex="-1" id="filtrModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Filtr Products On Manufacturers</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название:</th>
|
||||
<th>Редактировать запись:</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="mnfs in filtr" :key="mnfs.id">
|
||||
<td>{{ mnfs.name }}</td>
|
||||
<td>
|
||||
<button class="btn btn-primary" type="button" @click="removeManufacturer(prod.id)">Удалить</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="input-group mb-3">
|
||||
<select class="form-select" v-model="manufacturerId">
|
||||
<option v-for="mnfctr in manufacturers" :key="mnfctr.id" :value="mnfctr.id">{{ mnfctr.name }}</option>
|
||||
</select>
|
||||
<button class="btn btn-outline-secondary" type="button" @click="addManufacturerFiltr()">Добавить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="editModal" @click="executeFiltr()">Применить</button>
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="editModal" @click="closeManufacturerFiltr()">Закрыть</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal" tabindex="-1" id="editModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
@ -69,6 +112,44 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal" tabindex="-1" id="manyToManyModal">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Add Manufacturers to Product</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название:</th>
|
||||
<th>Редактировать запись:</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="prod in productManufacturers" :key="prod.id">
|
||||
<td>{{ prod.name }}</td>
|
||||
<td>
|
||||
<button class="btn btn-primary" type="button" @click="removeManufacturer(prod.id)">Удалить</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="input-group mb-3">
|
||||
<select class="form-select" v-model="manufacturerId">
|
||||
<option v-for="mnfctr in manufacturers" :key="mnfctr.id" :value="mnfctr.id">{{ mnfctr.name }}</option>
|
||||
</select>
|
||||
<button class="btn btn-outline-secondary" type="button" @click="addManufacturer()">Добавить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="editModal" @click="closeManyToManyModal()">Закрыть</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import 'axios';
|
||||
@ -83,12 +164,19 @@ export default {
|
||||
const addModal = document.getElementById('editModal');
|
||||
addModal.addEventListener('shown.bs.modal', function () {
|
||||
})
|
||||
const addFiltrModal = document.getElementById('filtrModal');
|
||||
addFiltrModal.addEventListener('shown.bs.modal', function () {
|
||||
})
|
||||
},
|
||||
|
||||
data() {
|
||||
return{
|
||||
products: [],
|
||||
categories: [],
|
||||
manufacturers: [],
|
||||
productManufacturers: [],
|
||||
filtr: [],
|
||||
manufacturerId: 0,
|
||||
URL: "http://localhost:8080/",
|
||||
product: new Product(),
|
||||
editedProduct: new Product(),
|
||||
@ -120,9 +208,11 @@ export default {
|
||||
this.product.price = parseInt(this.product.price);
|
||||
console.log(this.product);
|
||||
axios.post(this.URL + "product", this.product)
|
||||
.then(() => {
|
||||
.then((response) => {
|
||||
this.getProducts();
|
||||
this.closeModal();
|
||||
this.product = response.data;
|
||||
this.openManyToManyModal();
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
@ -135,7 +225,8 @@ export default {
|
||||
})
|
||||
},
|
||||
async editProduct(product){
|
||||
await this.toBase64();
|
||||
if(product.photo === undefined) await this.toBase64();
|
||||
console.log(product);
|
||||
axios.put(this.URL + `product/${product.id}`, product)
|
||||
.then(() =>{
|
||||
const index = this.products.findIndex((s) => s.id === product.id);
|
||||
@ -169,7 +260,6 @@ export default {
|
||||
await new Promise((resolve, reject) => {
|
||||
reader.onload = function () {
|
||||
phototemp.photo = reader.result;
|
||||
console.log("phototemp: " + phototemp);
|
||||
resolve();
|
||||
};
|
||||
reader.onerror = function (error) {
|
||||
@ -178,6 +268,76 @@ export default {
|
||||
};
|
||||
});
|
||||
},
|
||||
openManyToManyModal(){
|
||||
this.getAllManufacturers();
|
||||
this.getProductManufacturers();
|
||||
document.getElementById("manyToManyModal").style.display = "block";
|
||||
},
|
||||
closeManyToManyModal() {
|
||||
document.getElementById("manyToManyModal").style.display = "none";
|
||||
},
|
||||
getAllManufacturers(){
|
||||
axios.get(this.URL + "manufacturer")
|
||||
.then(response => {
|
||||
this.manufacturers = response.data;
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
getProductManufacturers(){
|
||||
console.log(this.product.id);
|
||||
axios.get(this.URL + `product/${this.product.id}/manufacturers`)
|
||||
.then(response => {
|
||||
this.productManufacturers = response.data;
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
addManufacturer(){
|
||||
axios.post(this.URL + `product/${this.product.id}/Manufacturer/${this.manufacturerId}`)
|
||||
.then(() => {
|
||||
this.getProductManufacturers();
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
removeManufacturer(id){
|
||||
axios.delete(this.URL + `product/${this.product.id}/Manufacturer/${id}`)
|
||||
.then(() =>{
|
||||
this.getProductManufacturers();
|
||||
})
|
||||
},
|
||||
openFiltrModel() {
|
||||
this.getAllManufacturers();
|
||||
document.getElementById("filtrModal").style.display = "block";
|
||||
},
|
||||
addManufacturerFiltr() {
|
||||
const selectedManufacturer = this.manufacturers.find(mnfctr => mnfctr.id === this.manufacturerId);
|
||||
if (selectedManufacturer) {
|
||||
this.filtr.push(selectedManufacturer);
|
||||
console.log(this.filtr);
|
||||
}
|
||||
},
|
||||
closeManufacturerFiltr() {
|
||||
document.getElementById("filtrModal").style.display = "none";
|
||||
},
|
||||
executeFiltr() {
|
||||
console.log(this.filtr);
|
||||
const manufacturers = this.filtr;
|
||||
axios.post(this.URL + "product/by-manufacturers", manufacturers)
|
||||
.then(response => {
|
||||
this.products = response.data;
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
@ -3,6 +3,7 @@ package ru.ulstu.is.sbapp.HardwareShop.controller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.models.Product;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.services.ProductService;
|
||||
@ -34,6 +35,16 @@ public class ProductController {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/manufacturers")
|
||||
public List<ManufacturerDTO> getProductManufacturers(@PathVariable Long id){
|
||||
return productService.getProductManufacturers(id).stream().map(ManufacturerDTO::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/by-manufacturers")
|
||||
public List<Product> getProductsByManufacturers(@RequestBody List<Manufacturer> manufacturers) {
|
||||
return productService.getProductsByManufacturers(manufacturers);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ProductDTO createProduct(@RequestBody @Valid ProductDTO productDTO) throws IOException{
|
||||
return new ProductDTO(productService.addProduct(productDTO));
|
||||
@ -44,9 +55,14 @@ public class ProductController {
|
||||
return new ProductDTO(productService.updateProduct(id, productDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/{manufacturer}")
|
||||
public void addManufacturer(@PathVariable Long id, @PathVariable Long manufacturer_id) {
|
||||
productService.addManufacturersToProduct(id, manufacturer_id);
|
||||
@PostMapping("/{id}/Manufacturer/{manufacturerId}")
|
||||
public void addManufacturer(@PathVariable Long id, @PathVariable Long manufacturerId) {
|
||||
productService.addManufacturersToProduct(id, manufacturerId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/Manufacturer/{manufacturerId}")
|
||||
public void removeManufacturer(@PathVariable Long id, @PathVariable Long manufacturerId) {
|
||||
productService.removeManufacturersToProduct(id, manufacturerId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
@ -38,6 +38,8 @@ public class Manufacturer {
|
||||
|
||||
public Long getId() { return id;}
|
||||
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getName() { return name; }
|
||||
|
||||
public String getAddress() { return address; }
|
||||
|
@ -58,7 +58,7 @@ public class Product {
|
||||
|
||||
public String getName() { return name; }
|
||||
|
||||
public void setName() { this.name = name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public void setPrice(Integer price) { this.price = price; }
|
||||
|
||||
|
@ -2,12 +2,14 @@ package ru.ulstu.is.sbapp.HardwareShop.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer;
|
||||
import ru.ulstu.is.sbapp.HardwareShop.models.Product;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||
@Query("Select manufacturerList from Product where id =:id")
|
||||
List<Manufacturer> getManufacturers(Long id);
|
||||
@Query("SELECT p FROM Product p WHERE p.manufacturerList IN :manufacturers")
|
||||
List<Product> findByManufacturers(@Param("manufacturers") List<Manufacturer> manufacturers);
|
||||
}
|
||||
|
||||
|
@ -73,11 +73,18 @@ public class ProductService {
|
||||
return productRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> getProductsByManufacturers(List<Manufacturer> manufacturers) {
|
||||
return productRepository.findByManufacturers(manufacturers);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product updateProduct(Long id, ProductDTO productDTO) {
|
||||
final Product currentProduct = findProduct(id);
|
||||
currentProduct.setName(productDTO.getName());
|
||||
currentProduct.setPrice(productDTO.getPrice());
|
||||
/*currentProduct.setPhoto(productDTO.getPhoto().getBytes(StandardCharsets.UTF_8));*/
|
||||
currentProduct.setPhoto(productDTO.getPhoto().getBytes(StandardCharsets.UTF_8));
|
||||
currentProduct.setCategory(categoryService.findCategory(productDTO.getCategory_id()));
|
||||
validatorUtil.validate(currentProduct);
|
||||
return productRepository.save(currentProduct);
|
||||
}
|
||||
@ -121,8 +128,7 @@ public class ProductService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List getProductManufacturers(Long id){
|
||||
List manufacturers = productRepository.getManufacturers(id);
|
||||
return manufacturers;
|
||||
public List<Manufacturer> getProductManufacturers(Long id){
|
||||
return productRepository.findById(id).get().getManufacturerList();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user