Compare commits
10 Commits
0f30ec0e19
...
LabWork05
| Author | SHA1 | Date | |
|---|---|---|---|
| cbda0f455f | |||
| 60c2564229 | |||
| 8348a9bf31 | |||
| 2918fee687 | |||
| 5ec74ef311 | |||
| a243920d42 | |||
| 809e593a86 | |||
| eada9f3c88 | |||
| d84000b5b4 | |||
| 382e4729fb |
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
HELP.md
|
||||||
|
.gradle
|
||||||
|
build/
|
||||||
|
!gradle/wrapper/gradle-wrapper.jar
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
lab4-vue-front/node_modules
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
11
build.gradle
11
build.gradle
@@ -15,6 +15,17 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
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 group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
|
implementation 'org.hibernate.validator:hibernate-validator'
|
||||||
|
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Calc</title>
|
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
|
|
||||||
<input type="number" class="form-control" id="num1">
|
|
||||||
<select class="form-control" id="operation">
|
|
||||||
<option value="sum">+</option>
|
|
||||||
<option value="sub">-</option>
|
|
||||||
<option value="mul">*</option>
|
|
||||||
<option value="div">/</option>
|
|
||||||
</select>
|
|
||||||
<input type="number" class="form-control" id="num2">
|
|
||||||
<button type="button" class="form-control btn btn-light" id="get-result" onclick="calc()">=</button>
|
|
||||||
<p class="h5" id="result"></p>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
async function calc() {
|
|
||||||
const num1 = document.getElementById("num1").value
|
|
||||||
const num2 = document.getElementById("num2").value
|
|
||||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/${document.getElementById("operation").value}?num1=${num1}&num2=${num2}`)).text()
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
const fs = require('fs')
|
|
||||||
const http = require('http')
|
|
||||||
|
|
||||||
const requestListener = async function (req, res) {
|
|
||||||
res.writeHead(200);
|
|
||||||
fs.readFile('index.html', (err, data) => {
|
|
||||||
res.end(data);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = http.createServer(requestListener)
|
|
||||||
server.listen(5050)
|
|
||||||
9
frontend/node_modules/fs/README.md
generated
vendored
9
frontend/node_modules/fs/README.md
generated
vendored
@@ -1,9 +0,0 @@
|
|||||||
# Security holding package
|
|
||||||
|
|
||||||
This package name is not currently in use, but was formerly occupied
|
|
||||||
by another package. To avoid malicious use, npm is hanging on to the
|
|
||||||
package name, but loosely, and we'll probably give it to you if you
|
|
||||||
want it.
|
|
||||||
|
|
||||||
You may adopt this package by contacting support@npmjs.com and
|
|
||||||
requesting the name.
|
|
||||||
46
frontend/node_modules/fs/package.json
generated
vendored
46
frontend/node_modules/fs/package.json
generated
vendored
@@ -1,46 +0,0 @@
|
|||||||
{
|
|
||||||
"_from": "fs",
|
|
||||||
"_id": "fs@0.0.1-security",
|
|
||||||
"_inBundle": false,
|
|
||||||
"_integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==",
|
|
||||||
"_location": "/fs",
|
|
||||||
"_phantomChildren": {},
|
|
||||||
"_requested": {
|
|
||||||
"type": "tag",
|
|
||||||
"registry": true,
|
|
||||||
"raw": "fs",
|
|
||||||
"name": "fs",
|
|
||||||
"escapedName": "fs",
|
|
||||||
"rawSpec": "",
|
|
||||||
"saveSpec": null,
|
|
||||||
"fetchSpec": "latest"
|
|
||||||
},
|
|
||||||
"_requiredBy": [
|
|
||||||
"#USER",
|
|
||||||
"/"
|
|
||||||
],
|
|
||||||
"_resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
|
|
||||||
"_shasum": "8a7bd37186b6dddf3813f23858b57ecaaf5e41d4",
|
|
||||||
"_spec": "fs",
|
|
||||||
"_where": "C:\\Users\\user\\Desktop\\something\\frontend",
|
|
||||||
"author": "",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/npm/security-holder/issues"
|
|
||||||
},
|
|
||||||
"bundleDependencies": false,
|
|
||||||
"deprecated": false,
|
|
||||||
"description": "This package name is not currently in use, but was formerly occupied by another package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.",
|
|
||||||
"homepage": "https://github.com/npm/security-holder#readme",
|
|
||||||
"keywords": [],
|
|
||||||
"license": "ISC",
|
|
||||||
"main": "index.js",
|
|
||||||
"name": "fs",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "git+https://github.com/npm/security-holder.git"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"version": "0.0.1-security"
|
|
||||||
}
|
|
||||||
9
frontend/node_modules/http/README.md
generated
vendored
9
frontend/node_modules/http/README.md
generated
vendored
@@ -1,9 +0,0 @@
|
|||||||
# Security holding package
|
|
||||||
|
|
||||||
This package name is not currently in use, but was formerly occupied
|
|
||||||
by another package. To avoid malicious use, npm is hanging on to the
|
|
||||||
package name, but loosely, and we'll probably give it to you if you
|
|
||||||
want it.
|
|
||||||
|
|
||||||
You may adopt this package by contacting support@npmjs.com and
|
|
||||||
requesting the name.
|
|
||||||
39
frontend/node_modules/http/package.json
generated
vendored
39
frontend/node_modules/http/package.json
generated
vendored
@@ -1,39 +0,0 @@
|
|||||||
{
|
|
||||||
"_from": "http",
|
|
||||||
"_id": "http@0.0.1-security",
|
|
||||||
"_inBundle": false,
|
|
||||||
"_integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g==",
|
|
||||||
"_location": "/http",
|
|
||||||
"_phantomChildren": {},
|
|
||||||
"_requested": {
|
|
||||||
"type": "tag",
|
|
||||||
"registry": true,
|
|
||||||
"raw": "http",
|
|
||||||
"name": "http",
|
|
||||||
"escapedName": "http",
|
|
||||||
"rawSpec": "",
|
|
||||||
"saveSpec": null,
|
|
||||||
"fetchSpec": "latest"
|
|
||||||
},
|
|
||||||
"_requiredBy": [
|
|
||||||
"#USER",
|
|
||||||
"/"
|
|
||||||
],
|
|
||||||
"_resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz",
|
|
||||||
"_shasum": "3aac09129d12dc2747bbce4157afde20ad1f7995",
|
|
||||||
"_spec": "http",
|
|
||||||
"_where": "C:\\Users\\user\\Desktop\\something\\frontend",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/npm/security-holder/issues"
|
|
||||||
},
|
|
||||||
"bundleDependencies": false,
|
|
||||||
"deprecated": false,
|
|
||||||
"description": "security holding package",
|
|
||||||
"homepage": "https://github.com/npm/security-holder#readme",
|
|
||||||
"name": "http",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "git+https://github.com/npm/security-holder.git"
|
|
||||||
},
|
|
||||||
"version": "0.0.1-security"
|
|
||||||
}
|
|
||||||
18
frontend/package-lock.json
generated
18
frontend/package-lock.json
generated
@@ -1,18 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "frontend",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"lockfileVersion": 1,
|
|
||||||
"requires": true,
|
|
||||||
"dependencies": {
|
|
||||||
"fs": {
|
|
||||||
"version": "0.0.1-security",
|
|
||||||
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
|
|
||||||
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w=="
|
|
||||||
},
|
|
||||||
"http": {
|
|
||||||
"version": "0.0.1-security",
|
|
||||||
"resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz",
|
|
||||||
"integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g=="
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "frontend",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"dev": "node index.js"
|
|
||||||
},
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"fs": "0.0.1-security",
|
|
||||||
"http": "0.0.1-security"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
19
lab4-vue-front/README.md
Normal file
19
lab4-vue-front/README.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# lab4-vue-front
|
||||||
|
|
||||||
|
## Project setup
|
||||||
|
```
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
### Compiles and hot-reloads for development
|
||||||
|
```
|
||||||
|
npm run serve
|
||||||
|
```
|
||||||
|
|
||||||
|
### Compiles and minifies for production
|
||||||
|
```
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Customize configuration
|
||||||
|
See [Configuration Reference](https://cli.vuejs.org/config/).
|
||||||
5
lab4-vue-front/babel.config.js
Normal file
5
lab4-vue-front/babel.config.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
presets: [
|
||||||
|
'@vue/cli-plugin-babel/preset'
|
||||||
|
]
|
||||||
|
}
|
||||||
19
lab4-vue-front/jsconfig.json
Normal file
19
lab4-vue-front/jsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es5",
|
||||||
|
"module": "esnext",
|
||||||
|
"baseUrl": "./",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"paths": {
|
||||||
|
"@/*": [
|
||||||
|
"src/*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"lib": [
|
||||||
|
"esnext",
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"scripthost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
17576
lab4-vue-front/package-lock.json
generated
Normal file
17576
lab4-vue-front/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
lab4-vue-front/package.json
Normal file
29
lab4-vue-front/package.json
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "lab4_vue_front",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"serve": "vue-cli-service serve",
|
||||||
|
"build": "vue-cli-service build"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@popperjs/core": "^2.11.7",
|
||||||
|
"axios": "^1.3.4",
|
||||||
|
"core-js": "^3.8.3",
|
||||||
|
"vue": "^3.2.13",
|
||||||
|
"vue-router": "^4.1.6",
|
||||||
|
"vuex": "^4.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@vue/cli-plugin-babel": "~5.0.0",
|
||||||
|
"@vue/cli-plugin-router": "~5.0.0",
|
||||||
|
"@vue/cli-plugin-vuex": "~5.0.0",
|
||||||
|
"@vue/cli-service": "~5.0.0"
|
||||||
|
},
|
||||||
|
"browserslist": [
|
||||||
|
"> 1%",
|
||||||
|
"last 2 versions",
|
||||||
|
"not dead",
|
||||||
|
"not ie 11"
|
||||||
|
]
|
||||||
|
}
|
||||||
15
lab4-vue-front/public/index.html
Normal file
15
lab4-vue-front/public/index.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||||
|
<title>LabWork04 - Social Network</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
|
||||||
|
<div id="app"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
48
lab4-vue-front/src/App.vue
Normal file
48
lab4-vue-front/src/App.vue
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<p class='text-center m-3 h3'>LabWork04 - Social Network</p>
|
||||||
|
</div>
|
||||||
|
<div class="nav row">
|
||||||
|
<div class="nav-left col-4">
|
||||||
|
<router-link to="/customers" class="text-decoration-none m-3">Профили</router-link>
|
||||||
|
<router-link to="/posts" class="text-decoration-none m-3">Посты</router-link>
|
||||||
|
<select class="form-select mt-4" style="font-size: 16px; max-height: 35px; max-width: 180px;" v-model="currentCustomerId">
|
||||||
|
<option value="-1" selected class="button dark outline" :on-click="updateCurrentCustomer()">Не выбран</option>
|
||||||
|
<option v-for="customer in customers" v-bind:value="customer['id']" class="button dark outline" :on-click="updateCurrentCustomer()">{{ customer['username'] }}</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<router-view></router-view>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import axios from 'axios'
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentCustomerId: -1,
|
||||||
|
customers: []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async updateCurrentCustomer() {
|
||||||
|
history.replaceState(this.currentCustomerId, "");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async beforeMount() {
|
||||||
|
setInterval(async () => {
|
||||||
|
const response = await axios.get('http://localhost:8080/api/customer');
|
||||||
|
this.customers = [];
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}, 500)
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="">
|
||||||
|
|
||||||
|
</style>
|
||||||
162
lab4-vue-front/src/components/Customers.vue
Normal file
162
lab4-vue-front/src/components/Customers.vue
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
<template>
|
||||||
|
<div class="ms-5">
|
||||||
|
<p class='h4 m-3'>Пользователи</p>
|
||||||
|
<button type='button' class="btn btn-primary ms-3 mb-3" data-bs-toggle="modal" data-bs-target="#customerCreate">
|
||||||
|
Добавить нового пользователя
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<p class='h4 ms-3'>Список</p>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-5">
|
||||||
|
<div v-for="customer in customers" class="row card is-left mb-3">
|
||||||
|
<div class="row is-left">
|
||||||
|
<div class="row is-left h3">{{ customer['username'] }}</div>
|
||||||
|
</div>
|
||||||
|
<p class="row is-left"></p>
|
||||||
|
|
||||||
|
<div class="row" v-if="!(customer['posts'].length == 0)">
|
||||||
|
<p class="h4">Посты:</p>
|
||||||
|
<div class="col">
|
||||||
|
<div v-for="post in customer['posts']" class="row is-left card mb-3">
|
||||||
|
<div class="row is-left h4">{{ post['title'] }}</div>
|
||||||
|
<div class="row is-left h5">{{ post['content'] }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="row is-left" v-if="!(customer['comments'].length == 0)">
|
||||||
|
<p class="h4">Комментарии:</p>
|
||||||
|
<div class="col">
|
||||||
|
<div v-for="comment in customer['comments']" class="row is-left card mb-3">
|
||||||
|
<div class="row is-left h5">"{{ comment['content'] }}" - к посту '{{ comment['postTitle'] }}' от пользователя {{ comment['postAuthor'] }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" v-if="customer['id'] == currentCustomerId">
|
||||||
|
<button v-on:click="deleteUser(customer['id'])" class="col button dark outline">Удалить</button>
|
||||||
|
<button v-on:click="selectedCustomer = customer; usernameModal=customer['username'];passwordModal=customer['password']" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#customerEdit">Редактировать</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="customerCreate" tabindex="-1" role="dialog" aria-labelledby="customerCreateLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="customerCreateLabel">Создать пользователя</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Имя пользователя</p>
|
||||||
|
<textarea v-model='usernameModal' id="usernameText" cols="30" rows="1"></textarea>
|
||||||
|
<p>Пароль</p>
|
||||||
|
<textarea v-model='passwordModal' id="passwordText" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
|
||||||
|
<button type="button" class="btn btn-primary" v-on:click='createUser'>Создать</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="customerEdit" tabindex="-1" role="dialog" aria-labelledby="customerEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="customerEditLabel">Изменение пользователя</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Имя пользователя</p>
|
||||||
|
<textarea v-model='usernameModal' id="usernameText" cols="30" rows="1"></textarea>
|
||||||
|
<p>Пароль</p>
|
||||||
|
<textarea v-model='passwordModal' id="passwordText" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
|
||||||
|
<button type="button" class="btn btn-primary" v-on:click='editUser'>Применить</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
customers: [],
|
||||||
|
usernameModal: '',
|
||||||
|
passwordModal: '',
|
||||||
|
selectedCustomer: {},
|
||||||
|
currentCustomerId: -1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async createUser(){
|
||||||
|
let customer = {
|
||||||
|
username: this.usernameModal,
|
||||||
|
password: this.passwordModal
|
||||||
|
};
|
||||||
|
const response = await axios.post('http://localhost:8080/api/customer', customer);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async deleteUser(id) {
|
||||||
|
const response = await axios.delete('http://localhost:8080/api/customer/' + id);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async editUser() {
|
||||||
|
let customer = {
|
||||||
|
username: this.usernameModal,
|
||||||
|
password: this.passwordModal
|
||||||
|
};
|
||||||
|
const response = await axios.put('http://localhost:8080/api/customer/' + this.selectedCustomer['id'], customer);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async refreshList() {
|
||||||
|
this.customers = [];
|
||||||
|
if (this.$route.params.id === "") {
|
||||||
|
const response = await axios.get('http://localhost:8080/api/customer');
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const response = await axios.get('http://localhost:8080/api/customer/' + this.$route.params.id);
|
||||||
|
this.customers.push(response.data)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
},
|
||||||
|
async mounted() {
|
||||||
|
this.currentCustomerId = history.state;
|
||||||
|
setInterval(async () => this.currentCustomerId = history.state, 50)
|
||||||
|
if (this.$route.params.id === "") {
|
||||||
|
const response = await axios.get('http://localhost:8080/api/customer');
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const response = await axios.get('http://localhost:8080/api/customer/' + this.$route.params.id);
|
||||||
|
this.customers.push(response.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
async beforeRouteUpdate(to, from) {
|
||||||
|
this.$route.params.id = to.params.id;
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="">
|
||||||
|
|
||||||
|
</style>
|
||||||
295
lab4-vue-front/src/components/Posts.vue
Normal file
295
lab4-vue-front/src/components/Posts.vue
Normal file
@@ -0,0 +1,295 @@
|
|||||||
|
<template>
|
||||||
|
<div class="ms-5">
|
||||||
|
<div class="col-7">
|
||||||
|
<div class="row">
|
||||||
|
<input type="text" v-bind:id="'searchString'" class="col-4"/>
|
||||||
|
<button type="button" v-on:click="getSearchResult()" class="button col-2 secondary outline">Поиск</button>
|
||||||
|
<button type="button" v-on:click="searchMode = -1;" class="button col-2 secondary outline">Отмена</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!(searchMode == -1)">
|
||||||
|
<div v-if="!(searchResults.length == 0)" class="row">
|
||||||
|
<div class="col-5">
|
||||||
|
<div class="row mb-5 card" v-for="result in searchResults">
|
||||||
|
<div class="col">
|
||||||
|
|
||||||
|
<div v-if="result['title'] == null">
|
||||||
|
<div class="row is-left mt-2">
|
||||||
|
<span class="h3">Комментарий</span>
|
||||||
|
<!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
|
||||||
|
<span class="h3">Контент: {{ result['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div v-else>
|
||||||
|
<div class="row is-left mt-2">
|
||||||
|
<span class="h3">Пост</span>
|
||||||
|
<!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
|
||||||
|
<span class="h3">Оглавление: {{ result['title'] }}</span>
|
||||||
|
<span class="h3">Контент: {{ result['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="searchMode == -1">
|
||||||
|
<p class='h3 m-3'>Посты</p>
|
||||||
|
|
||||||
|
<div class="row" v-if="currentCustomerId != -1">
|
||||||
|
<div class="col-7">
|
||||||
|
<div class="row is-left">
|
||||||
|
<p class="col-2 is-left mb-2">Заголовок:</p>
|
||||||
|
<input type="text" class="col-5" v-model="titleModal"/>
|
||||||
|
</div>
|
||||||
|
<div class="row is-left">
|
||||||
|
<p class="col-2 is-left mb-2">Текст:</p>
|
||||||
|
<textarea type="textarea" class="col-5" v-model="postContentModal"/>
|
||||||
|
</div>
|
||||||
|
<div class="row is-left">
|
||||||
|
<button type='button' class="button btn-primary col-7" v-on:click='createPost'>
|
||||||
|
Опубликовать
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!(posts.length == 0)" class="row">
|
||||||
|
<div class="col-5">
|
||||||
|
<div class="row mb-5 card" v-for="post in posts">
|
||||||
|
<div class="col">
|
||||||
|
<div class="row is-left my-3">
|
||||||
|
<p class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row text-left">
|
||||||
|
<span class="h2">{{ post['title'] }}</span>
|
||||||
|
<span class="h3">{{ post['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div v-if="!(post['comments'].length == 0)">
|
||||||
|
<p class="row h3 is-left my-3">Комментарии:</p>
|
||||||
|
|
||||||
|
<div class="row text-left mb-4 card" v-for="comment in post['comments']">
|
||||||
|
<div class="row is-left">
|
||||||
|
<span class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></span>
|
||||||
|
<span class="h3">{{ comment['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="selectedCustomerContainsComment(comment) == true" class="row mt-3">
|
||||||
|
<button v-on:click="selectedCommentId = comment['id']; selectedPostTitle = post['title']" class="button primary outline col" data-bs-toggle="modal" data-bs-target="#commentEdit">Изменить</button>
|
||||||
|
<button v-on:click="deleteComment(comment['id'])" class="button error col">Удалить</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" v-if="currentCustomerId != -1">
|
||||||
|
<input type="text" v-bind:id="'post-comment-' + post['id']" class="col-7"/>
|
||||||
|
<button type="button" v-on:click="selectedPostId = post['id']; selectedPostTitle = post['title']; createComment()" class="button col-5 secondary outline">Комментировать</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" v-if="post['customerId'] == currentCustomerId">
|
||||||
|
<button type="button" v-on:click="deletePost(post['id'])" class="col button dark outline">Удалить пост</button>
|
||||||
|
<button type="button" v-on:click="selectedPostId = post['id']" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#postEdit">Изменить пост</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="postEdit" tabindex="-1" role="dialog" aria-labelledby="postEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="postEditLabel">Редактировать пост</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>{{ currentCustomer['username'] }}</p>
|
||||||
|
<p>Заголовок</p>
|
||||||
|
<textarea v-model='titleModal' id="editModalTitle" cols="30" rows="1"></textarea>
|
||||||
|
<p>Содержание</p>
|
||||||
|
<textarea v-model='postContentModal' id="editModalPostContent" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="button" class="btn btn-primary" v-on:click='editPost'>Изменить</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="commentEdit" tabindex="-1" role="dialog" aria-labelledby="commentEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="commentEditLabel">Изменить комментарий</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Под именем:</p>
|
||||||
|
<p>{{ currentCustomer['username'] }}</p>
|
||||||
|
<p>К посту:</p>
|
||||||
|
<p>{{ selectedPostTitle }}</p>
|
||||||
|
<p>Комментарий</p>
|
||||||
|
<textarea v-model='contentModal' id="editModalText" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="button" class="btn btn-primary" v-on:click='editComment'>Изменить</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import axios from 'axios'
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
searchResults: [],
|
||||||
|
customers: [],
|
||||||
|
selectedCustomerId: 0,
|
||||||
|
currentCustomer: {},
|
||||||
|
posts: [],
|
||||||
|
selectedPostId: 0,
|
||||||
|
selectedPostTitle: '',
|
||||||
|
contentModal: '',
|
||||||
|
titleModal: '',
|
||||||
|
postContentModal: '',
|
||||||
|
selectedCommentId: 0,
|
||||||
|
currentCustomerId: -1,
|
||||||
|
searchMode: -1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async getSearchResult(){
|
||||||
|
const content = document.getElementById("searchString").value
|
||||||
|
if (content != ''){
|
||||||
|
this.searchMode = 0;
|
||||||
|
this.searchResults = [];
|
||||||
|
const responseSearch = await axios.post('http://localhost:8080/search?searchStr=' + content);
|
||||||
|
responseSearch.data.forEach(element => {
|
||||||
|
this.searchResults.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.searchMode = -1;
|
||||||
|
this.refreshList();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
selectedCustomerContainsComment(comment) {
|
||||||
|
var customer = this.customers.find(element => element['id'] == this.currentCustomerId);
|
||||||
|
console.log(customer);
|
||||||
|
if (customer == null) return false;
|
||||||
|
var flag = false;
|
||||||
|
customer['comments'].forEach(commentIn => {
|
||||||
|
if (commentIn['id'] == comment['id']) {
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return flag;
|
||||||
|
},
|
||||||
|
|
||||||
|
async createComment() {
|
||||||
|
const content = document.getElementById("post-comment-" + this.selectedPostId).value
|
||||||
|
let comment = {
|
||||||
|
"content": content,
|
||||||
|
customerId: this.currentCustomerId,
|
||||||
|
postId: this.selectedPostId
|
||||||
|
};
|
||||||
|
await axios.post('http://localhost:8080/api/comment', comment);
|
||||||
|
document.getElementById("post-comment-" + this.selectedPostId).value = ''
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async deleteComment(commentId) {
|
||||||
|
await axios.delete('http://localhost:8080/api/comment/' + commentId);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async editComment(){
|
||||||
|
let comment = {
|
||||||
|
content: this.contentModal,
|
||||||
|
customerId: this.currentCustomerId,
|
||||||
|
postId: 0
|
||||||
|
};
|
||||||
|
await axios.put('http://localhost:8080/api/comment/' + this.selectedCommentId, comment);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async createPost() {
|
||||||
|
let post = {
|
||||||
|
"content": this.postContentModal,
|
||||||
|
title: this.titleModal,
|
||||||
|
customerId: this.currentCustomerId
|
||||||
|
};
|
||||||
|
const response = await axios.post('http://localhost:8080/api/post', post);
|
||||||
|
this.titleModal = ''
|
||||||
|
this.postContentModal = ''
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async deletePost(postId) {
|
||||||
|
const response = await axios.delete('http://localhost:8080/api/post/' + postId);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async editPost(){
|
||||||
|
let post = {
|
||||||
|
"content": this.postContentModal,
|
||||||
|
title: this.titleModal,
|
||||||
|
customerId: this.currentCustomerId
|
||||||
|
};
|
||||||
|
const response = await axios.put('http://localhost:8080/api/post/' + this.selectedPostId, post);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
|
||||||
|
async refreshList(){
|
||||||
|
this.customers = [];
|
||||||
|
this.posts = [];
|
||||||
|
const responseCustomer = await axios.get('http://localhost:8080/api/customer');
|
||||||
|
responseCustomer.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
const responsePost = await axios.get('http://localhost:8080/api/post');
|
||||||
|
responsePost.data.forEach(element => {
|
||||||
|
this.posts.splice(0, 0, element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async beforeMount() {
|
||||||
|
this.currentCustomerId = history.state;
|
||||||
|
setInterval(async () => this.currentCustomerId = history.state, 50)
|
||||||
|
const responseCustomer = await axios.get('http://localhost:8080/api/customer');
|
||||||
|
responseCustomer.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
if (element['id'] == this.currentCustomerId) {
|
||||||
|
this.currentCustomer = element;
|
||||||
|
}
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
const responsePost = await axios.get('http://localhost:8080/api/post');
|
||||||
|
responsePost.data.forEach(element => {
|
||||||
|
this.posts.splice(0, 0, element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="">
|
||||||
|
|
||||||
|
</style>
|
||||||
29
lab4-vue-front/src/main.js
Normal file
29
lab4-vue-front/src/main.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import {createApp} from 'vue'
|
||||||
|
import App from './App'
|
||||||
|
import { createRouter, createWebHistory } from "vue-router"
|
||||||
|
|
||||||
|
import Customers from './components/Customers'
|
||||||
|
import Posts from './components/Posts'
|
||||||
|
|
||||||
|
const routes = [
|
||||||
|
{
|
||||||
|
path: '/customers/:id?',
|
||||||
|
name: "Customers",
|
||||||
|
component: Customers,
|
||||||
|
props: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/posts',
|
||||||
|
name: "Posts",
|
||||||
|
component: Posts,
|
||||||
|
props: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
const router = createRouter({
|
||||||
|
routes,
|
||||||
|
history: createWebHistory()
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
createApp(App).use(router).mount('#app')
|
||||||
4
lab4-vue-front/vue.config.js
Normal file
4
lab4-vue-front/vue.config.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
const { defineConfig } = require('@vue/cli-service')
|
||||||
|
module.exports = defineConfig({
|
||||||
|
transpileDependencies: true
|
||||||
|
})
|
||||||
94
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java
Normal file
94
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.DTO;
|
||||||
|
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
public class CommentDto {
|
||||||
|
public long id;
|
||||||
|
public String content;
|
||||||
|
public long customerId;
|
||||||
|
public String customerName;
|
||||||
|
public long postId;
|
||||||
|
public String postTitle;
|
||||||
|
public String postAuthor;
|
||||||
|
public long postAuthorId;
|
||||||
|
|
||||||
|
public CommentDto() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommentDto(Comment comment) {
|
||||||
|
this.id = comment.getId();
|
||||||
|
this.content = comment.getContent();
|
||||||
|
this.customerId = comment.getCustomer().getId();
|
||||||
|
this.customerName = comment.getCustomer().getUsername();
|
||||||
|
this.postId = comment.getPost().getId();
|
||||||
|
this.postTitle = comment.getPost().getTitle();
|
||||||
|
this.postAuthor = comment.getPost().getCustomer().getUsername();
|
||||||
|
this.postAuthorId = comment.getPost().getCustomer().getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCustomerId() {
|
||||||
|
return customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerId(long customerId) {
|
||||||
|
this.customerId = customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getPostId() {
|
||||||
|
return postId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPostId(long postId) {
|
||||||
|
this.postId = postId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public String getPostTitle() {
|
||||||
|
return postTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public String getPostAuthor() {
|
||||||
|
return postAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public long getPostAuthorId() {
|
||||||
|
return postAuthorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
CommentDto that = (CommentDto) o;
|
||||||
|
|
||||||
|
return id == that.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return (int) (id ^ (id >>> 32));
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java
Normal file
56
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.DTO;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CustomerDto {
|
||||||
|
public long id;
|
||||||
|
public String username;
|
||||||
|
public String password;
|
||||||
|
public List<CommentDto> comments;
|
||||||
|
public List<PostDto> posts;
|
||||||
|
|
||||||
|
public CustomerDto() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomerDto(Customer customer){
|
||||||
|
this.id = customer.getId();
|
||||||
|
this.username = customer.getUsername();
|
||||||
|
this.password = customer.getPassword();
|
||||||
|
this.comments = customer.getComments().stream().map(CommentDto::new).toList();
|
||||||
|
this.posts = customer.getPosts().stream().map(PostDto::new).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public List<CommentDto> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public List<PostDto> getPosts() {
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
}
|
||||||
67
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java
Normal file
67
src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.DTO;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Post;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PostDto {
|
||||||
|
public long id;
|
||||||
|
public String title;
|
||||||
|
public String content;
|
||||||
|
public String customerName;
|
||||||
|
public long customerId;
|
||||||
|
public ArrayList<CommentDto> comments;
|
||||||
|
|
||||||
|
public PostDto() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostDto(Post post){
|
||||||
|
this.id = post.getId();
|
||||||
|
this.title = post.getTitle();
|
||||||
|
this.content = post.getContent();
|
||||||
|
this.customerName = post.getCustomer().getUsername();
|
||||||
|
this.customerId = post.getCustomer().getId();
|
||||||
|
this.comments = new ArrayList<>(post.getComments().stream().map(CommentDto::new).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public List<CommentDto> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCustomerId() {
|
||||||
|
return customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerId(long customerId) {
|
||||||
|
this.customerId = customerId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.controller;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CommentDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.PostService;
|
||||||
|
import ru.ulstu.is.labwork.WebConfiguration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(WebConfiguration.REST_API + "/comment")
|
||||||
|
public class CommentController {
|
||||||
|
private final CommentService commentService;
|
||||||
|
private final CustomerService customerService;
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
public CommentController(CommentService commentService, CustomerService customerService, PostService postService) {
|
||||||
|
this.commentService = commentService;
|
||||||
|
this.customerService = customerService;
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public CommentDto getComment(@PathVariable Long id) {
|
||||||
|
return new CommentDto(commentService.findComment(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<CommentDto> getComments() {
|
||||||
|
return commentService.findAllComments().stream()
|
||||||
|
.map(CommentDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public CommentDto createComment(@RequestBody @Valid CommentDto commentDto){
|
||||||
|
final Comment comment = commentService.addComment(
|
||||||
|
customerService.findCustomer(commentDto.getCustomerId()),
|
||||||
|
postService.findPost(commentDto.getPostId()),
|
||||||
|
commentDto.getContent()
|
||||||
|
);
|
||||||
|
return new CommentDto(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CommentDto updateComment(@RequestBody @Valid CommentDto commentDto, @PathVariable Long id) {
|
||||||
|
return new CommentDto(commentService.updateComment(id, commentDto.getContent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CommentDto deleteComment(@PathVariable Long id) {
|
||||||
|
return new CommentDto(commentService.deleteComment(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllComments(){
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.controller;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CustomerDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.WebConfiguration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(WebConfiguration.REST_API + "/customer")
|
||||||
|
public class CustomerController {
|
||||||
|
private final CustomerService customerService;
|
||||||
|
|
||||||
|
public CustomerController(CustomerService customerService) {
|
||||||
|
this.customerService = customerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public CustomerDto getCustomer(@PathVariable Long id) {
|
||||||
|
return new CustomerDto(customerService.findCustomer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<CustomerDto> getCustomers() {
|
||||||
|
return customerService.findAllCustomers().stream()
|
||||||
|
.map(CustomerDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public CustomerDto createCustomer(@RequestBody @Valid CustomerDto customerDto){
|
||||||
|
final Customer customer = customerService.addCustomer(customerDto.getUsername(), customerDto.getPassword());
|
||||||
|
return new CustomerDto(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CustomerDto updateCustomer(@RequestBody @Valid CustomerDto customerDto, @PathVariable Long id) {
|
||||||
|
return new CustomerDto(customerService.updateCustomer(id, customerDto.getUsername(), customerDto.getPassword()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CustomerDto deleteCustomer(@PathVariable Long id) {
|
||||||
|
return new CustomerDto(customerService.deleteCustomer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllCustomers(){
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.controller;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CommentDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.PostDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Post;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.PostService;
|
||||||
|
import ru.ulstu.is.labwork.WebConfiguration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(WebConfiguration.REST_API + "/post")
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
private final CustomerService customerService;
|
||||||
|
|
||||||
|
public PostController(PostService postService, CustomerService customerService) {
|
||||||
|
this.postService = postService;
|
||||||
|
this.customerService = customerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public PostDto getPost(@PathVariable Long id) {
|
||||||
|
return new PostDto(postService.findPost(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<PostDto> getPosts() {
|
||||||
|
return postService.findAllPosts().stream()
|
||||||
|
.map(PostDto::new)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public PostDto createPost(@RequestBody @Valid PostDto postDto) {
|
||||||
|
return new PostDto(postService.addPost(customerService.findCustomer(postDto.getCustomerId()), postDto.getTitle(), postDto.getContent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public PostDto updatePost(@RequestBody @Valid PostDto postDto, @PathVariable Long id) {
|
||||||
|
return new PostDto(postService.updatePost(id, postDto.title, postDto.content));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public PostDto deletePost (@PathVariable Long id) {
|
||||||
|
return new PostDto(postService.deletePost(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllPosts() {
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
}
|
||||||
|
}
|
||||||
67
src/main/java/ru/ulstu/is/labwork/Lab4/model/Comment.java
Normal file
67
src/main/java/ru/ulstu/is/labwork/Lab4/model/Comment.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Comment {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="customer_fk")
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="post_fk")
|
||||||
|
private Post post;
|
||||||
|
|
||||||
|
public Comment() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Comment(Customer customer, Post post, String content) {
|
||||||
|
this.customer = customer;
|
||||||
|
this.post = post;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post getPost() {
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Comment comment = (Comment) o;
|
||||||
|
|
||||||
|
return Objects.equals(id, comment.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/main/java/ru/ulstu/is/labwork/Lab4/model/Customer.java
Normal file
72
src/main/java/ru/ulstu/is/labwork/Lab4/model/Customer.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Customer {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
private String username;
|
||||||
|
@Column
|
||||||
|
private String password;
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
|
private List<Comment> comments;
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
|
private List<Post> posts;
|
||||||
|
|
||||||
|
public Customer() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.comments = new ArrayList<>();
|
||||||
|
this.posts = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Comment> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Post> getPosts() {
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
Customer customer = (Customer) obj;
|
||||||
|
return Objects.equals(id, customer.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
79
src/main/java/ru/ulstu/is/labwork/Lab4/model/Post.java
Normal file
79
src/main/java/ru/ulstu/is/labwork/Lab4/model/Post.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Post {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name = "customer_fk")
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL)
|
||||||
|
private List<Comment> comments;
|
||||||
|
|
||||||
|
public Post() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post(Customer customer, String title, String content) {
|
||||||
|
this.customer = customer;
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
this.comments = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Post post = (Post) o;
|
||||||
|
|
||||||
|
return id.equals(post.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return id.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Comment> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java
Normal file
55
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.mvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CommentDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.PostDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.PostService;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/comments")
|
||||||
|
public class Comments {
|
||||||
|
private final CustomerService customerService;
|
||||||
|
private final CommentService commentService;
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
public Comments(CustomerService customerService, CommentService commentService, PostService postService) {
|
||||||
|
this.customerService = customerService;
|
||||||
|
this.commentService = commentService;
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = { "/", "/{id}"})
|
||||||
|
public String manipulateComment(@PathVariable(required = false) Long id, @ModelAttribute @Valid CommentDto commentDto,
|
||||||
|
HttpServletRequest request, HttpSession session, BindingResult bindingResult, Model model) {
|
||||||
|
model.addAttribute("request", request);
|
||||||
|
model.addAttribute("session", session);
|
||||||
|
model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList());
|
||||||
|
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||||
|
return "/feed";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == null || id <= 0) {
|
||||||
|
commentService.addComment(customerService.findCustomer(commentDto.customerId), postService.findPost(commentDto.postId), commentDto.content);
|
||||||
|
} else {
|
||||||
|
commentService.updateComment(id, commentDto.content);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "redirect:/feed";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String deleteComment(@PathVariable Long id) {
|
||||||
|
commentService.deleteComment(id);
|
||||||
|
return "redirect:/feed";
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java
Normal file
62
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.mvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CustomerDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/customers")
|
||||||
|
public class Customers {
|
||||||
|
private final CustomerService customerService;
|
||||||
|
|
||||||
|
public Customers(CustomerService customerService) {
|
||||||
|
this.customerService = customerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = {"", "/", "/{id}" })
|
||||||
|
public String getCustomers(@PathVariable(required = false) Long id, HttpServletRequest request, HttpSession session, Model model) {
|
||||||
|
model.addAttribute("request", request);
|
||||||
|
model.addAttribute("session", session);
|
||||||
|
if (id == null || id <= 0) {
|
||||||
|
model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList());
|
||||||
|
} else {
|
||||||
|
model.addAttribute("customers", new CustomerDto[] { new CustomerDto(customerService.findCustomer(id)) });
|
||||||
|
}
|
||||||
|
|
||||||
|
return "customers";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String deleteCustomer(@PathVariable Long id, HttpSession session) {
|
||||||
|
session.setAttribute("currentCustomerId", -1);
|
||||||
|
customerService.deleteCustomer(id);
|
||||||
|
return "redirect:/customers/";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = { "/", "/{id}"})
|
||||||
|
public String manipulateCustomer(@PathVariable(required = false) Long id, @ModelAttribute @Valid CustomerDto customerDto,
|
||||||
|
HttpServletRequest request, HttpSession session,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute("request", request);
|
||||||
|
model.addAttribute("session", session);
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||||
|
return "/customers";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == null || id <= 0) {
|
||||||
|
customerService.addCustomer(customerDto.username, customerDto.password);
|
||||||
|
} else {
|
||||||
|
customerService.updateCustomer(id, customerDto.username, customerDto.password);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "redirect:/customers/";
|
||||||
|
}
|
||||||
|
}
|
||||||
50
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java
Normal file
50
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.mvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.CustomerDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.PostDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.PostService;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/feed")
|
||||||
|
public class Feed {
|
||||||
|
private final PostService postService;
|
||||||
|
private final CustomerService customerService;
|
||||||
|
private final CommentService commentService;
|
||||||
|
|
||||||
|
public Feed(PostService postService, CustomerService customerService, CommentService commentService) {
|
||||||
|
this.postService = postService;
|
||||||
|
this.customerService = customerService;
|
||||||
|
this.commentService = commentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getPosts(@RequestParam(required = false) String search,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpSession session,
|
||||||
|
@RequestParam(defaultValue = "0") int page,
|
||||||
|
@RequestParam(defaultValue = "5") int size,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute("request", request);
|
||||||
|
model.addAttribute("session", session);
|
||||||
|
if (search == null) {
|
||||||
|
model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList());
|
||||||
|
} else {
|
||||||
|
var posts = postService.searchPosts(page, size, search);
|
||||||
|
model.addAttribute("posts", posts.stream().map(PostDto::new).toList());
|
||||||
|
}
|
||||||
|
model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList());
|
||||||
|
|
||||||
|
return "/feed";
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java
Normal file
56
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.mvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.services.PostService;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.PostDto;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/posts")
|
||||||
|
public class Posts {
|
||||||
|
private final CustomerService customerService;
|
||||||
|
private final CommentService commentService;
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
public Posts(CustomerService customerService, CommentService commentService, PostService postService) {
|
||||||
|
this.customerService = customerService;
|
||||||
|
this.commentService = commentService;
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String deletePost(@PathVariable Long id) {
|
||||||
|
postService.deletePost(id);
|
||||||
|
return "redirect:/feed";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = { "/", "/{id}"})
|
||||||
|
public String manipulatePost(@PathVariable(required = false) Long id, @ModelAttribute @Valid PostDto postDto,
|
||||||
|
HttpServletRequest request, HttpSession session,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute("request", request);
|
||||||
|
model.addAttribute("session", session);
|
||||||
|
model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList());
|
||||||
|
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||||
|
return "/feed";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == null || id <= 0) {
|
||||||
|
postService.addPost(customerService.findCustomer(postDto.customerId), postDto.title, postDto.content);
|
||||||
|
} else {
|
||||||
|
postService.updatePost(id, postDto.title, postDto.content);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "redirect:/feed";
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java
Normal file
16
src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.mvc;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class Session {
|
||||||
|
@PostMapping("/update-session")
|
||||||
|
public ResponseEntity<Void> updateSession(@RequestParam("currentCustomerId") int currentCustomerId, HttpSession session) {
|
||||||
|
session.setAttribute("currentCustomerId", currentCustomerId);
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.repositories;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface CommentRepository extends JpaRepository<Comment, Long> {
|
||||||
|
@Query("SELECT DISTINCT c FROM Comment c WHERE c.content LIKE %:tag%")
|
||||||
|
Page<Comment> searchComments(@Param("tag") String tag, Pageable pageable);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.repositories;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
|
||||||
|
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.repositories;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Post;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||||
|
//@Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%")
|
||||||
|
//List<Post> searchPosts(@Param("tag") String tag);
|
||||||
|
@Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%")
|
||||||
|
Page<Post> searchPosts(@Param("tag") String tag, Pageable pageable);
|
||||||
|
|
||||||
|
//Page<Post> findDistinctByContentOrTitleLikeIgnoreCase
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.services;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Post;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.repositories.CommentRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CommentService {
|
||||||
|
private final CommentRepository commentRepository;
|
||||||
|
|
||||||
|
|
||||||
|
public CommentService(CommentRepository commentRepository) {
|
||||||
|
this.commentRepository = commentRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
//@jakarta.transaction.Transactional
|
||||||
|
//public List<Comment> searchComments(String tag) {
|
||||||
|
// return commentRepository.searchComments(tag, PageRequest.of(0,10)).stream().toList();
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment findComment(Long id) {
|
||||||
|
final Optional<Comment> comment = commentRepository.findById(id);
|
||||||
|
return comment.orElseThrow(EntityNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Comment> findAllComments() {
|
||||||
|
return commentRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment addComment(Customer customer, Post post, String content) {
|
||||||
|
if (customer == null || post == null) {
|
||||||
|
throw new IllegalArgumentException("Invalid customer or post");
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Invalid comment's content");
|
||||||
|
}
|
||||||
|
|
||||||
|
final Comment comment = new Comment(customer, post, content);
|
||||||
|
customer.getComments().add(comment);
|
||||||
|
post.getComments().add(comment);
|
||||||
|
return commentRepository.save(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment updateComment(Long id, String content) {
|
||||||
|
if (!StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Comment's content is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
final Comment currentComment = findComment(id);
|
||||||
|
currentComment.setContent(content);
|
||||||
|
return commentRepository.save(currentComment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment deleteComment(Long id) {
|
||||||
|
final Comment currentComment = findComment(id);
|
||||||
|
commentRepository.delete(currentComment);
|
||||||
|
currentComment.getPost().getComments().remove(currentComment);
|
||||||
|
currentComment.getCustomer().getComments().remove(currentComment);
|
||||||
|
return currentComment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllComments() {
|
||||||
|
commentRepository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.services;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.repositories.CustomerRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CustomerService {
|
||||||
|
private final CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
public CustomerService(CustomerRepository customerRepository) {
|
||||||
|
this.customerRepository = customerRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer findCustomer(Long id) {
|
||||||
|
final Optional<Customer> customer = customerRepository.findById(id);
|
||||||
|
return customer.orElseThrow(EntityNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Customer> findAllCustomers() {
|
||||||
|
return customerRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer addCustomer(String username, String password) {
|
||||||
|
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||||
|
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||||
|
}
|
||||||
|
Customer customer = new Customer(username, password);
|
||||||
|
return customerRepository.save(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer updateCustomer(Long id, String username, String password) {
|
||||||
|
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||||
|
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||||
|
}
|
||||||
|
Customer customer = findCustomer(id);
|
||||||
|
customer.setUsername(username);
|
||||||
|
customer.setPassword(password);
|
||||||
|
return customerRepository.save(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer deleteCustomer(Long id) {
|
||||||
|
Customer customer = findCustomer(id);
|
||||||
|
customerRepository.delete(customer);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCustomers() {
|
||||||
|
customerRepository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
104
src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java
Normal file
104
src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package ru.ulstu.is.labwork.Lab4.services;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.DTO.PostDto;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Customer;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.model.Post;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.repositories.CommentRepository;
|
||||||
|
import ru.ulstu.is.labwork.Lab4.repositories.PostRepository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostService {
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
private final CommentRepository commentRepository;
|
||||||
|
|
||||||
|
public PostService(PostRepository postRepository, CommentRepository commentRepository) {
|
||||||
|
this.postRepository = postRepository;
|
||||||
|
this.commentRepository = commentRepository;
|
||||||
|
}
|
||||||
|
@Transactional
|
||||||
|
public List<Post> searchPosts(int page, int size, String tag) {
|
||||||
|
//var posts = postRepository.searchPosts(tag);
|
||||||
|
var posts = new ArrayList<>(postRepository.searchPosts(tag, PageRequest.of(page, size)).getContent().stream().toList());
|
||||||
|
var comments = new ArrayList<>(commentRepository.searchComments(tag, PageRequest.of(page, size)).getContent().stream().toList());
|
||||||
|
for (var post: posts) {
|
||||||
|
post.getComments().clear();
|
||||||
|
}
|
||||||
|
for (var comment: comments) {
|
||||||
|
boolean found = false;
|
||||||
|
for (var post: posts) {
|
||||||
|
if (Objects.equals(comment.getPost().getId(), post.getId())) {
|
||||||
|
post.getComments().add(comment);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
var newPost = comment.getPost();
|
||||||
|
newPost.getComments().clear();
|
||||||
|
newPost.getComments().add(comment);
|
||||||
|
posts.add(newPost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post findPost(Long id) {
|
||||||
|
final Optional<Post> post = postRepository.findById(id);
|
||||||
|
return post.orElseThrow(EntityNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Post> findAllPosts() {
|
||||||
|
return postRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post addPost(Customer customer, String title, String content) {
|
||||||
|
if (customer == null) {
|
||||||
|
throw new IllegalArgumentException("Invalid customer");
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(title) | !StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Invalid post's content or title");
|
||||||
|
}
|
||||||
|
Post post = new Post(customer, title, content);
|
||||||
|
customer.getPosts().add(post);
|
||||||
|
return postRepository.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post updatePost(Long id, String title, String content) {
|
||||||
|
if (!StringUtils.hasText(content) | !StringUtils.hasText(title)) {
|
||||||
|
throw new IllegalArgumentException("Post's content or title is empty");
|
||||||
|
}
|
||||||
|
Post post = findPost(id);
|
||||||
|
post.setTitle(title);
|
||||||
|
post.setContent(content);
|
||||||
|
return postRepository.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post deletePost(Long id) {
|
||||||
|
Post post = findPost(id);
|
||||||
|
post.getCustomer().getPosts().remove(post);
|
||||||
|
postRepository.delete(post);
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllPosts() {
|
||||||
|
postRepository.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,44 +2,12 @@ package ru.ulstu.is.labwork;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
public class LabworkApplication {
|
public class LabworkApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(LabworkApplication.class, args);
|
SpringApplication.run(LabworkApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/good_morning")
|
|
||||||
public String good_morning(@RequestParam(value = "name", defaultValue = "Sir") String name) {
|
|
||||||
return String.format("Good morning, %s!", name);
|
|
||||||
}
|
|
||||||
@GetMapping("/sum")
|
|
||||||
public int sum( @RequestParam(value = "num1", defaultValue = "0") int num1,
|
|
||||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
|
||||||
{
|
|
||||||
return num1 + num2;
|
|
||||||
}
|
|
||||||
@GetMapping("/sub")
|
|
||||||
public int sub( @RequestParam(value = "num1", defaultValue = "0") int num1,
|
|
||||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
|
||||||
{
|
|
||||||
return num1 - num2;
|
|
||||||
}
|
|
||||||
@GetMapping("/mul")
|
|
||||||
public int mul( @RequestParam(value = "num1", defaultValue = "0") int num1,
|
|
||||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
|
||||||
{
|
|
||||||
return num1 * num2;
|
|
||||||
}
|
|
||||||
@GetMapping("/div")
|
|
||||||
public int div( @RequestParam(value = "num1", defaultValue = "0") int num1,
|
|
||||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
|
||||||
{
|
|
||||||
return num1 / num2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebConfiguration implements WebMvcConfigurer {
|
public class WebConfiguration implements WebMvcConfigurer {
|
||||||
|
public static final String REST_API = "/api";
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**").allowedMethods("*");
|
registry.addMapping("/**").allowedMethods("*");
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
103
src/main/resources/templates/customers.html
Normal file
103
src/main/resources/templates/customers.html
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<div class="ms-5">
|
||||||
|
<p class='h4 m-3'>Профили</p>
|
||||||
|
<button class="button primary" data-bs-toggle="modal" data-bs-target="#customerCreate">
|
||||||
|
Добавить нового пользователя
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<p class='h4 ms-3'>Список</p>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-5">
|
||||||
|
<div th:each="customer: ${customers}" class="row card is-left mb-3">
|
||||||
|
<div class="row is-left">
|
||||||
|
<div class="row is-left h3" th:text="${customer.username}"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" th:if="!${#arrays.isEmpty(customer.comments)}">
|
||||||
|
<p class="h4">Посты:</p>
|
||||||
|
<div class="col">
|
||||||
|
<div th:each="post: ${customer.posts}" class="row is-left card mb-3">
|
||||||
|
<div class="row is-left h4" th:text="${post.title}"></div>
|
||||||
|
<div class="row is-left h5" th:text="${post.content}"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" th:if="!${#arrays.isEmpty(customer.comments)}">
|
||||||
|
<p class="h4">Комментарии:</p>
|
||||||
|
<div class="col">
|
||||||
|
<div th:each="comment: ${customer.comments}" class="row is-left card mb-3">
|
||||||
|
<div class="row is-left h5" th:text="${'"' + comment.content + '"' + ' - к посту ' + '"' + comment.postTitle + '"' + ' от пользователя ' + comment.postAuthor}"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" th:if="${session.currentCustomerId == customer.id}">
|
||||||
|
<form th:action="@{/customers/delete/{id}(id=${customer.id})}" method="post" class="col">
|
||||||
|
<button class="button dark outline is-full-width" type="submit">Удалить</button>
|
||||||
|
</form>
|
||||||
|
<button th:data-username="${customer.username}" th:data-password="${customer.password}" th:data-id="${customer.id}" th:onclick="|prepareEditModal(this)|" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#customerEdit">Редактировать</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="customerCreate" tabindex="-1" role="dialog" aria-labelledby="customerCreateLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<form class="modal-content" th:action="@{/customers/}" method="post">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="customerCreateLabel">Создать пользователя</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Имя пользователя</p>
|
||||||
|
<textarea name="username" id="usernameTextC" cols="30" rows="1"></textarea>
|
||||||
|
<p>Пароль</p>
|
||||||
|
<textarea name="password" id="passwordTextC" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Создать</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="customerEdit" tabindex="-1" role="dialog" aria-labelledby="customerEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<form class="modal-content" id="edit-customer-form" method="post">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="customerEditLabel">Изменение пользователя</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Имя пользователя</p>
|
||||||
|
<textarea name="username" id="usernameTextE" cols="30" rows="1"></textarea>
|
||||||
|
<p>Пароль</p>
|
||||||
|
<textarea name="password" id="passwordTextE" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Отмена</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Применить</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<th:block layout:fragment="scripts">
|
||||||
|
<script th:inline="javascript">
|
||||||
|
function prepareEditModal(btn) {
|
||||||
|
document.getElementById('usernameTextE').value = btn.getAttribute("data-username");
|
||||||
|
document.getElementById('passwordTextE').value = btn.getAttribute("data-password");
|
||||||
|
document.getElementById('edit-customer-form').setAttribute('action', '/customers/' + btn.getAttribute("data-id"));
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</th:block>
|
||||||
|
</html>
|
||||||
42
src/main/resources/templates/default.html
Normal file
42
src/main/resources/templates/default.html
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||||
|
<title>LabWork05 - Social Network</title>
|
||||||
|
</head>
|
||||||
|
<body class="container">
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
|
||||||
|
<div>
|
||||||
|
<div>
|
||||||
|
<p class='text-center m-3 h3'>LabWork05 - Social Network</p>
|
||||||
|
</div>
|
||||||
|
<div class="nav row">
|
||||||
|
<div class="nav-left col-4" th:with="activeLink=${request.requestURI}">
|
||||||
|
<a href="/customers/" class="text-decoration-none m-3" th:classappend="${#strings.startsWith(activeLink, '/customers')} ? 'clear' : ''">Профили</a>
|
||||||
|
<a href="/feed" class="text-decoration-none m-3" th:classappend="${#strings.startsWith(activeLink, '/feed')} ? 'clear' : ''">Посты</a>
|
||||||
|
<select class="form-select mt-4" style="font-size: 16px; max-height: 35px; max-width: 180px; min-height: 35px; min-width: 180px;" th:onchange="updateSession()" id="selectBox">
|
||||||
|
<option value="-1" th:selected="${session.currentCustomerId == -1}" class="button dark outline">Не выбран</option>
|
||||||
|
<option th:each="customer: ${customers}" th:selected="${session.currentCustomerId == customer.id}" th:value="${customer.id}" class="button dark outline" th:text="${customer.username}"></option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
function updateSession() {
|
||||||
|
let selectBox = document.getElementById("selectBox");
|
||||||
|
let id = selectBox.options[selectBox.selectedIndex].value;
|
||||||
|
fetch("/update-session?currentCustomerId=" + id, {method: "post"})
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<th:block layout:fragment="scripts">
|
||||||
|
</th:block>
|
||||||
|
</html>
|
||||||
144
src/main/resources/templates/feed.html
Normal file
144
src/main/resources/templates/feed.html
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<div class="ms-5">
|
||||||
|
<p class='h3 m-3'>Посты</p>
|
||||||
|
|
||||||
|
<div class="col-5">
|
||||||
|
<form class="row mb-5" action="/feed">
|
||||||
|
<input type="search" name="search" class="col-7">
|
||||||
|
<button class="button primary col" type="submit">Найти</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" th:if="${session.currentCustomerId > 0}">
|
||||||
|
<form class="col-7" th:action="@{/posts/}" method="post">
|
||||||
|
<input name="customerId" type="text" style="display: none;" th:value="${session.currentCustomerId}">
|
||||||
|
<div class="row is-left">
|
||||||
|
<p class="col-2 is-left mb-2">Заголовок:</p>
|
||||||
|
<input name="title" type="text" class="col-5" id="createPostTitle" />
|
||||||
|
</div>
|
||||||
|
<div class="row is-left">
|
||||||
|
<p class="col-2 is-left mb-2">Текст:</p>
|
||||||
|
<textarea name="content" type="textarea" class="col-5" id="createPostContent"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="row is-left">
|
||||||
|
<button type='submit' class="button primary col-7">
|
||||||
|
Опубликовать
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div th:unless="${#arrays.isEmpty(posts)}" class="row">
|
||||||
|
<div class="col-5">
|
||||||
|
<div class="row mb-5 card" th:each="post: ${posts}">
|
||||||
|
<div class="col">
|
||||||
|
|
||||||
|
<div class="row is-left my-3">
|
||||||
|
<p><a th:href="${'/customers/' + post.customerId}" class="text-primary" th:text="${post.customerName}"></a></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row text-left">
|
||||||
|
<span class="h2" th:text="${post.title}"></span>
|
||||||
|
<span class="h3" th:text="${post.content}"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div th:unless="${#arrays.isEmpty(post.comments)}">
|
||||||
|
<p class="row h3 is-left my-3">Комментарии</p>
|
||||||
|
|
||||||
|
<div class="row text-left mb-4 card" th:each="comment: ${post.comments}">
|
||||||
|
<div class="row is-left">
|
||||||
|
<span class="h2 text-primary" th:text="${comment.customerName}"></span>
|
||||||
|
<span class="h3" th:text="${comment.content}"></span>
|
||||||
|
</div>
|
||||||
|
<div th:if="${session.currentCustomerId == comment.customerId}" class="row mt-3">
|
||||||
|
<button th:data-content="${comment.content}" th:data-id="${comment.id}" th:onclick="|prepareCommentEditModal(this)|" class="button primary outline col" data-bs-toggle="modal" data-bs-target="#commentEdit">Изменить</button>
|
||||||
|
<form th:action="@{/comments/delete/{id}(id=${comment.id})}" method="post" class="col">
|
||||||
|
<button type="submit" class="button error is-full-width">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form class="row" th:if="${session.currentCustomerId != -1}" th:action="@{/comments/}" method="post">
|
||||||
|
<input name="content" type="text" class="col-6"/>
|
||||||
|
<input name="customerId" type="text" style="display: none;" th:value="${session.currentCustomerId}">
|
||||||
|
<input name="postId" type="text" style="display: none;" th:value="${post.id}">
|
||||||
|
<button type="submit" class="button col-6 secondary outline">Комментировать</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row" th:if="${session.currentCustomerId == post.customerId}">
|
||||||
|
<form class="col" th:action="@{/posts/delete/{id}(id=${post.id})}" method="post">
|
||||||
|
<button type="submit" class="is-full-width button dark outline">Удалить пост</button>
|
||||||
|
</form>
|
||||||
|
<button th:data-customer="${session.currentCustomerId}" th:data-id="${post.id}" th:data-title="${post.title}" th:data-content="${post.content}" type="button" th:onclick="|preparePostEditModal(this)|" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#postEdit">Изменить пост</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="postEdit" tabindex="-1" role="dialog" aria-labelledby="postEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<form method="post" class="modal-content" id="editPost">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="postEditLabel">Редактировать пост</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<p>Заголовок</p>
|
||||||
|
<textarea name="title" id="editModalTitle" cols="30" rows="1"></textarea>
|
||||||
|
<p>Содержание</p>
|
||||||
|
<textarea name="content" id="editModalPostContent" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Изменить</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade" id="commentEdit" tabindex="-1" role="dialog" aria-labelledby="commentEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<form method="post" class="modal-content" id="editComment">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="commentEditLabel">Изменить комментарий</h5>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body text-center">
|
||||||
|
<textarea name='content' id="editModalText" cols="30" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Изменить</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
<th:block layout:fragment="scripts">
|
||||||
|
<script th:inline="javascript">
|
||||||
|
function prepareCommentEditModal(btn) {
|
||||||
|
document.getElementById('editModalText').value = btn.getAttribute("data-content");
|
||||||
|
document.getElementById('editComment').setAttribute('action', '/comments/' + btn.getAttribute("data-id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function preparePostEditModal(btn) {
|
||||||
|
document.getElementById('editModalTitle').value = btn.getAttribute("data-title");
|
||||||
|
document.getElementById('editModalPostContent').value = btn.getAttribute("data-content");
|
||||||
|
document.getElementById('editPost').setAttribute('action', '/posts/' + btn.getAttribute("data-id"));
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</th:block>
|
||||||
|
</html>
|
||||||
@@ -1,13 +1,118 @@
|
|||||||
package ru.ulstu.is.labwork;
|
package ru.ulstu.is.labwork;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class LabworkApplicationTests {
|
class LabworkApplicationTests {
|
||||||
|
|
||||||
@Test
|
// @Autowired
|
||||||
void contextLoads() {
|
// CustomerService customerService;
|
||||||
}
|
//
|
||||||
|
// @Autowired
|
||||||
|
// CommentService commentService;
|
||||||
|
//
|
||||||
|
// @Autowired
|
||||||
|
// PostService postService;
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// void testCustomers() {
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
//
|
||||||
|
// Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
// Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
// Customer c3 = customerService.addCustomer("third", "3");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals("first", c1.getUsername());
|
||||||
|
// Assertions.assertEquals("second", c2.getUsername());
|
||||||
|
// Assertions.assertEquals("third", c3.getUsername());
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(c1, customerService.findCustomer(c1.getId()));
|
||||||
|
//
|
||||||
|
// customerService.deleteCustomer(c2.getId());
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(2, customerService.findAllCustomers().size());
|
||||||
|
//
|
||||||
|
// Customer c4 = customerService.updateCustomer(c3.getId(), "fourth", "4");
|
||||||
|
//
|
||||||
|
// Assertions.assertNotEquals(c3.getUsername(), c4.getUsername());
|
||||||
|
// Assertions.assertNotEquals(c3.getHashedPassword(), c4.getHashedPassword());
|
||||||
|
//
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// void testPost() {
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
//
|
||||||
|
// Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
// Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
//
|
||||||
|
// Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||||
|
// Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(p1.getCustomer(), c1);
|
||||||
|
// Assertions.assertEquals(p2.getCustomer(), c2);
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(c1.getPosts().get(0), p1);
|
||||||
|
// Assertions.assertEquals(c2.getPosts().get(0), p2);
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(p1, postService.findPost(p1.getId()));
|
||||||
|
// Assertions.assertEquals(p2, postService.findPost(p2.getId()));
|
||||||
|
//
|
||||||
|
// Post p3 = postService.addPost(c1, "asdf", "asd");
|
||||||
|
// postService.deletePost(p1.getId());
|
||||||
|
// Assertions.assertEquals(1, customerService.findCustomer(c1.getId()).getPosts().size());
|
||||||
|
//
|
||||||
|
// Post p4 = postService.updatePost(p2.getId(), "third title", "wow");
|
||||||
|
//
|
||||||
|
// Assertions.assertNotEquals(p2.getTitle(), p4.getTitle());
|
||||||
|
// Assertions.assertNotEquals(p2.getContent(), p4.getContent());
|
||||||
|
//
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// void testComment() {
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
//
|
||||||
|
// Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
// Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
//
|
||||||
|
// Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||||
|
// Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||||
|
//
|
||||||
|
// Comment com1 = commentService.addComment(c1, p2, "What");
|
||||||
|
// Comment com2 = commentService.addComment(c2, p1, "How");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(c1, p2.getComments().get(0).getCustomer());
|
||||||
|
// Assertions.assertEquals(c2, p1.getComments().get(0).getCustomer());
|
||||||
|
//
|
||||||
|
// Comment com3 = commentService.addComment(c1, p1, "Really");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(com2, commentService.findComment(p1.getComments().get(0).getId()));
|
||||||
|
//
|
||||||
|
// Comment com4 = commentService.updateComment(com3.getId(), "Not really");
|
||||||
|
//
|
||||||
|
// Assertions.assertNotEquals(com3.getContent(), com4.getContent());
|
||||||
|
// Assertions.assertEquals(com3.getCustomer().getId(), com4.getCustomer().getId());
|
||||||
|
//
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user