Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
522cf1d2e8 | |||
30120ca18d | |||
5e26c1aa1e | |||
e007f51d04 | |||
1faa719f92 | |||
|
5c09ff94f8 | ||
|
e64d4e6434 | ||
|
019bcc6411 | ||
a73685f3b3 | |||
e620af3165 | |||
13a162f744 | |||
6f91e433fa | |||
|
7eff23080c | ||
|
951bf3001f | ||
|
a15ed7c52b | ||
|
edbbe4d2b1 | ||
|
7330e6e550 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,6 +4,8 @@ build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
data.mv.db
|
||||
data.trace.db
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
|
18
build.gradle
18
build.gradle
@ -1,6 +1,6 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.0.2'
|
||||
id 'org.springframework.boot' version '2.6.3'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
}
|
||||
|
||||
@ -14,6 +14,22 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||
implementation 'org.springframework.boot:spring-boot-devtools'
|
||||
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
|
||||
|
||||
implementation 'org.webjars:bootstrap:5.1.3'
|
||||
implementation 'org.webjars:jquery:3.6.0'
|
||||
implementation 'org.webjars:font-awesome:6.1.0'
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.1.210'
|
||||
|
||||
implementation 'org.hibernate.validator:hibernate-validator'
|
||||
|
||||
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
|
||||
implementation 'org.jetbrains:annotations:24.0.0'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
@ -1,49 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
||||
<title>Тестовое приложение</title>
|
||||
<link href="/node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body style="padding: 10px;">
|
||||
<h2>Конвертр текста!</h2>
|
||||
<form class="needs-validation" novalidate>
|
||||
<div class="mb-3">
|
||||
<label for="input" class="form-label">Ваш текст</label>
|
||||
<input type="text" class="form-control" id="input" required>
|
||||
<div class="invalid-feedback">Пожалуйста, введите текст</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="radio" id="radioUppercase" checked>
|
||||
<label class="form-check-label" for="radioUppercase">Undercase</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="radio" id="radioLowcase">
|
||||
<label class="form-check-label" for="radioLowcase">Lowcase</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="radio" id="radioSplit">
|
||||
<label class="form-check-label" for="radioSplit">Split</label>
|
||||
<input type="text" class="form-control" id="inputSplitBy">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="radio" id="radioReplace">
|
||||
<label class="form-check-label" for="radioReplace">Replace</label>
|
||||
<input type="text" class="form-control" id="inputReplaceOld" maxlength="1">
|
||||
<input type="text" class="form-control" id="inputReplaceNew" maxlength="1">
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Обработать</button>
|
||||
</form>
|
||||
<h2>Результат:</h2>
|
||||
<p id="result"></p>
|
||||
</body>
|
||||
<script src="index.js"></script>
|
||||
</html>
|
@ -1,41 +0,0 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var forms = document.querySelectorAll('.needs-validation');
|
||||
|
||||
Array.from(forms).forEach(form => {
|
||||
form.addEventListener('submit', event => {
|
||||
if (!form.checkValidity()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
form.classList.add('was-validated');
|
||||
}, false);
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementsByClassName("needs-validation")[0].addEventListener('submit', async function(event) {
|
||||
event.preventDefault();
|
||||
url = ""
|
||||
if (document.getElementById("radioUppercase").checked) {
|
||||
url = "http://localhost:8080/uppercase?value=" + document.getElementById("input").value
|
||||
}
|
||||
if (document.getElementById("radioLowcase").checked) {
|
||||
url = "http://localhost:8080/lowercase?value=" + document.getElementById("input").value
|
||||
}
|
||||
if (document.getElementById("radioSplit").checked) {
|
||||
url = "http://localhost:8080/split?value=" + document.getElementById("input").value + "&splitBy=" + document.getElementById("inputSplitBy").value
|
||||
}
|
||||
if (document.getElementById("radioReplace").checked) {
|
||||
url = "http://localhost:8080/replace?value=" + document.getElementById("input").value + "&old=" + document.getElementById("inputReplaceOld").value + "&new=" + document.getElementById("inputReplaceNew").value
|
||||
}
|
||||
if (url == "") {
|
||||
document.getElementById("result").innerHTML = "Выберите действие!"
|
||||
}
|
||||
// performFetch(url).then(data => {document.getElementById("result").innerHTML = data})
|
||||
document.getElementById("result").innerHTML = await performFetch(url)
|
||||
});
|
||||
|
||||
async function performFetch(url) {
|
||||
let resp = await fetch(url)
|
||||
return await resp.text()
|
||||
}
|
472
front/package-lock.json
generated
472
front/package-lock.json
generated
@ -1,472 +0,0 @@
|
||||
{
|
||||
"name": "front",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "front",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"bootstrap": "5.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"http-server": "14.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@popperjs/core": {
|
||||
"version": "2.11.6",
|
||||
"resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.6.tgz",
|
||||
"integrity": "sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==",
|
||||
"peer": true,
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/popperjs"
|
||||
}
|
||||
},
|
||||
"node_modules/ansi-styles": {
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
|
||||
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"color-convert": "^2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/async": {
|
||||
"version": "2.6.4",
|
||||
"resolved": "https://registry.npmjs.org/async/-/async-2.6.4.tgz",
|
||||
"integrity": "sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.14"
|
||||
}
|
||||
},
|
||||
"node_modules/basic-auth": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
||||
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"safe-buffer": "5.1.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/bootstrap": {
|
||||
"version": "5.2.1",
|
||||
"resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-5.2.1.tgz",
|
||||
"integrity": "sha512-UQi3v2NpVPEi1n35dmRRzBJFlgvWHYwyem6yHhuT6afYF+sziEt46McRbT//kVXZ7b1YUYEVGdXEH74Nx3xzGA==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/twbs"
|
||||
},
|
||||
{
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/bootstrap"
|
||||
}
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@popperjs/core": "^2.11.6"
|
||||
}
|
||||
},
|
||||
"node_modules/call-bind": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
|
||||
"integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"function-bind": "^1.1.1",
|
||||
"get-intrinsic": "^1.0.2"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "4.1.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
|
||||
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"ansi-styles": "^4.1.0",
|
||||
"supports-color": "^7.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/color-convert": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
||||
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"color-name": "~1.1.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=7.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/color-name": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
|
||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/corser": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/corser/-/corser-2.0.1.tgz",
|
||||
"integrity": "sha512-utCYNzRSQIZNPIcGZdQc92UVJYAhtGAteCFg0yRaFm8f0P+CPtyGyHXJcGXnffjCybUCEx3FQ2G7U3/o9eIkVQ==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">= 0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/debug": {
|
||||
"version": "3.2.7",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
|
||||
"integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"ms": "^2.1.1"
|
||||
}
|
||||
},
|
||||
"node_modules/eventemitter3": {
|
||||
"version": "4.0.7",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
|
||||
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.15.2",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
|
||||
"integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
|
||||
"dev": true,
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||
}
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"debug": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/function-bind": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
|
||||
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/get-intrinsic": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.0.tgz",
|
||||
"integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"function-bind": "^1.1.1",
|
||||
"has": "^1.0.3",
|
||||
"has-symbols": "^1.0.3"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/has": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
|
||||
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"function-bind": "^1.1.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/has-flag": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
||||
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/has-symbols": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz",
|
||||
"integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">= 0.4"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/he": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
|
||||
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"he": "bin/he"
|
||||
}
|
||||
},
|
||||
"node_modules/html-encoding-sniffer": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz",
|
||||
"integrity": "sha512-oWv4T4yJ52iKrufjnyZPkrN0CH3QnrUqdB6In1g5Fe1mia8GmF36gnfNySxoZtxD5+NmYw1EElVXiBk93UeskA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"whatwg-encoding": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/http-proxy": {
|
||||
"version": "1.18.1",
|
||||
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
|
||||
"integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"eventemitter3": "^4.0.0",
|
||||
"follow-redirects": "^1.0.0",
|
||||
"requires-port": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/http-server": {
|
||||
"version": "14.1.1",
|
||||
"resolved": "https://registry.npmjs.org/http-server/-/http-server-14.1.1.tgz",
|
||||
"integrity": "sha512-+cbxadF40UXd9T01zUHgA+rlo2Bg1Srer4+B4NwIHdaGxAGGv59nYRnGGDJ9LBk7alpS0US+J+bLLdQOOkJq4A==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"basic-auth": "^2.0.1",
|
||||
"chalk": "^4.1.2",
|
||||
"corser": "^2.0.1",
|
||||
"he": "^1.2.0",
|
||||
"html-encoding-sniffer": "^3.0.0",
|
||||
"http-proxy": "^1.18.1",
|
||||
"mime": "^1.6.0",
|
||||
"minimist": "^1.2.6",
|
||||
"opener": "^1.5.1",
|
||||
"portfinder": "^1.0.28",
|
||||
"secure-compare": "3.0.1",
|
||||
"union": "~0.5.0",
|
||||
"url-join": "^4.0.1"
|
||||
},
|
||||
"bin": {
|
||||
"http-server": "bin/http-server"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/iconv-lite": {
|
||||
"version": "0.6.3",
|
||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
|
||||
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/lodash": {
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/mime": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
|
||||
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"mime": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/minimist": {
|
||||
"version": "1.2.8",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
|
||||
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
||||
"dev": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/mkdirp": {
|
||||
"version": "0.5.6",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
|
||||
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"minimist": "^1.2.6"
|
||||
},
|
||||
"bin": {
|
||||
"mkdirp": "bin/cmd.js"
|
||||
}
|
||||
},
|
||||
"node_modules/ms": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/object-inspect": {
|
||||
"version": "1.12.3",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
|
||||
"integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==",
|
||||
"dev": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/opener": {
|
||||
"version": "1.5.2",
|
||||
"resolved": "https://registry.npmjs.org/opener/-/opener-1.5.2.tgz",
|
||||
"integrity": "sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"opener": "bin/opener-bin.js"
|
||||
}
|
||||
},
|
||||
"node_modules/portfinder": {
|
||||
"version": "1.0.32",
|
||||
"resolved": "https://registry.npmjs.org/portfinder/-/portfinder-1.0.32.tgz",
|
||||
"integrity": "sha512-on2ZJVVDXRADWE6jnQaX0ioEylzgBpQk8r55NE4wjXW1ZxO+BgDlY6DXwj20i0V8eB4SenDQ00WEaxfiIQPcxg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"async": "^2.6.4",
|
||||
"debug": "^3.2.7",
|
||||
"mkdirp": "^0.5.6"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/qs": {
|
||||
"version": "6.11.0",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
|
||||
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"side-channel": "^1.0.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.6"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/requires-port": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
|
||||
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/safe-buffer": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/safer-buffer": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/secure-compare": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/secure-compare/-/secure-compare-3.0.1.tgz",
|
||||
"integrity": "sha512-AckIIV90rPDcBcglUwXPF3kg0P0qmPsPXAj6BBEENQE1p5yA1xfmDJzfi1Tappj37Pv2mVbKpL3Z1T+Nn7k1Qw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/side-channel": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
||||
"integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"call-bind": "^1.0.0",
|
||||
"get-intrinsic": "^1.0.2",
|
||||
"object-inspect": "^1.9.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/supports-color": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"has-flag": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/union": {
|
||||
"version": "0.5.0",
|
||||
"resolved": "https://registry.npmjs.org/union/-/union-0.5.0.tgz",
|
||||
"integrity": "sha512-N6uOhuW6zO95P3Mel2I2zMsbsanvvtgn6jVqJv4vbVcz/JN0OkL9suomjQGmWtxJQXOCqUJvquc1sMeNz/IwlA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"qs": "^6.4.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/url-join": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz",
|
||||
"integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/whatwg-encoding": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-2.0.0.tgz",
|
||||
"integrity": "sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"iconv-lite": "0.6.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "front",
|
||||
"version": "1.0.0",
|
||||
"main": "index.html",
|
||||
"scripts": {
|
||||
"web-server": "http-server -p 3000 ./",
|
||||
"start": "npm run web-server"
|
||||
},
|
||||
"dependencies": {
|
||||
"bootstrap": "5.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"http-server": "14.1.1"
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class LowercaseController {
|
||||
@GetMapping("/lowercase")
|
||||
public String lowercase(@RequestParam(value = "value", defaultValue = "None") String value) {
|
||||
return value.toLowerCase();
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ReplaceController {
|
||||
@GetMapping("/replace")
|
||||
public String replace(@RequestParam(value = "value", defaultValue = "None") String value,
|
||||
@RequestParam(value = "old", defaultValue = "") char oldChar,
|
||||
@RequestParam(value = "new", defaultValue = "") char newChar) {
|
||||
return value.replace(oldChar, newChar);
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class SplitController {
|
||||
@GetMapping("/split")
|
||||
public String split(@RequestParam(value = "value", defaultValue = "None") String value,
|
||||
@RequestParam(value = "splitBy", defaultValue = " ") String splitBy) {
|
||||
String[] temp = value.split(splitBy);
|
||||
String res = "";
|
||||
for (int i = 0; i < temp.length; i++) {
|
||||
res += temp[i];
|
||||
res += "; ";
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UppercaseController {
|
||||
@GetMapping("/uppercase")
|
||||
public String uppercase(@RequestParam(value = "value", defaultValue = "None") String value) {
|
||||
return value.toUpperCase();
|
||||
}
|
||||
}
|
@ -2,12 +2,19 @@ package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
public static final String REST_API = "/api";
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
WebMvcConfigurer.super.addViewControllers(registry);
|
||||
registry.addViewController("rest-test");
|
||||
}
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**").allowedMethods("*");
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
public class YanApplication {
|
||||
|
@ -0,0 +1,64 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.controller.ComputerDto;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/cabinet")
|
||||
public class CabinetController {
|
||||
private final CabinetService cabinetService;
|
||||
public CabinetController(CabinetService cabinetService) {
|
||||
this.cabinetService = cabinetService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public CabinetDto getCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(cabinetService.findCabinet(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<CabinetDto> getCabinets() {
|
||||
return cabinetService.findAllCabinets().stream()
|
||||
.map(CabinetDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/{id}/computers")
|
||||
public List<ComputerDto> getCabinetComputers(@PathVariable Long id) {
|
||||
return cabinetService.listComputersFromCabinet(id).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<CabinetDto> getFilteredCabinets(@RequestParam(value = "id", required = false) Long id,
|
||||
@RequestParam(value = "number", required = false) String number) {
|
||||
return cabinetService.findFilteredCabinets(id, number).stream()
|
||||
.map(CabinetDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping("/")
|
||||
public CabinetDto createCabinet(@RequestBody @Valid CabinetDto cabinetDto) {
|
||||
return new CabinetDto(cabinetService.addCabinet(cabinetDto.getNumber()));
|
||||
}
|
||||
@PostMapping("/{id}/computer")
|
||||
public void createCabinetComputer(@PathVariable Long id,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
cabinetService.addComputerToCabinet(computerId, id);
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public CabinetDto updateCabinet(@PathVariable Long id,
|
||||
@RequestBody @Valid CabinetDto cabinetDto) {
|
||||
return new CabinetDto(cabinetService.updateCabinet(id, cabinetDto.getNumber()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public CabinetDto deleteCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(cabinetService.deleteCabinet(id));
|
||||
}
|
||||
@DeleteMapping("/{cabinetId}/computer")
|
||||
public void deleteCabinetComputer(@PathVariable Long cabinetId,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
cabinetService.deleteComputerFromCabinet(computerId, cabinetId);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CabinetDto {
|
||||
private Long id;
|
||||
private String number;
|
||||
private List<Long> computerIds;
|
||||
public CabinetDto() {}
|
||||
public CabinetDto(Cabinet cabinet) {
|
||||
this.id = cabinet.getId();
|
||||
this.number = cabinet.getNumber();
|
||||
if (cabinet.getComputers() == null) {
|
||||
this.computerIds = new ArrayList<>();
|
||||
} else {
|
||||
this.computerIds = new ArrayList<>();
|
||||
List<Computer> computers = cabinet.getComputers();
|
||||
for (Computer computer : computers) {
|
||||
computerIds.add(computer.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getNumber() { return this.number; }
|
||||
public void setNumber(String number) { this.number = number; }
|
||||
public List<Long> getComputerIds() { return this.computerIds; }
|
||||
public void setComputerIds(List<Long> computerIds) { this.computerIds = computerIds; }
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import com.kalyshev.yan.computer.controller.ComputerDto;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/index")
|
||||
public class CabinetMvcController {
|
||||
private final CabinetService cabinetService;
|
||||
private final ComputerService computerService;
|
||||
public CabinetMvcController(CabinetService cabinetService,
|
||||
ComputerService computerService) {
|
||||
this.cabinetService = cabinetService;
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping
|
||||
public String getCabinets(Model model) {
|
||||
model.addAttribute("cabinets",
|
||||
cabinetService.findAllCabinets().stream()
|
||||
.map(CabinetDto::new)
|
||||
.toList());
|
||||
return "index";
|
||||
}
|
||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||
public String editCabinet(@PathVariable(required = false) Long id,
|
||||
Model model) {
|
||||
if (id == null || id <= 0) {
|
||||
model.addAttribute("cabinetDto", new CabinetDto());
|
||||
} else {
|
||||
model.addAttribute("cabinetId", id);
|
||||
model.addAttribute("cabinetDto", new CabinetDto(cabinetService.findCabinet(id)));
|
||||
}
|
||||
model.addAttribute("computers",
|
||||
computerService.findFilteredComputers(null, null, null, null, id).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList());
|
||||
model.addAttribute("allComputers",
|
||||
computerService.findAllComputers().stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList());
|
||||
return "cabinet-edit";
|
||||
}
|
||||
@PostMapping(value = {"", "/{id}"})
|
||||
public String saveCabinet(@PathVariable(required = false) Long id,
|
||||
@ModelAttribute @Valid CabinetDto cabinetDto,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||
return "cabinet-edit";
|
||||
}
|
||||
if (id == null || id <= 0) {
|
||||
cabinetService.addCabinet(cabinetDto.getNumber());
|
||||
} else {
|
||||
cabinetService.updateCabinet(id, cabinetDto.getNumber());
|
||||
}
|
||||
return "redirect:/index";
|
||||
}
|
||||
@PostMapping(value = "/{id}/computer/{computerId}")
|
||||
public String addCabinetComputer(@PathVariable(value = "id") Long id,
|
||||
@PathVariable(value = "computerId") Long computerId) {
|
||||
cabinetService.addComputerToCabinet(computerId, id);
|
||||
return "redirect:/index";
|
||||
}
|
||||
@PostMapping(value = "/{id}/computerDelete/{computerId}")
|
||||
public String deleteCabinetComputer(@PathVariable(value = "id") Long id,
|
||||
@PathVariable(value = "computerId") Long computerId) {
|
||||
cabinetService.deleteComputerFromCabinet(computerId, id);
|
||||
return "redirect:/index";
|
||||
}
|
||||
@PostMapping("/delete/{id}")
|
||||
public String deleteCabinet(@PathVariable Long id) {
|
||||
cabinetService.deleteCabinet(id);
|
||||
return "redirect:/index";
|
||||
}
|
||||
}
|
71
src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java
Normal file
71
src/main/java/com/kalyshev/yan/cabinet/model/Cabinet.java
Normal file
@ -0,0 +1,71 @@
|
||||
package com.kalyshev.yan.cabinet.model;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "cabinet")
|
||||
public class Cabinet {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String number;
|
||||
@OneToMany(cascade = {CascadeType.MERGE})
|
||||
private List<Computer> computers;
|
||||
|
||||
public Cabinet() {
|
||||
}
|
||||
public Cabinet(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
public List<Computer> getComputers() {
|
||||
return computers;
|
||||
}
|
||||
public void addComputer(Computer computer){
|
||||
if (computers == null){
|
||||
this.computers = new ArrayList<>();
|
||||
}
|
||||
if (!computers.contains(computer)) {
|
||||
this.computers.add(computer);
|
||||
computer.setCabinet(this);
|
||||
}
|
||||
}
|
||||
public void removeComputer(Computer computer){
|
||||
if (computers.contains(computer))
|
||||
this.computers.remove(computer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Cabinet))
|
||||
return false;
|
||||
Cabinet cabinet = (Cabinet) o;
|
||||
return Objects.equals(id, cabinet.id) && Objects.equals(this.number, cabinet.number);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cabinet{" +
|
||||
"id=" + id +
|
||||
", number='" + number + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.cabinet.repository;
|
||||
|
||||
public class CabinetNotFoundException extends RuntimeException {
|
||||
public CabinetNotFoundException(Long id) {
|
||||
super(String.format("Cabinet with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.kalyshev.yan.cabinet.repository;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CabinetRepository extends JpaRepository<Cabinet, Long> {
|
||||
@Query(value = "select s from Cabinet s where (s.id = :id or :id is Null) and (s.number = :number or :number is Null)")
|
||||
public List<Cabinet> findFilteredCabinets(@Param("id") Long id,
|
||||
@Param("number") String number);
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.kalyshev.yan.cabinet.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetRepository;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CabinetService {
|
||||
private final CabinetRepository cabinetRepository;
|
||||
private final ComputerService computerService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public CabinetService(CabinetRepository cabinetRepository,
|
||||
ComputerService computerService,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.cabinetRepository = cabinetRepository;
|
||||
this.computerService = computerService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet addCabinet(String number) {
|
||||
if (!StringUtils.hasText(number)) {
|
||||
throw new IllegalArgumentException("Cabinet number is null or empty");
|
||||
}
|
||||
final Cabinet cabinet = new Cabinet(number);
|
||||
validatorUtil.validate(cabinet);
|
||||
return cabinetRepository.save(cabinet);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public Cabinet findCabinet(Long id) {
|
||||
final Optional<Cabinet> cabinet = cabinetRepository.findById(id);
|
||||
return cabinet.orElseThrow(() -> new CabinetNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findFilteredCabinets(Long id, String number) {
|
||||
return cabinetRepository.findFilteredCabinets(id, number);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findAllCabinets() {
|
||||
return cabinetRepository.findAll();
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet updateCabinet(Long id, String number) {
|
||||
if (!StringUtils.hasText(number)) {
|
||||
throw new IllegalArgumentException("Cabinet number is null or empty");
|
||||
}
|
||||
final Cabinet currentCabinet = findCabinet(id);
|
||||
currentCabinet.setNumber(number);
|
||||
validatorUtil.validate(currentCabinet);
|
||||
return cabinetRepository.save(currentCabinet);
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet deleteCabinet(Long id) {
|
||||
final Cabinet currentCabinet = findCabinet(id);
|
||||
computerService.deleteRelationsWithCabinets(currentCabinet.getComputers());
|
||||
cabinetRepository.delete(currentCabinet);
|
||||
return currentCabinet;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllCabinets() {
|
||||
cabinetRepository.deleteAll();
|
||||
}
|
||||
@Transactional
|
||||
public List<Computer> listComputersFromCabinet(Long id) {
|
||||
if ((Object)id == null) {
|
||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
||||
}
|
||||
return findCabinet(id).getComputers();
|
||||
}
|
||||
@Transactional
|
||||
public void addComputerToCabinet(Long computerId, Long cabinetId) {
|
||||
if ((Object)computerId == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
}
|
||||
if ((Object)cabinetId == null) {
|
||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
||||
}
|
||||
final Computer computer = computerService.findComputer(computerId);
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.addComputer(computer);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteComputerFromCabinet(Long computerId, Long cabinetId) {
|
||||
if ((Object) computerId == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
}
|
||||
if ((Object) cabinetId == null) {
|
||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
||||
}
|
||||
final Computer computer = computerService.findComputer(computerId);
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.removeComputer(computer);
|
||||
computer.setCabinet(null);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.controller.MonitorDto;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/computer")
|
||||
public class ComputerController {
|
||||
private final ComputerService computerService;
|
||||
public ComputerController(ComputerService computerService) {
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public ComputerDto getStudent(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.findComputer(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<ComputerDto> getComputers() {
|
||||
return computerService.findAllComputers().stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<ComputerDto> getFilteredComputers(@RequestParam(value = "id", required = false) Long id,
|
||||
@RequestParam(value = "modelName", required = false) String modelName,
|
||||
@RequestParam(value = "serialNum", required = false) String serialNum,
|
||||
@RequestParam(value = "monitorId", required = false) Long monitorId,
|
||||
@RequestParam(value = "cabinetId", required = false) Long cabinetId) {
|
||||
return computerService.findFilteredComputers(id, modelName, serialNum, monitorId, cabinetId).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping("/")
|
||||
public ComputerDto createComputer(@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId()));
|
||||
}
|
||||
@PostMapping("/{id}/monitor")
|
||||
public MonitorDto setMonitorComputer(@PathVariable Long id,
|
||||
@RequestParam("monitorId") Long monitorId) {
|
||||
return new MonitorDto(computerService.setMonitor(monitorId, id));
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public ComputerDto updateComputer(@PathVariable Long id,
|
||||
@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.updateComputer(id, computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public ComputerDto deleteComputer(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.deleteComputer(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
public class ComputerDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
private String serialNum;
|
||||
private Long monitorId;
|
||||
private String monitorModelName;
|
||||
private Long cabinetId;
|
||||
private String cabinetNumber;
|
||||
public ComputerDto() {}
|
||||
public ComputerDto(Computer computer) {
|
||||
this.id = computer.getId();
|
||||
this.modelName = computer.getModelName();
|
||||
this.serialNum = computer.getSerialNum();
|
||||
if (computer.getMonitor() == null) {
|
||||
this.monitorId = null;
|
||||
this.monitorModelName = "";
|
||||
} else {
|
||||
this.monitorId = computer.getMonitor().getId();
|
||||
this.monitorModelName = computer.getMonitor().getModelName();
|
||||
}
|
||||
if (computer.getCabinet() == null) {
|
||||
this.cabinetId = null;
|
||||
this.cabinetNumber = "";
|
||||
} else {
|
||||
this.cabinetId = computer.getCabinet().getId();
|
||||
this.cabinetNumber = computer.getCabinet().getNumber();
|
||||
}
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public String getModelName() { return this.modelName; }
|
||||
public void setModelName(String modelName) { this.modelName = modelName; }
|
||||
public String getSerialNum() { return this.serialNum; }
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Long getMonitorId() { return this.monitorId; }
|
||||
public void setMonitorId(Long monitorId) { this.monitorId = monitorId; }
|
||||
public Long getCabinetId() { return this.cabinetId; }
|
||||
public void setCabinetId(Long cabinetId) { this.cabinetId = cabinetId; }
|
||||
public String getMonitorModelName() { return this.monitorModelName; }
|
||||
public String getCabinetNumber() { return this.cabinetNumber; }
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.cabinet.controller.CabinetDto;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/computers")
|
||||
public class ComputerMvcController {
|
||||
private final ComputerService computerService;
|
||||
private final MonitorService monitorService;
|
||||
public ComputerMvcController(ComputerService computerService,
|
||||
MonitorService monitorService) {
|
||||
this.computerService = computerService;
|
||||
this.monitorService = monitorService;
|
||||
}
|
||||
@GetMapping
|
||||
public String getComputers(Model model) {
|
||||
model.addAttribute("computers",
|
||||
computerService.findAllComputers().stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList());
|
||||
return "computers";
|
||||
}
|
||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||
public String editComputer(@PathVariable(required = false) Long id,
|
||||
Model model) {
|
||||
if (id == null || id <= 0) {
|
||||
model.addAttribute("computerDto", new ComputerDto());
|
||||
} else {
|
||||
model.addAttribute("computerId", id);
|
||||
model.addAttribute("computerDto", new ComputerDto(computerService.findComputer(id)));
|
||||
}
|
||||
model.addAttribute("monitors",
|
||||
monitorService.findAllMonitors());
|
||||
return "computer-edit";
|
||||
}
|
||||
@PostMapping(value = {"", "/{id}"})
|
||||
public String saveComputer(@PathVariable(required = false) Long id,
|
||||
@ModelAttribute @Valid ComputerDto computerDto,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||
return "computer-edit";
|
||||
}
|
||||
if (id == null || id <= 0) {
|
||||
computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId());
|
||||
} else {
|
||||
computerService.updateComputer(id, computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId());
|
||||
}
|
||||
return "redirect:/computers";
|
||||
}
|
||||
@PostMapping("/delete/{id}")
|
||||
public String deleteComputer(@PathVariable Long id) {
|
||||
computerService.deleteComputer(id);
|
||||
return "redirect:/computers";
|
||||
}
|
||||
}
|
81
src/main/java/com/kalyshev/yan/computer/model/Computer.java
Normal file
81
src/main/java/com/kalyshev/yan/computer/model/Computer.java
Normal file
@ -0,0 +1,81 @@
|
||||
package com.kalyshev.yan.computer.model;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "computer")
|
||||
public class Computer {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String modelName;
|
||||
private String serialNum;
|
||||
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "cabinet", nullable = true)
|
||||
private Cabinet cabinet;
|
||||
@OneToOne(cascade = {CascadeType.MERGE})
|
||||
@JoinColumn(name = "monitor_id")
|
||||
private Monitor monitor;
|
||||
|
||||
public Computer() {
|
||||
}
|
||||
public Computer(String modelName, String serialNum) {
|
||||
this.modelName = modelName;
|
||||
this.serialNum = serialNum;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
public String getSerialNum() {
|
||||
return serialNum;
|
||||
}
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Cabinet getCabinet() {
|
||||
return cabinet;
|
||||
}
|
||||
public void setCabinet(Cabinet cabinet) { this.cabinet = cabinet; }
|
||||
|
||||
public Monitor getMonitor() {
|
||||
return monitor;
|
||||
}
|
||||
public void setMonitor(Monitor monitor) {
|
||||
this.monitor = monitor;
|
||||
}
|
||||
public Monitor removeMonitor() {
|
||||
Monitor temp = this.monitor;
|
||||
this.monitor = null;
|
||||
return temp;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Computer))
|
||||
return false;
|
||||
Computer computer = (Computer) o;
|
||||
return Objects.equals(id, computer.id) && Objects.equals(this.modelName, computer.modelName) && Objects.equals(this.monitor, computer.monitor) && Objects.equals(this.cabinet, computer.cabinet);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" +
|
||||
"id=" + id +
|
||||
", modelName='" + modelName + '\'' +
|
||||
", serialNum='" + serialNum + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
public class ComputerNotFoundException extends RuntimeException {
|
||||
public ComputerNotFoundException(Long id) {
|
||||
super(String.format("Computer with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ComputerRepository extends JpaRepository<Computer, Long> {
|
||||
@Query(value = "select * from Computer where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null) and " +
|
||||
"(\"SERIAL_NUM\" = :serialNum or :serialNum is Null) and " +
|
||||
"(\"MONITOR_ID\" = :monitorId or :monitorId is Null) and " +
|
||||
"(\"CABINET\" = :cabinetId or :cabinetId is Null)", nativeQuery = true)
|
||||
public List<Computer> findFilteredComputers(@Param("id") Long id,
|
||||
@Param("modelName") String modelName,
|
||||
@Param("serialNum") String serialNum,
|
||||
@Param("monitorId") Long monitorId,
|
||||
@Param("cabinetId") Long cabinetId);
|
||||
|
||||
@Query(value = "select * from Computer where \"MONITOR_ID\" = :monitorId", nativeQuery = true)
|
||||
public Computer findComputerByMonitor(@Param("monitorId") Long monitorId);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "update Computer set \"MONITOR_ID\" = null", nativeQuery = true)
|
||||
public void removeAllComputerMonitorRelations();
|
||||
|
||||
// @Query(value = "update Computer where id in :computerIds set cabinet_id = null")
|
||||
// public void deleteRelationsWithCabinets(@Param("computerIds") Collection<Long> computerIds);
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
package com.kalyshev.yan.computer.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
import com.kalyshev.yan.computer.repository.ComputerRepository;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import javax.validation.constraints.Null;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class ComputerService {
|
||||
private final ComputerRepository computerRepository;
|
||||
private final MonitorService monitorService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public ComputerService(ComputerRepository computerRepository,
|
||||
MonitorService monitorService,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.computerRepository = computerRepository;
|
||||
this.monitorService = monitorService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Computer addComputer(String modelName, String serialNum, @Null Long monitorId) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Computer model name is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(serialNum)) {
|
||||
throw new IllegalArgumentException("Computer serial number is null or empty");
|
||||
}
|
||||
final Computer computer = new Computer(modelName, serialNum);
|
||||
if (monitorId != null) {
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
}
|
||||
validatorUtil.validate(computer);
|
||||
return computerRepository.save(computer);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public Computer findComputer(Long id) {
|
||||
final Optional<Computer> computer = computerRepository.findById(id);
|
||||
return computer.orElseThrow(() -> new ComputerNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findAllComputers() {
|
||||
return computerRepository.findAll();
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findFilteredComputers(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
return computerRepository.findFilteredComputers(id, modelName, serialNum, monitorId, cabinetId);
|
||||
}
|
||||
@Transactional
|
||||
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
|
||||
throw new IllegalArgumentException("Need at least one argument");
|
||||
}
|
||||
final Computer currentComputer = findComputer(id);
|
||||
if (modelName != null) {
|
||||
currentComputer.setModelName(modelName);
|
||||
}
|
||||
if (serialNum != null) {
|
||||
currentComputer.setSerialNum(serialNum);
|
||||
}
|
||||
if (monitorId != null) {
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
currentComputer.setMonitor(monitor);
|
||||
}
|
||||
validatorUtil.validate(currentComputer);
|
||||
return computerRepository.save(currentComputer);
|
||||
}
|
||||
@Transactional
|
||||
public Computer deleteComputer(Long id) {
|
||||
final Computer currentComputer = findComputer(id);
|
||||
Cabinet currentComputerCabinet = currentComputer.getCabinet();
|
||||
if (currentComputerCabinet != null) {
|
||||
currentComputerCabinet.removeComputer(currentComputer);
|
||||
}
|
||||
computerRepository.delete(currentComputer);
|
||||
return currentComputer;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllComputers() {
|
||||
List<Computer> computers = findAllComputers();
|
||||
for (Computer computer : computers) {
|
||||
deleteComputer(computer.getId());
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Monitor setMonitor(Long monitorId, Long computerId) {
|
||||
if ((Object)computerId == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
}
|
||||
if ((Object)monitorId == null) {
|
||||
throw new IllegalArgumentException("Monitor id is null or empty");
|
||||
}
|
||||
final Computer computer = findComputer(computerId);
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
return monitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteRelationsWithCabinets(List<Computer> computers) {
|
||||
for (Computer computer: computers) {
|
||||
computer.setCabinet(null);
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Computer findComputerByMonitor(Monitor monitor) {
|
||||
if (monitor.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return computerRepository.findComputerByMonitor(monitor.getId());
|
||||
}
|
||||
@Transactional
|
||||
public Monitor deleteMonitorWithRelation(Long id) {
|
||||
final Monitor currentMonitor = monitorService.findMonitor(id);
|
||||
Computer computer = findComputerByMonitor(currentMonitor);
|
||||
if (computer != null) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorService.deleteMonitor(currentMonitor);
|
||||
return currentMonitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitorsWithRelations() {
|
||||
computerRepository.removeAllComputerMonitorRelations();
|
||||
monitorService.deleteAllMonitors();
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/monitor")
|
||||
public class MonitorController {
|
||||
private final MonitorService monitorService;
|
||||
private final ComputerService computerService;
|
||||
public MonitorController(MonitorService monitorService,
|
||||
ComputerService computerService) {
|
||||
this.monitorService = monitorService;
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public MonitorDto getMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(monitorService.findMonitor(id));
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<MonitorDto> getMonitors() {
|
||||
return monitorService.findAllMonitors().stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/filter")
|
||||
public List<MonitorDto> getFilteredmonitors(@RequestParam(value = "id", required = false) Long id,
|
||||
@RequestParam(value = "modelName", required = false) String modelName) {
|
||||
return monitorService.findFilteredMonitors(id, modelName).stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping("/")
|
||||
public MonitorDto createMonitor(@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.addMonitor(monitorDto.getModelName()));
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public MonitorDto updateMonitor(@PathVariable Long id,
|
||||
@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.updateMonitor(id, monitorDto.getModelName()));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public MonitorDto deleteMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(computerService.deleteMonitorWithRelation(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
|
||||
public class MonitorDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
public MonitorDto() {
|
||||
}
|
||||
public MonitorDto(Monitor monitor) {
|
||||
this.id = monitor.getId();
|
||||
this.modelName = monitor.getModelName();
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.cabinet.controller.CabinetDto;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/monitors")
|
||||
public class MonitorMvcController {
|
||||
private final MonitorService monitorService;
|
||||
public MonitorMvcController(MonitorService monitorService) {
|
||||
this.monitorService = monitorService;
|
||||
}
|
||||
@GetMapping
|
||||
public String getMonitors(Model model) {
|
||||
model.addAttribute("monitors",
|
||||
monitorService.findAllMonitors().stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList());
|
||||
return "monitors";
|
||||
}
|
||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||
public String editMonitor(@PathVariable(required = false) Long id,
|
||||
Model model) {
|
||||
if (id == null || id <= 0) {
|
||||
model.addAttribute("monitorDto", new MonitorDto());
|
||||
} else {
|
||||
model.addAttribute("monitorId", id);
|
||||
model.addAttribute("monitorDto", new MonitorDto(monitorService.findMonitor(id)));
|
||||
}
|
||||
return "monitor-edit";
|
||||
}
|
||||
@PostMapping(value = {"", "/{id}"})
|
||||
public String saveComputer(@PathVariable(required = false) Long id,
|
||||
@ModelAttribute @Valid MonitorDto monitorDto,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||
return "monitor-edit";
|
||||
}
|
||||
if (id == null || id <= 0) {
|
||||
monitorService.addMonitor(monitorDto.getModelName());
|
||||
} else {
|
||||
monitorService.updateMonitor(id, monitorDto.getModelName());
|
||||
}
|
||||
return "redirect:/monitors";
|
||||
}
|
||||
@PostMapping("/delete/{id}")
|
||||
public String deleteComputer(@PathVariable Long id) {
|
||||
monitorService.deleteMonitor(monitorService.findMonitor(id));
|
||||
return "redirect:/monitors";
|
||||
}
|
||||
}
|
51
src/main/java/com/kalyshev/yan/monitor/model/Monitor.java
Normal file
51
src/main/java/com/kalyshev/yan/monitor/model/Monitor.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.kalyshev.yan.monitor.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "monitor")
|
||||
public class Monitor {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String modelName;
|
||||
public Monitor() {
|
||||
}
|
||||
public Monitor(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Monitor))
|
||||
return false;
|
||||
Monitor monitor = (Monitor) o;
|
||||
return Objects.equals(id, monitor.id) && Objects.equals(this.modelName, monitor.modelName);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Monitor{" +
|
||||
"id=" + id +
|
||||
", modelName='" + modelName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
public class MonitorNotFoundException extends RuntimeException {
|
||||
public MonitorNotFoundException(Long id) {
|
||||
super(String.format("Monitor with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MonitorRepository extends JpaRepository<Monitor, Long> {
|
||||
@Query(value = "select * from Monitor where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null)", nativeQuery = true)
|
||||
public List<Monitor> findFilteredMonitors(@Param("id") Long id,
|
||||
@Param("modelName") String modelName);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.kalyshev.yan.monitor.service;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorRepository;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MonitorService {
|
||||
private final MonitorRepository monitorRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public MonitorService(MonitorRepository monitorRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.monitorRepository = monitorRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Monitor addMonitor(String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor monitor = new Monitor(modelName);
|
||||
validatorUtil.validate(monitor);
|
||||
return monitorRepository.save(monitor);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public Monitor findMonitor(Long id) {
|
||||
final Optional<Monitor> monitor = monitorRepository.findById(id);
|
||||
return monitor.orElseThrow(() -> new MonitorNotFoundException(id));
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findAllMonitors() {
|
||||
return monitorRepository.findAll();
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findFilteredMonitors(Long id, String modelName) {
|
||||
return monitorRepository.findFilteredMonitors(id, modelName);
|
||||
}
|
||||
@Transactional
|
||||
public Monitor updateMonitor(Long id, String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor currentMonitor = findMonitor(id);
|
||||
currentMonitor.setModelName(modelName);
|
||||
validatorUtil.validate(currentMonitor);
|
||||
return monitorRepository.save(currentMonitor);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteMonitor(Monitor monitor) {
|
||||
monitorRepository.delete(monitor);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitors() { monitorRepository.deleteAll(); }
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.kalyshev.yan.util.error;
|
||||
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.util.validation.ValidationException;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice(annotations = RestController.class)
|
||||
public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
CabinetNotFoundException.class,
|
||||
ValidationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||
final ValidationException validationException = new ValidationException(
|
||||
e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
return handleException(validationException);
|
||||
}
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.kalyshev.yan.util.validation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public <T> ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.kalyshev.yan.util.validation;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ValidatorUtil {
|
||||
private final Validator validator;
|
||||
public ValidatorUtil() {
|
||||
this.validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
public <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1,11 @@
|
||||
|
||||
spring.main.banner-mode=off
|
||||
#server.port=8080
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.settings.trace=false
|
||||
spring.h2.console.settings.web-allow-others=false
|
15
src/main/resources/public/css/style.css
Normal file
15
src/main/resources/public/css/style.css
Normal file
@ -0,0 +1,15 @@
|
||||
.container-padding {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.margin-bottom {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.button-fixed {
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.button-sm {
|
||||
padding: 1px;
|
||||
}
|
72
src/main/resources/templates/cabinet-edit.html
Normal file
72
src/main/resources/templates/cabinet-edit.html
Normal file
@ -0,0 +1,72 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
|
||||
<form action="#" th:action="@{/index/{id}(id=${id})}" th:object="${cabinetDto}" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="number" class="form-label">Номер кабинета</label>
|
||||
<input type="text" class="form-control" id="number" th:field="${cabinetDto.number}" required="true">
|
||||
</div>
|
||||
<div th:if="${id != null}" class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col">Серийный номер</th>
|
||||
<th scope="col">Монитор</th>
|
||||
<th scope="col">Кабинет</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="computer, iterator: ${computers}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${computer.modelName}" style="width: 60%"/>
|
||||
<td th:text="${computer.serialNum}" style="width: 60%"/>
|
||||
<td th:text="${computer.monitorModelName}" style="width: 60%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
th:attr="onclick=|confirm('Удалить?') && document.getElementById('remove-${computer.id}').click()|">
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
</div>
|
||||
<form th:action="@{/index/{id}/computerDelete/{computerId}(id=${id}, computerId=${computer.id})}" method="post">
|
||||
<button th:id="'remove-' + ${computer.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary button-fixed">
|
||||
<span th:if="${id == null}">Добавить</span>
|
||||
<span th:if="${id != null}">Обновить</span>
|
||||
</button>
|
||||
<a class="btn btn-secondary button-fixed" th:href="@{/index}">
|
||||
Назад
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<form th:if="${id != null}" th:action="@{/index/{id}/computer(id=${id})}" id="addComputerForm" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="computers" class="form-label">Добавить компьютер</label>
|
||||
<select class="form-select" id="computers" required>
|
||||
<option disabled value="">Выберите компьютер</option>
|
||||
<option th:each="computer, iterator: ${allComputers}" th:text="${computer.modelName}" th:value="${computer.id}">
|
||||
</select>
|
||||
<button class="btn btn-outline-secondary" id="addComputerButton" th:attr="onclick=|document.getElementById('addComputerForm').action = document.getElementById('addComputerForm').action + '/' + document.getElementById('computers').value ; document.getElementById('addComputerForm}').submit()|">Добавить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
38
src/main/resources/templates/computer-edit.html
Normal file
38
src/main/resources/templates/computer-edit.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
|
||||
<form action="#" th:action="@{/computers/{id}(id=${id})}" th:object="${computerDto}" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="modelName" class="form-label">Название</label>
|
||||
<input type="text" class="form-control" id="modelName" th:field="${computerDto.modelName}" required="true">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="serialNum" class="form-label">Серийный номер</label>
|
||||
<input type="text" class="form-control" id="serialNum" th:field="${computerDto.serialNum}" required="true">
|
||||
</div>
|
||||
<label for="monitor" class="form-label">Монитор</label>
|
||||
<select class="form-select" id="monitor" th:field="${computerDto.monitorId}">
|
||||
<option value="">Выберите монитор</option>
|
||||
<option th:each="monitor, iterator: ${monitors}" th:text="${monitor.modelName}" th:value="${monitor.id}"
|
||||
th:selected="${monitor.id==computerDto.monitorId}">
|
||||
</option>
|
||||
</select>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary button-fixed">
|
||||
<span th:if="${id == null}">Добавить</span>
|
||||
<span th:if="${id != null}">Обновить</span>
|
||||
</button>
|
||||
<a class="btn btn-secondary button-fixed" th:href="@{/computers}">
|
||||
Назад
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
57
src/main/resources/templates/computers.html
Normal file
57
src/main/resources/templates/computers.html
Normal file
@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div>
|
||||
<a class="btn btn-success button-fixed"
|
||||
th:href="@{/computers/edit/}">
|
||||
<i class="fa-solid fa-plus"></i> Добавить
|
||||
</a>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col">Серийный номер</th>
|
||||
<th scope="col">Монитор</th>
|
||||
<th scope="col">Кабинет</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="computer, iterator: ${computers}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${computer.modelName}" style="width: 60%"/>
|
||||
<td th:text="${computer.serialNum}" style="width: 60%"/>
|
||||
<td th:text="${computer.monitorModelName}" style="width: 60%"/>
|
||||
<td th:text="${computer.cabinetNumber}" style="width: 60%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<a class="btn btn-warning button-fixed button-sm"
|
||||
th:href="@{/computers/edit/{id}(id=${computer.id})}">
|
||||
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
|
||||
</a>
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${computer.id}').click()|">
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
</div>
|
||||
<form th:action="@{/computers/delete/{id}(id=${computer.id})}" method="post">
|
||||
<button th:id="'remove-' + ${computer.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
44
src/main/resources/templates/default.html
Normal file
44
src/main/resources/templates/default.html
Normal file
@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>MVC App</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<script type="text/javascript" src="/webjars/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap/5.1.3/css/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" href="/webjars/font-awesome/6.1.0/css/all.min.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="/">
|
||||
<i class="fa-solid fa-font-awesome"></i>
|
||||
Рабочее место оператора пункта выдачи заказов
|
||||
</a>
|
||||
<button class="navbar-toggler" type="button"
|
||||
data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
||||
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav" th:with="activeLink=${#request.requestURI}">
|
||||
<a class="nav-link" href="/index"
|
||||
th:classappend="${#strings.equals(activeLink, '/index')} ? 'active' : ''">Кабинеты</a>
|
||||
<a class="nav-link" href="/computers"
|
||||
th:classappend="${#strings.equals(activeLink, '/computers')} ? 'active' : ''">Компьютеры</a>
|
||||
<a class="nav-link" href="/monitors"
|
||||
th:classappend="${#strings.equals(activeLink, '/monitors')} ? 'active' : ''">Мониторы</a>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container-fluid">
|
||||
<div class="container container-padding" layout:fragment="content">
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<th:block layout:fragment="scripts">
|
||||
</th:block>
|
||||
</html>
|
13
src/main/resources/templates/error.html
Normal file
13
src/main/resources/templates/error.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div><span th:text="${error}"></span></div>
|
||||
<a href="/">На главную</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
52
src/main/resources/templates/index.html
Normal file
52
src/main/resources/templates/index.html
Normal file
@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Сайт</title>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div>
|
||||
<a class="btn btn-success button-fixed"
|
||||
th:href="@{/index/edit/}">
|
||||
<i class="fa-solid fa-plus"></i> Добавить
|
||||
</a>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Номер кабинета</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="cabinet, iterator: ${cabinets}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${cabinet.number}" style="width: 60%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<a class="btn btn-warning button-fixed button-sm"
|
||||
th:href="@{/index/edit/{id}(id=${cabinet.id})}">
|
||||
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
|
||||
</a>
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${cabinet.id}').click()|">
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
</div>
|
||||
<form th:action="@{/index/delete/{id}(id=${cabinet.id})}" method="post">
|
||||
<button th:id="'remove-' + ${cabinet.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
27
src/main/resources/templates/monitor-edit.html
Normal file
27
src/main/resources/templates/monitor-edit.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
|
||||
<form action="#" th:action="@{/monitors/{id}(id=${id})}" th:object="${monitorDto}" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="modelName" class="form-label">Название</label>
|
||||
<input type="text" class="form-control" id="modelName" th:field="${monitorDto.modelName}" required="true">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary button-fixed">
|
||||
<span th:if="${id == null}">Добавить</span>
|
||||
<span th:if="${id != null}">Обновить</span>
|
||||
</button>
|
||||
<a class="btn btn-secondary button-fixed" th:href="@{/monitors}">
|
||||
Назад
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
51
src/main/resources/templates/monitors.html
Normal file
51
src/main/resources/templates/monitors.html
Normal file
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<div>
|
||||
<a class="btn btn-success button-fixed"
|
||||
th:href="@{/monitors/edit/}">
|
||||
<i class="fa-solid fa-plus"></i> Добавить
|
||||
</a>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Название</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="monitor, iterator: ${monitors}">
|
||||
<th scope="row" th:text="${iterator.index} + 1"/>
|
||||
<td th:text="${monitor.modelName}" style="width: 60%"/>
|
||||
<td style="width: 10%">
|
||||
<div class="btn-group" role="group" aria-label="Basic example">
|
||||
<a class="btn btn-warning button-fixed button-sm"
|
||||
th:href="@{/monitors/edit/{id}(id=${monitor.id})}">
|
||||
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
|
||||
</a>
|
||||
<button type="button" class="btn btn-danger button-fixed button-sm"
|
||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${monitor.id}').click()|">
|
||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||
</button>
|
||||
</div>
|
||||
<form th:action="@{/monitors/delete/{id}(id=${monitor.id})}" method="post">
|
||||
<button th:id="'remove-' + ${monitor.id}" type="submit" style="display: none">
|
||||
Удалить
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
64
src/test/java/com/kalyshev/yan/JpaCabinetTests.java
Normal file
64
src/test/java/com/kalyshev/yan/JpaCabinetTests.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaCabinetTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaCabinetTests.class);
|
||||
@Autowired
|
||||
private ComputerService computerService;
|
||||
@Autowired
|
||||
private MonitorService monitorService;
|
||||
@Autowired
|
||||
private CabinetService cabinetService;
|
||||
|
||||
@Test
|
||||
void testCabinetCreate() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
log.info(cabinet.toString());
|
||||
Assertions.assertNotNull(cabinet.getId());
|
||||
}
|
||||
@Test
|
||||
void testCabinetRead() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
log.info(cabinet.toString());
|
||||
final Cabinet findCabinet = cabinetService.findCabinet(cabinet.getId());
|
||||
log.info(findCabinet.toString());
|
||||
Assertions.assertEquals(cabinet, findCabinet);
|
||||
}
|
||||
@Test
|
||||
void testCabinetReadNotFound() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
Assertions.assertThrows(CabinetNotFoundException.class, () -> cabinetService.findCabinet(-1L));
|
||||
}
|
||||
@Test
|
||||
void testCabinetReadAll() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
cabinetService.addCabinet("18");
|
||||
cabinetService.addCabinet("19");
|
||||
final List<Cabinet> cabinets = cabinetService.findAllCabinets();
|
||||
log.info(cabinets.toString());
|
||||
Assertions.assertEquals(cabinets.size(), 2);
|
||||
}
|
||||
@Test
|
||||
void testComputerReadAllEmpty() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
final List<Cabinet> cabinets = cabinetService.findAllCabinets();
|
||||
log.info(cabinets.toString());
|
||||
Assertions.assertEquals(cabinets.size(), 0);
|
||||
}
|
||||
}
|
85
src/test/java/com/kalyshev/yan/JpaComputerCabinetTests.java
Normal file
85
src/test/java/com/kalyshev/yan/JpaComputerCabinetTests.java
Normal file
@ -0,0 +1,85 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaComputerCabinetTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaCabinetTests.class);
|
||||
@Autowired
|
||||
private ComputerService computerService;
|
||||
@Autowired
|
||||
private MonitorService monitorService;
|
||||
@Autowired
|
||||
private CabinetService cabinetService;
|
||||
|
||||
@Test
|
||||
void testCabinetAddOneToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getSerialNum(), computer.getSerialNum());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCabinetDeleteOneToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getSerialNum(), computer.getSerialNum());
|
||||
cabinet.removeComputer(computer);
|
||||
Assertions.assertEquals(cabinet.getComputers().size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testComputerAddManyToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(computer.getCabinet().getNumber(), cabinet.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testComputerDeleteManyToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(computer.getCabinet().getNumber(), cabinet.getNumber());
|
||||
computer.setCabinet(null);
|
||||
Assertions.assertNull(computer.getCabinet());
|
||||
}
|
||||
}
|
89
src/test/java/com/kalyshev/yan/JpaComputerTests.java
Normal file
89
src/test/java/com/kalyshev/yan/JpaComputerTests.java
Normal file
@ -0,0 +1,89 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaComputerTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaComputerTests.class);
|
||||
@Autowired
|
||||
private ComputerService computerService;
|
||||
@Autowired
|
||||
private MonitorService monitorService;
|
||||
@Autowired
|
||||
private CabinetService cabinetService;
|
||||
|
||||
@Test
|
||||
void testComputerPartialCreate() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertNotNull(computer.getId());
|
||||
Assertions.assertNull(computer.getCabinet());
|
||||
}
|
||||
@Test
|
||||
void testComputerFullCreate() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18a");
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(computer.toString());
|
||||
Assertions.assertNotNull(computer.getId());
|
||||
Assertions.assertEquals(cabinet, computer.getCabinet());
|
||||
}
|
||||
@Test
|
||||
void testComputerDelete() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null);
|
||||
log.info(computer.toString());
|
||||
computerService.deleteComputer(computer.getId());
|
||||
Assertions.assertThrows(ComputerNotFoundException.class, () -> computerService.findComputer(computer.getId()));
|
||||
}
|
||||
@Test
|
||||
void testComputerRead() {
|
||||
computerService.deleteAllComputers();
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null);
|
||||
log.info(computer.toString());
|
||||
final Computer findComputer = computerService.findComputer(computer.getId());
|
||||
log.info(findComputer.toString());
|
||||
Assertions.assertEquals(computer, findComputer);
|
||||
}
|
||||
@Test
|
||||
void testComputerReadNotFound() {
|
||||
computerService.deleteAllComputers();
|
||||
Assertions.assertThrows(ComputerNotFoundException.class, () -> computerService.findComputer(-1L));
|
||||
}
|
||||
@Test
|
||||
void testComputerReadAll() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
computerService.addComputer("Computer", "w7894572", null);
|
||||
computerService.addComputer("Another comp", "3453s", null);
|
||||
final List<Computer> computers = computerService.findAllComputers();
|
||||
log.info(computers.toString());
|
||||
Assertions.assertEquals(computers.size(), 2);
|
||||
}
|
||||
@Test
|
||||
void testComputerReadAllEmpty() {
|
||||
computerService.deleteAllComputers();
|
||||
final List<Computer> computers = computerService.findAllComputers();
|
||||
log.info(computers.toString());
|
||||
Assertions.assertEquals(computers.size(), 0);
|
||||
}
|
||||
}
|
78
src/test/java/com/kalyshev/yan/JpaMonitorTests.java
Normal file
78
src/test/java/com/kalyshev/yan/JpaMonitorTests.java
Normal file
@ -0,0 +1,78 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
@SpringBootTest
|
||||
public class JpaMonitorTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaMonitorTests.class);
|
||||
@Autowired
|
||||
private MonitorService monitorService;
|
||||
@Autowired
|
||||
private ComputerService computerService;
|
||||
@Test
|
||||
void testMonitorCreate() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
Assertions.assertNotNull(monitor.getId());
|
||||
}
|
||||
@Test
|
||||
void testMonitorRead() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
final Monitor findMonitor = monitorService.findMonitor(monitor.getId());
|
||||
log.info(findMonitor.toString());
|
||||
Assertions.assertEquals(monitor, findMonitor);
|
||||
}
|
||||
@Test
|
||||
void testMonitorDelete() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
computerService.deleteMonitorWithRelation(monitor.getId());
|
||||
Assertions.assertThrows(MonitorNotFoundException.class, () -> monitorService.findMonitor(monitor.getId()));
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadNotFound() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
Assertions.assertThrows(MonitorNotFoundException.class, () -> monitorService.findMonitor(-1L));
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadAll() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
monitorService.addMonitor("Asus");
|
||||
monitorService.addMonitor("HP");
|
||||
final List<Monitor> monitors = monitorService.findAllMonitors();
|
||||
log.info(monitors.toString());
|
||||
Assertions.assertEquals(monitors.size(), 2);
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadAllEmpty() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final List<Monitor> monitors = monitorService.findAllMonitors();
|
||||
log.info(monitors.toString());
|
||||
Assertions.assertEquals(monitors.size(), 0);
|
||||
}
|
||||
@Test
|
||||
void testMonitorGetComputer() {
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
computerService.deleteAllComputers();
|
||||
Monitor monitor = monitorService.addMonitor("Asus");
|
||||
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId());
|
||||
log.info(computer.toString());
|
||||
Computer fetchedComputer = computerService.findComputerByMonitor(monitor);
|
||||
Assertions.assertEquals(computer, fetchedComputer);
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class YanApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user