Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2224416988 | ||
00c99afbcf | |||
|
cbf4b0b9a3 | ||
|
2096215195 | ||
|
8315a23855 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/frontend/node_modules/
|
||||||
|
/build
|
||||||
|
/.idea
|
@ -14,7 +14,11 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
implementation 'org.hibernate.validator:hibernate-validator'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
17
frontend/README.md
Normal file
17
frontend/README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
## 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
frontend/babel.config.js
Normal file
5
frontend/babel.config.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
presets: [
|
||||||
|
'@vue/cli-plugin-babel/preset'
|
||||||
|
]
|
||||||
|
}
|
@ -1,42 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Калькулятор</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>
|
|
||||||
<div class="m-auto dflex flex-row align-items-center">
|
|
||||||
<div class="col-sm-3 my-1">
|
|
||||||
<input type="number" class="form-control" id="num1">
|
|
||||||
</div>
|
|
||||||
<div class="col-sm-3 my-1">
|
|
||||||
<select class="form-control" id="operation">
|
|
||||||
<option value="sum" selected>+</option>
|
|
||||||
<option value="sub">-</option>
|
|
||||||
<option value="mul">*</option>
|
|
||||||
<option value="div">/</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
<div class="col-sm-3 my-1">
|
|
||||||
<input type="number" class="form-control" id="num2">
|
|
||||||
</div>
|
|
||||||
<div class="col-sm-3 my-1">
|
|
||||||
<button type="button" class="form-control btn btn-light" id="get-result" onclick="calculate()">=</button>
|
|
||||||
</div>
|
|
||||||
<div class="col-sm-3 my-1">
|
|
||||||
<p class="h5" id="result"></p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
async function calculate() {
|
|
||||||
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)
|
|
19
frontend/jsconfig.json
Normal file
19
frontend/jsconfig.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es5",
|
||||||
|
"module": "esnext",
|
||||||
|
"baseUrl": "./",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"paths": {
|
||||||
|
"@/*": [
|
||||||
|
"src/*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"lib": [
|
||||||
|
"esnext",
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"scripthost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
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"
|
|
||||||
}
|
|
17567
frontend/package-lock.json
generated
17567
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,29 @@
|
|||||||
{
|
{
|
||||||
"name": "frontend",
|
"name": "front",
|
||||||
"version": "1.0.0",
|
"version": "0.1.0",
|
||||||
"description": "",
|
"private": true,
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "node index.js"
|
"serve": "vue-cli-service serve",
|
||||||
|
"build": "vue-cli-service build"
|
||||||
},
|
},
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fs": "0.0.1-security",
|
"@popperjs/core": "^2.11.7",
|
||||||
"http": "0.0.1-security"
|
"axios": "^1.3.4",
|
||||||
}
|
"core-js": "^3.8.3",
|
||||||
|
"vue": "^3.2.13",
|
||||||
|
"vue-router": "^4.0.3",
|
||||||
|
"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"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
17
frontend/public/index.html
Normal file
17
frontend/public/index.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!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.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||||
|
<title>Социальная сеть</title>
|
||||||
|
</head>
|
||||||
|
<body class="container">
|
||||||
|
<script src="https://unpkg.com/@popperjs/core@2"></script>
|
||||||
|
<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 id="app"></div>
|
||||||
|
<!-- built files will be auto injected -->
|
||||||
|
</body>
|
||||||
|
</html>
|
47
frontend/src/App.vue
Normal file
47
frontend/src/App.vue
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<template>
|
||||||
|
<div class="nav row">
|
||||||
|
<div class="nav-left col-10">
|
||||||
|
<router-link to="/customers" class="button primary clear">Профили</router-link>
|
||||||
|
<router-link to="/posts" class="button primary clear">Посты</router-link>
|
||||||
|
</div>
|
||||||
|
<div class="nav-right col-2">
|
||||||
|
<select class="form-select" style="font-size: 16px;" 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/customer');
|
||||||
|
this.customers = [];
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}, 500)
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="">
|
||||||
|
|
||||||
|
</style>
|
164
frontend/src/components/Customers.vue
Normal file
164
frontend/src/components/Customers.vue
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
<template>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div class="row mb-5">
|
||||||
|
<p class='is-center h2'>Профили</p>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-5">
|
||||||
|
<div class="col"></div>
|
||||||
|
<div class="col-10 is-center">
|
||||||
|
<button class="button primary" data-bs-toggle="modal" data-bs-target="#customerCreate">
|
||||||
|
Добавить нового пользователя
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="col"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class='h3 is-center row mb-5'>Список профилей</p>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div v-for="customer in customers" class="row card mb-3">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col is-left h3">ID: {{ customer['id'] }}</div>
|
||||||
|
<div class="col is-center h3">Никнейм: {{ customer['username'] }}</div>
|
||||||
|
<div class="col is-right h3">Пароль: {{ customer['password'] }}</div>
|
||||||
|
</div>
|
||||||
|
<p class="row">Комментарии:</p>
|
||||||
|
<div class="row" v-if="!(customer['comments'].length == 0)">
|
||||||
|
<div class="col">
|
||||||
|
<div v-for="comment in customer['comments']" class="row is-left card mb-3">
|
||||||
|
<div class="row is-left h4">"{{ comment['content'] }}" - к посту '{{ comment['postTitle'] }}' от пользователя {{ comment['postAuthor'] }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p v-else class="row">Нет комментариев</p>
|
||||||
|
<p class="row">Посты: </p>
|
||||||
|
<div class="row" v-if="!(customer['posts'].length == 0)">
|
||||||
|
<div class="col">
|
||||||
|
<div v-for="post in customer['posts']" class="row is-left card mb-3">
|
||||||
|
<div class="row is-center h1">{{ post['title'] }}</div>
|
||||||
|
<div class="row is-left h3">{{ post['content'] }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p v-else class="row">Нет постов</p>
|
||||||
|
<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>
|
||||||
|
</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(){
|
||||||
|
const response = await axios.post('http://localhost:8080/customer?username=' + this.usernameModal + '&password=' + this.passwordModal);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async deleteUser(id) {
|
||||||
|
const response = await axios.delete('http://localhost:8080/customer/' + id);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async editUser() {
|
||||||
|
const response = await axios.put('http://localhost:8080/customer/' + this.selectedCustomer['id'] + '?username=' + this.usernameModal + '&password=' + this.passwordModal);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async refreshList() {
|
||||||
|
this.customers = [];
|
||||||
|
if (this.$route.params.id === "") {
|
||||||
|
const response = await axios.get('http://localhost:8080/customer');
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const response = await axios.get('http://localhost:8080/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/customer');
|
||||||
|
response.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const response = await axios.get('http://localhost:8080/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>
|
227
frontend/src/components/Posts.vue
Normal file
227
frontend/src/components/Posts.vue
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
<template>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div class="row mb-5">
|
||||||
|
<p class='is-center h2'>Посты</p>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-5" v-if="currentCustomerId != -1">
|
||||||
|
<div class="col"></div>
|
||||||
|
<div class="col-10">
|
||||||
|
<div class="row mb-4 is-center">
|
||||||
|
<p class="col-2 is-left mb-2">Заголовок:</p>
|
||||||
|
<input type="text" class="col-6" v-model="titleModal"/>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-4 is-center">
|
||||||
|
<p class="col-2 is-left mb-2">Текст:</p>
|
||||||
|
<textarea type="textarea" class="col-6" v-model="postContentModal"/>
|
||||||
|
</div>
|
||||||
|
<div class="row is-center">
|
||||||
|
<button type='button' class="button primary col-8" v-on:click='createPost'>
|
||||||
|
Опубликовать
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col"></div>
|
||||||
|
</div>
|
||||||
|
<div v-if="!(posts.length == 0)" class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div class="row mb-5 card" v-for="post in posts">
|
||||||
|
<div class="col">
|
||||||
|
<div class="row h3">
|
||||||
|
<div class="col is-left">
|
||||||
|
<p>Автор: <router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></p>
|
||||||
|
</div>
|
||||||
|
<div class="col is-right">
|
||||||
|
<p>Дата: <span class="text-primary">{{ post['createDate'] }}</span></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row text-center">
|
||||||
|
<span class="h1">{{ post['title'] }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="row text-center mb-5">
|
||||||
|
<span class="h3">{{ post['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<p class="row h2 is-center mb-3">Комментарии</p>
|
||||||
|
<div v-if="!(post['comments'].length == 0)" class="row text-left mb-5 card" v-for="comment in post['comments']">
|
||||||
|
<div class="row mb-1">
|
||||||
|
<div class="col is-left">
|
||||||
|
<span class="h2 text-primary">{{ comment['customerName'] }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="col is-right">
|
||||||
|
<span class="h2 text-primary">{{ comment['createDate'] }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-3">
|
||||||
|
<span class="h3">{{ comment['content'] }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="selectedCustomerContainsComment(comment) == true" class="row">
|
||||||
|
<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>
|
||||||
|
<p v-else class="h3 row is-center mb-5">Пусто</p>
|
||||||
|
<div class="row" v-if="currentCustomerId != -1">
|
||||||
|
<input type="text" v-bind:id="'post-comment-' + post['id']" class="col-9"/>
|
||||||
|
<button type="button" v-on:click="selectedPostId = post['id']; selectedPostTitle = post['title']; createComment()" class="button col-3 secondary outline">Комментировать</button>
|
||||||
|
</div>
|
||||||
|
</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 v-else class="row text-center is-center">
|
||||||
|
Нет постов
|
||||||
|
</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>
|
||||||
|
|
||||||
|
<!-- 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>
|
||||||
|
</template>
|
||||||
|
<script>
|
||||||
|
import axios from 'axios'
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
customers: [],
|
||||||
|
selectedCustomerId: 0,
|
||||||
|
currentCustomer: {},
|
||||||
|
posts: [],
|
||||||
|
selectedPostId: 0,
|
||||||
|
selectedPostTitle: '',
|
||||||
|
contentModal: '',
|
||||||
|
titleModal: '',
|
||||||
|
postContentModal: '',
|
||||||
|
selectedCommentId: 0,
|
||||||
|
currentCustomerId: -1,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
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
|
||||||
|
await axios.post('http://localhost:8080/comment?text=' + content + '&ownerId=' + this.currentCustomerId + '&postId=' + this.selectedPostId);
|
||||||
|
document.getElementById("post-comment-" + this.selectedPostId).value = ''
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async deleteComment(commentId) {
|
||||||
|
await axios.delete('http://localhost:8080/comment/' + commentId);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async editComment(){
|
||||||
|
await axios.put('http://localhost:8080/comment/' + this.selectedCommentId + '?text=' + this.contentModal);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async createPost() {
|
||||||
|
const response = await axios.post('http://localhost:8080/post?title=' + this.titleModal + '&content=' + this.postContentModal + '&authorId=' + this.currentCustomerId);
|
||||||
|
this.titleModal = ''
|
||||||
|
this.postContentModal = ''
|
||||||
|
this.refreshList();
|
||||||
|
|
||||||
|
},
|
||||||
|
async deletePost(postId) {
|
||||||
|
const response = await axios.delete('http://localhost:8080/post/' + postId);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async editPost(){
|
||||||
|
const response = await axios.put('http://localhost:8080/post/' + this.selectedPostId + '?title=' + this.titleModal + '&content=' + this.postContentModal);
|
||||||
|
this.refreshList();
|
||||||
|
},
|
||||||
|
async refreshList(){
|
||||||
|
this.customers = [];
|
||||||
|
this.posts = [];
|
||||||
|
const responseCustomer = await axios.get('http://localhost:8080/customer');
|
||||||
|
responseCustomer.data.forEach(element => {
|
||||||
|
this.customers.push(element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
const responsePost = await axios.get('http://localhost:8080/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/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/post');
|
||||||
|
responsePost.data.forEach(element => {
|
||||||
|
this.posts.splice(0, 0, element);
|
||||||
|
console.log(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<style lang="">
|
||||||
|
|
||||||
|
</style>
|
29
frontend/src/main.js
Normal file
29
frontend/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
frontend/vue.config.js
Normal file
4
frontend/vue.config.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
const { defineConfig } = require('@vue/cli-service')
|
||||||
|
module.exports = defineConfig({
|
||||||
|
transpileDependencies: true
|
||||||
|
})
|
53
src/main/java/np/something/DTO/CommentDto.java
Normal file
53
src/main/java/np/something/DTO/CommentDto.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package np.something.DTO;
|
||||||
|
|
||||||
|
import np.something.model.Comment;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
public class CommentDto {
|
||||||
|
public final long id;
|
||||||
|
public final String content;
|
||||||
|
public final String customerName;
|
||||||
|
public final String postTitle;
|
||||||
|
public final String postAuthor;
|
||||||
|
public final long postAuthorId;
|
||||||
|
|
||||||
|
public final String createDate;
|
||||||
|
|
||||||
|
public CommentDto(Comment comment) {
|
||||||
|
this.id = comment.getId();
|
||||||
|
this.content = comment.getContent();
|
||||||
|
this.customerName = comment.getCustomer().getUsername();
|
||||||
|
this.postTitle = comment.getPost().getTitle();
|
||||||
|
this.postAuthor = comment.getPost().getCustomer().getUsername();
|
||||||
|
this.postAuthorId = comment.getPost().getCustomer().getId();
|
||||||
|
this.createDate = comment.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostTitle() {return postTitle;}
|
||||||
|
|
||||||
|
public String getPostAuthor() {
|
||||||
|
return postAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getPostAuthorId() {
|
||||||
|
return postAuthorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
}
|
41
src/main/java/np/something/DTO/CustomerDto.java
Normal file
41
src/main/java/np/something/DTO/CustomerDto.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package np.something.DTO;
|
||||||
|
|
||||||
|
import np.something.model.Customer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CustomerDto {
|
||||||
|
public final long id;
|
||||||
|
public final String username;
|
||||||
|
public final String password;
|
||||||
|
public final List<CommentDto> comments;
|
||||||
|
public final List<PostDto> posts;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CommentDto> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostDto> getPosts() {
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
}
|
57
src/main/java/np/something/DTO/PostDto.java
Normal file
57
src/main/java/np/something/DTO/PostDto.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package np.something.DTO;
|
||||||
|
|
||||||
|
import np.something.model.Post;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PostDto {
|
||||||
|
public final long id;
|
||||||
|
public final String title;
|
||||||
|
public final String content;
|
||||||
|
public final String customerName;
|
||||||
|
|
||||||
|
public final long customerId;
|
||||||
|
public final List<CommentDto> comments;
|
||||||
|
|
||||||
|
public String createDate;
|
||||||
|
|
||||||
|
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 = post.getComments().stream().map(CommentDto::new).toList();
|
||||||
|
this.createDate = post.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CommentDto> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCustomerId() {
|
||||||
|
return customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.Exceptions;
|
||||||
|
|
||||||
|
public class CommentNotFoundException extends RuntimeException {
|
||||||
|
public CommentNotFoundException(Long id) {
|
||||||
|
super(String.format("Comment with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.Exceptions;
|
||||||
|
|
||||||
|
public class CustomerNotFoundException extends RuntimeException {
|
||||||
|
public CustomerNotFoundException(Long id) {
|
||||||
|
super(String.format("Customer with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.Exceptions;
|
||||||
|
|
||||||
|
public class PostNotFoundException extends RuntimeException {
|
||||||
|
public PostNotFoundException(Long id) {
|
||||||
|
super(String.format("Post with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package np.something.controllers;
|
||||||
|
|
||||||
|
import np.something.DTO.CommentDto;
|
||||||
|
import np.something.model.Comment;
|
||||||
|
import np.something.services.*;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/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(@RequestParam("text") String text, @RequestParam("ownerId") Long ownerId, @RequestParam("postId") Long postId){
|
||||||
|
final Comment comment = commentService.addComment(customerService.findCustomer(ownerId), postService.findPost(postId), text);
|
||||||
|
return new CommentDto(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CommentDto updateComment(@RequestParam("text") String text, @PathVariable Long id) {
|
||||||
|
return new CommentDto(commentService.updateComment(id, text));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CommentDto deleteComment(@PathVariable Long id) {
|
||||||
|
return new CommentDto(commentService.deleteComment(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllComments(){
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package np.something.controllers;
|
||||||
|
|
||||||
|
import np.something.DTO.CustomerDto;
|
||||||
|
import np.something.model.Customer;
|
||||||
|
import np.something.services.*;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/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(@RequestParam("username") String username, @RequestParam("password") String password){
|
||||||
|
final Customer customer = customerService.addCustomer(username, password);
|
||||||
|
return new CustomerDto(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public CustomerDto updateCustomer(@RequestParam("username") String username, @RequestParam("password") String password, @PathVariable Long id) {
|
||||||
|
return new CustomerDto(customerService.updateCustomer(id, username, password));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public CustomerDto deleteCustomer(@PathVariable Long id) {
|
||||||
|
return new CustomerDto(customerService.deleteCustomer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllCustomers(){
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
}
|
||||||
|
}
|
@ -1,41 +0,0 @@
|
|||||||
package np.something.controllers;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class MathController {
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
61
src/main/java/np/something/controllers/PostController.java
Normal file
61
src/main/java/np/something/controllers/PostController.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package np.something.controllers;
|
||||||
|
|
||||||
|
import np.something.DTO.PostDto;
|
||||||
|
import np.something.services.CustomerService;
|
||||||
|
import np.something.services.PostService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/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(
|
||||||
|
@RequestParam("title") String title,
|
||||||
|
@RequestParam("content") String content,
|
||||||
|
@RequestParam("authorId") Long authorId
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return new PostDto(postService.addPost(customerService.findCustomer(authorId), title, content));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public PostDto updatePost(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@RequestParam("title") String title,
|
||||||
|
@RequestParam("content") String content
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return new PostDto(postService.updatePost(id, title, content));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public PostDto deletePost (@PathVariable Long id) {
|
||||||
|
return new PostDto(postService.deletePost(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public void deleteAllPosts() {
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
}
|
||||||
|
}
|
82
src/main/java/np/something/model/Comment.java
Normal file
82
src/main/java/np/something/model/Comment.java
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package np.something.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Comment {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
@NotBlank(message = "Content cannot be empty")
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Column(columnDefinition = "timestamp default NOW()")
|
||||||
|
private LocalDateTime createDate;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateDate(LocalDateTime createDate) {
|
||||||
|
this.createDate = createDate;
|
||||||
|
}
|
||||||
|
}
|
75
src/main/java/np/something/model/Customer.java
Normal file
75
src/main/java/np/something/model/Customer.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package np.something.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Customer {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
@NotBlank(message = "Username cannot be empty")
|
||||||
|
private String username;
|
||||||
|
@Column
|
||||||
|
@NotBlank(message = "Password cannot be empty")
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
95
src/main/java/np/something/model/Post.java
Normal file
95
src/main/java/np/something/model/Post.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package np.something.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Post {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
@NotBlank(message = "Title cannot be empty")
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
@NotBlank(message = "Content cannot be empty")
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Column(columnDefinition = "timestamp default NOW()")
|
||||||
|
private LocalDateTime createDate;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreateDate() {
|
||||||
|
return createDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateDate(LocalDateTime createDate) {
|
||||||
|
this.createDate = createDate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.repositories;
|
||||||
|
|
||||||
|
import np.something.model.Comment;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface CommentRepository extends JpaRepository<Comment, Long> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.repositories;
|
||||||
|
|
||||||
|
import np.something.model.Customer;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package np.something.repositories;
|
||||||
|
|
||||||
|
import np.something.model.Post;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||||
|
}
|
69
src/main/java/np/something/services/CommentService.java
Normal file
69
src/main/java/np/something/services/CommentService.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package np.something.services;
|
||||||
|
|
||||||
|
import np.something.Exceptions.CommentNotFoundException;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import np.something.model.Comment;
|
||||||
|
import np.something.model.Customer;
|
||||||
|
import np.something.model.Post;
|
||||||
|
import np.something.repositories.CommentRepository;
|
||||||
|
import np.something.util.validation.ValidatorUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CommentService {
|
||||||
|
private final CommentRepository commentRepository;
|
||||||
|
private final ValidatorUtil validatorUtil;
|
||||||
|
|
||||||
|
public CommentService(CommentRepository commentRepository,
|
||||||
|
ValidatorUtil validatorUtil) {
|
||||||
|
this.commentRepository = commentRepository;
|
||||||
|
this.validatorUtil = validatorUtil;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Comment findComment(Long id) {
|
||||||
|
final Optional<Comment> comment = commentRepository.findById(id);
|
||||||
|
return comment.orElseThrow(() -> new CommentNotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Comment> findAllComments() {
|
||||||
|
return commentRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment addComment(Customer customer, Post post, String content) {
|
||||||
|
final Comment comment = new Comment(customer, post, content);
|
||||||
|
comment.setCreateDate(LocalDateTime.now());
|
||||||
|
validatorUtil.validate(comment);
|
||||||
|
customer.getComments().add(comment);
|
||||||
|
post.getComments().add(comment);
|
||||||
|
return commentRepository.save(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment updateComment(Long id, String content) {
|
||||||
|
final Comment currentComment = findComment(id);
|
||||||
|
currentComment.setContent(content);
|
||||||
|
validatorUtil.validate(currentComment);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
65
src/main/java/np/something/services/CustomerService.java
Normal file
65
src/main/java/np/something/services/CustomerService.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package np.something.services;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import np.something.Exceptions.CustomerNotFoundException;
|
||||||
|
import np.something.model.Customer;
|
||||||
|
import np.something.repositories.CommentRepository;
|
||||||
|
import np.something.repositories.CustomerRepository;
|
||||||
|
import np.something.util.validation.ValidatorUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CustomerService {
|
||||||
|
private final CustomerRepository customerRepository;
|
||||||
|
private final ValidatorUtil validatorUtil;
|
||||||
|
|
||||||
|
public CustomerService(CustomerRepository customerRepository,
|
||||||
|
ValidatorUtil validatorUtil) {
|
||||||
|
this.customerRepository = customerRepository;
|
||||||
|
this.validatorUtil = validatorUtil;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer findCustomer(Long id) {
|
||||||
|
return customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Customer> findAllCustomers() {
|
||||||
|
return customerRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer addCustomer(String username, String password) {
|
||||||
|
Customer customer = new Customer(username, password);
|
||||||
|
validatorUtil.validate(customer);
|
||||||
|
return customerRepository.save(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer updateCustomer(Long id, String username, String password) {
|
||||||
|
Customer customer = findCustomer(id);
|
||||||
|
customer.setUsername(username);
|
||||||
|
customer.setPassword(password);
|
||||||
|
validatorUtil.validate(customer);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
71
src/main/java/np/something/services/PostService.java
Normal file
71
src/main/java/np/something/services/PostService.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package np.something.services;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import np.something.Exceptions.PostNotFoundException;
|
||||||
|
import np.something.model.Comment;
|
||||||
|
import np.something.model.Customer;
|
||||||
|
import np.something.model.Post;
|
||||||
|
import np.something.repositories.CustomerRepository;
|
||||||
|
import np.something.repositories.PostRepository;
|
||||||
|
import np.something.util.validation.ValidatorUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostService {
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
private final ValidatorUtil validatorUtil;
|
||||||
|
|
||||||
|
public PostService(PostRepository postRepository,
|
||||||
|
ValidatorUtil validatorUtil) {
|
||||||
|
this.postRepository = postRepository;
|
||||||
|
this.validatorUtil = validatorUtil;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post findPost(Long id) {
|
||||||
|
return postRepository.findById(id).orElseThrow(() -> new PostNotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Post> findAllPosts() {
|
||||||
|
return postRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post addPost(Customer customer, String title, String content) {
|
||||||
|
Post post = new Post(customer, title, content);
|
||||||
|
post.setCreateDate(LocalDateTime.now());
|
||||||
|
validatorUtil.validate(post);
|
||||||
|
customer.getPosts().add(post);
|
||||||
|
return postRepository.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post updatePost(Long id, String title, String content) {
|
||||||
|
Post post = findPost(id);
|
||||||
|
post.setTitle(title);
|
||||||
|
post.setContent(content);
|
||||||
|
validatorUtil.validate(post);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
42
src/main/java/np/something/util/error/AdviceController.java
Normal file
42
src/main/java/np/something/util/error/AdviceController.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package np.something.util.error;
|
||||||
|
|
||||||
|
import np.something.Exceptions.CommentNotFoundException;
|
||||||
|
import np.something.Exceptions.CustomerNotFoundException;
|
||||||
|
import np.something.Exceptions.PostNotFoundException;
|
||||||
|
import np.something.util.validation.ValidationException;
|
||||||
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class AdviceController {
|
||||||
|
@ExceptionHandler({
|
||||||
|
CommentNotFoundException.class,
|
||||||
|
CustomerNotFoundException.class,
|
||||||
|
PostNotFoundException.class,
|
||||||
|
ValidationException.class
|
||||||
|
})
|
||||||
|
public ResponseEntity<Object> handleException(Throwable e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||||
|
final ValidationException validationException = new ValidationException(
|
||||||
|
e.getBindingResult().getAllErrors().stream()
|
||||||
|
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
return handleException(validationException);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package np.something.util.validation;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ValidationException extends RuntimeException {
|
||||||
|
public ValidationException(Set<String> errors) {
|
||||||
|
super(String.join("\n", errors));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package np.something.util.validation;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintViolation;
|
||||||
|
import jakarta.validation.Validation;
|
||||||
|
import jakarta.validation.Validator;
|
||||||
|
import jakarta.validation.ValidatorFactory;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ValidatorUtil {
|
||||||
|
private final Validator validator;
|
||||||
|
|
||||||
|
public ValidatorUtil() {
|
||||||
|
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||||
|
this.validator = factory.getValidator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void validate(T object) {
|
||||||
|
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||||
|
if (!errors.isEmpty()) {
|
||||||
|
throw new ValidationException(errors.stream()
|
||||||
|
.map(ConstraintViolation::getMessage)
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,11 @@
|
|||||||
|
spring.main.banner-mode=off
|
||||||
|
#server.port=8080
|
||||||
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
spring.h2.console.settings.trace=false
|
||||||
|
spring.h2.console.settings.web-allow-others=false
|
||||||
|
123
src/test/java/np/something/SocialNetworkTest.java
Normal file
123
src/test/java/np/something/SocialNetworkTest.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package np.something;
|
||||||
|
|
||||||
|
import np.something.model.*;
|
||||||
|
import np.something.services.CommentService;
|
||||||
|
import np.something.services.CustomerService;
|
||||||
|
import np.something.services.PostService;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class SocialNetworkTest {
|
||||||
|
@Autowired
|
||||||
|
CustomerService customerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CommentService commentService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
PostService postService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCustomers() {
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
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.getPassword(), c4.getPassword());
|
||||||
|
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPost() {
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
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.deleteAllComments();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testComment() {
|
||||||
|
commentService.deleteAllComments();
|
||||||
|
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.deleteAllComments();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package np.something;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class SomethingApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
6
src/test/resources/application.properties
Normal file
6
src/test/resources/application.properties
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
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=create-drop
|
Loading…
Reference in New Issue
Block a user