Merge remote-tracking branch 'origin/LabWork04' into LabWork04

# Conflicts:
#	src/main/java/ip/labwork/shop/controller/OrderController.java
#	src/main/java/ip/labwork/shop/controller/OrderDTO.java
#	src/main/java/ip/labwork/shop/controller/ProductController.java
#	src/main/java/ip/labwork/shop/controller/ProductDTO.java
#	src/main/java/ip/labwork/shop/service/OrderService.java
#	src/main/java/ip/labwork/shop/service/ProductService.java
This commit is contained in:
Nikita Sergeev 2023-04-09 14:06:36 +04:00
commit 5d790b350c
5 changed files with 128 additions and 78 deletions

View File

@ -27,26 +27,38 @@
</ul> </ul>
</div> </div>
</nav> </nav>
<form id="form"> <form id="form" novalidate>
<div class="row mt-3"> <div class="row mt-3">
<div class="col-sm-6"> <div class="col-sm-4">
<label for="componentName" class="form-label">Название компонента</label> <label for="orderId" class="form-label">ИД Заказа</label>
<input type="text" class="form-control" id="componentName" required> <input type="text" class="form-control" id="orderId" required>
</div> </div>
<div class="col-sm-6"> <div class="col-sm-4">
<label for="firstName" class="form-label">First name</label> <label for="orderDate" class="form-label">Время оформления</label>
<input type="text" class="form-control" id="firstName" required> <input type="text" class="form-control" id="orderDate" required>
</div>
<div class="col-sm-4">
<label for="orderPrice" class="form-label">Цена</label>
<input type="text" class="form-control" id="orderPrice" required>
</div>
<div class="col-sm-4">
<label for="productId" class="form-label">ИД продукта</label>
<input type="text" class="form-control" id="productId" required>
</div>
<div class="col-sm-4">
<label for="productCount" class="form-label">Количество продукта</label>
<input type="text" class="form-control" id="productCount" required>
</div> </div>
</div> </div>
<div class="row mt-3"> <div class="row mt-3">
<div class="d-grid col-sm-4 mx-auto"> <div class="d-grid col-sm-4 mx-auto">
<button type="submit" class="btn btn-success">Add</button> <button type="submit" class="btn btn-success">Добавить</button>
</div> </div>
<div class="d-grid col-sm-4 mx-auto mt-3 mt-sm-0"> <div class="d-grid col-sm-4 mx-auto">
<button id="testError" type="button" class="btn btn-danger">Test</button> <button type="submit" class="btn btn-success" id="btnUpdate" >Обновить</button>
</div> </div>
<div class="d-grid col-sm-4 mx-auto mt-3 mt-sm-0"> <div class="d-grid col-sm-4 mx-auto">
<button id="testNormal" type="button" class="btn btn-secondary">Test</button> <button id="btnRemove" class="btn btn-success">Удалить</button>
</div> </div>
</div> </div>
</form> </form>
@ -54,9 +66,10 @@
<table class="table mt-3"> <table class="table mt-3">
<thead> <thead>
<tr> <tr>
<th scope="col">#</th> <th scope="col">ИД</th>
<th scope="col">First name</th> <th scope="col">Дата оформления</th>
<th scope="col">Last name</th> <th scope="col">Цена</th>
<th scope="col">Продукты</th>
</tr> </tr>
</thead> </thead>
<tbody id="tbody"> <tbody id="tbody">

View File

@ -36,7 +36,7 @@ window.addEventListener('DOMContentLoaded', function () {
const remove = async function (){ const remove = async function (){
console.info('Try to remove item'); console.info('Try to remove item');
if (itemId.value !== 0) { if (componentIdInput.value !== 0) {
if (!confirm('Do you really want to remove this item?')) { if (!confirm('Do you really want to remove this item?')) {
console.info('Canceled'); console.info('Canceled');
return; return;
@ -48,7 +48,7 @@ window.addEventListener('DOMContentLoaded', function () {
"Content-Type": "application/json", "Content-Type": "application/json",
} }
}; };
const response = await fetch(host + `/component/` + itemId.value, requestParams); const response = await fetch(host + `/component/` + componentIdInput.value, requestParams);
return await response.json(); return await response.json();
} }

View File

@ -4,72 +4,110 @@ window.addEventListener('DOMContentLoaded', function () {
const host = "http://localhost:8080"; const host = "http://localhost:8080";
const table = document.getElementById("tbody"); const table = document.getElementById("tbody");
const form = document.getElementById("form"); const form = document.getElementById("form");
const lastNameInput = document.getElementById("componentName"); const orderIdInput = document.getElementById("orderId");
const firstNameInput = document.getElementById("firstName"); const orderDate = document.getElementById("orderDate");
const testErrorBtn = document.getElementById("testError"); const priceInput = document.getElementById("orderPrice");
const testNormalBtn = document.getElementById("testNormal"); const productIdInput = document.getElementById("productId");
const productCountInput = document.getElementById("productCount");
const buttonRemove = document.getElementById("btnRemove");
const buttonUpdate = document.getElementById("btnUpdate");
const getData = async function () { const getData = async function () {
table.innerHTML = ""; table.innerHTML = "";
const response = await fetch(host + "/student"); const response = await fetch(host + "/order");
const data = await response.json(); const data = await response.json();
data.forEach(student => { data.forEach(Order => {
let temp = "<select>";
Order.productDTOList.forEach(Product => {
temp += `<option>${Product.productName + " " + Product.count}</option>>`
})
temp += "</select>"
table.innerHTML += table.innerHTML +=
`<tr> `<tr>
<th scope="row">${student.id}</th> <th scope="row">${Order.id}</th>
<td>${student.firstName}</td> <td>${Order.date}</td>
<td>${student.lastName}</td> <td>${Order.price}</td>
<td>${temp}</td>
</tr>`; </tr>`;
}) })
} }
const create = async function (firstName, lastName) { const create = async function () {
const requestParams = { const requestParams = {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
} }
}; };
const response = await fetch(host + `/student?firstName=${firstName}&lastName=${lastName}`, requestParams); let temp = Date.now();
let time = new Date(temp);
let tru = time.toString();
const response = await fetch(host + `/order?price=${priceInput.value}&date=${tru}&count=${productCountInput.value}&prod=${productIdInput.value}`, requestParams);
return await response.json(); return await response.json();
} }
const test = async function (testObject) { const remove = async function (){
console.info('Try to remove item');
if (orderIdInput.value !== 0) {
if (!confirm('Do you really want to remove this item?')) {
console.info('Canceled');
return;
}
}
const requestParams = { const requestParams = {
method: "POST", method: "DELETE",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, }
body: JSON.stringify(testObject),
}; };
const response = await fetch(host + "/test", requestParams); const response = await fetch(host + `/order/` + orderIdInput.value, requestParams);
if (response.status === 200) { return await response.json();
const data = await response.json();
alert(`TestDto=[id=${data.id}, name=${data.name}, data=${data.data}]`);
} }
if (response.status === 400) {
const data = await response.text(); const update = async function (){
alert(data); console.info('Try to update item');
if (orderIdInput.value === 0 || orderDate.value == null || orderPrice.value === 0 || productIdInput.value === 0 || productCountInput.value === 0) {
return;
} }
const requestParams = {
method: "PUT",
headers: {
"Content-Type": "application/json",
} }
};
let temp = Date.now();
let time = new Date(temp);
let tru = time.toString();
const response = await fetch(host + `/order/${orderIdInput.value}?price=${priceInput.value}&date=${tru}&count=${productCountInput.value}&prod=${productIdInput.value}`, requestParams);
return await response.json();
}
buttonRemove.addEventListener('click', function (event){
event.preventDefault();
remove().then((result) => {
getData()
orderIdInput.value = "";
});
});
buttonUpdate.addEventListener('click', function (event){
event.preventDefault();
update().then((result) => {
getData()
orderIdInput.value = "";
priceInput.value = "";
});
});
form.addEventListener("submit", function (event) { form.addEventListener("submit", function (event) {
event.preventDefault(); event.preventDefault();
create(firstNameInput.value, lastNameInput.value).then((result) => { create().then((result) => {
getData(); getData();
firstNameInput.value = ""; priceInput.value = "";
lastNameInput.value = ""; orderIdInput.value = "";
alert(`Student[id=${result.id}, firstName=${result.firstName}, lastName=${result.lastName}]`); alert(`Component[id=${result.id}, price=${result.price}, componentName=${result.date}]`);
}); });
}); });
testErrorBtn.addEventListener("click", function () {
test({});
});
testNormalBtn.addEventListener("click", function () {
test({id: 10, name: "test"});
});
getData(); getData();
}); });

View File

@ -14,7 +14,7 @@ public class Order {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@NotNull(message = "Date can't be null or empty") //@NotNull(message = "Date can't be null or empty")
@Column(name = "date") @Column(name = "date")
private Date date; private Date date;
@NotNull(message = "Price can't be null or empty") @NotNull(message = "Price can't be null or empty")

View File

@ -27,7 +27,6 @@ public class OrderProducts {
public OrderProducts(Order order, Product product, Integer count) { public OrderProducts(Order order, Product product, Integer count) {
this.order = order; this.order = order;
this.id = new OrderProductsKey(product.getId(), order.getId());
this.id.setOrderId(order.getId()); this.id.setOrderId(order.getId());
this.id.setProductId(product.getId()); this.id.setProductId(product.getId());
this.product = product; this.product = product;