From 22244169889d723f40629c7dec0f193ed1b6be60 Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Tue, 4 Apr 2023 12:03:56 +0400 Subject: [PATCH] Lab4 is done --- .gitignore | 2 + frontend/node_modules/fs/README.md | 9 - frontend/node_modules/fs/package.json | 46 ---- frontend/node_modules/http/README.md | 9 - frontend/node_modules/http/package.json | 39 --- frontend/package-lock.json | 4 +- frontend/package.json | 2 +- frontend/src/App.vue | 38 ++- frontend/src/components/Comment.vue | 194 --------------- .../{Customer.vue => Customers.vue} | 40 ++- frontend/src/components/Post.vue | 188 --------------- frontend/src/components/Posts.vue | 227 ++++++++++++++++++ frontend/src/main.js | 19 +- .../java/np/something/DTO/CommentDto.java | 16 ++ src/main/java/np/something/DTO/PostDto.java | 16 ++ src/main/java/np/something/model/Comment.java | 12 + src/main/java/np/something/model/Post.java | 12 + .../np/something/services/CommentService.java | 2 + .../np/something/services/PostService.java | 2 + 19 files changed, 362 insertions(+), 515 deletions(-) delete mode 100644 frontend/node_modules/fs/README.md delete mode 100644 frontend/node_modules/fs/package.json delete mode 100644 frontend/node_modules/http/README.md delete mode 100644 frontend/node_modules/http/package.json delete mode 100644 frontend/src/components/Comment.vue rename frontend/src/components/{Customer.vue => Customers.vue} (85%) delete mode 100644 frontend/src/components/Post.vue create mode 100644 frontend/src/components/Posts.vue diff --git a/.gitignore b/.gitignore index f1e9216..f22e3fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /frontend/node_modules/ +/build +/.idea diff --git a/frontend/node_modules/fs/README.md b/frontend/node_modules/fs/README.md deleted file mode 100644 index 5e9a74c..0000000 --- a/frontend/node_modules/fs/README.md +++ /dev/null @@ -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. diff --git a/frontend/node_modules/fs/package.json b/frontend/node_modules/fs/package.json deleted file mode 100644 index e92ec55..0000000 --- a/frontend/node_modules/fs/package.json +++ /dev/null @@ -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" -} diff --git a/frontend/node_modules/http/README.md b/frontend/node_modules/http/README.md deleted file mode 100644 index 5e9a74c..0000000 --- a/frontend/node_modules/http/README.md +++ /dev/null @@ -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. diff --git a/frontend/node_modules/http/package.json b/frontend/node_modules/http/package.json deleted file mode 100644 index cd334ca..0000000 --- a/frontend/node_modules/http/package.json +++ /dev/null @@ -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" -} diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 79c0b0d..813074a 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -1,11 +1,11 @@ { - "name": "lab4_vue_front", + "name": "front", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "lab4_vue_front", + "name": "front", "version": "0.1.0", "dependencies": { "@popperjs/core": "^2.11.7", diff --git a/frontend/package.json b/frontend/package.json index 56405ff..f0f576e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,5 +1,5 @@ { - "name": "lab4_vue_front", + "name": "front", "version": "0.1.0", "private": true, "scripts": { diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 1aa93f3..6845d17 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,20 +1,44 @@ diff --git a/frontend/src/components/Comment.vue b/frontend/src/components/Comment.vue deleted file mode 100644 index a6924b7..0000000 --- a/frontend/src/components/Comment.vue +++ /dev/null @@ -1,194 +0,0 @@ - - - \ No newline at end of file diff --git a/frontend/src/components/Customer.vue b/frontend/src/components/Customers.vue similarity index 85% rename from frontend/src/components/Customer.vue rename to frontend/src/components/Customers.vue index 162042d..a50b20d 100644 --- a/frontend/src/components/Customer.vue +++ b/frontend/src/components/Customers.vue @@ -42,7 +42,7 @@

Нет постов

-
+
@@ -104,6 +104,7 @@ export default { usernameModal: '', passwordModal: '', selectedCustomer: {}, + currentCustomerId: -1 } }, methods: { @@ -120,22 +121,41 @@ export default { this.refreshList(); }, async refreshList() { - const response = await axios.get('http://localhost:8080/customer'); 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 beforeMount() { - const response = await axios.get('http://localhost:8080/customer'); - response.data.forEach(element => { - this.customers.push(element); - console.log(element); - }); + }, - } + }, + 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(); + }, + + } diff --git a/frontend/src/components/Post.vue b/frontend/src/components/Post.vue deleted file mode 100644 index cea80d8..0000000 --- a/frontend/src/components/Post.vue +++ /dev/null @@ -1,188 +0,0 @@ - - - - \ No newline at end of file diff --git a/frontend/src/components/Posts.vue b/frontend/src/components/Posts.vue new file mode 100644 index 0000000..4435368 --- /dev/null +++ b/frontend/src/components/Posts.vue @@ -0,0 +1,227 @@ + + + \ No newline at end of file diff --git a/frontend/src/main.js b/frontend/src/main.js index dc48cea..aafcf65 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -2,22 +2,21 @@ import {createApp} from 'vue' import App from './App' import { createRouter, createWebHistory } from "vue-router" -import Customer from './components/Customer' -import Post from './components/Post' -import Comment from './components/Comment' +import Customers from './components/Customers' +import Posts from './components/Posts' const routes = [ { - path: '/customers', - component: Customer + path: '/customers/:id?', + name: "Customers", + component: Customers, + props: true }, { path: '/posts', - component: Post - }, - { - path: '/comments', - component: Comment + name: "Posts", + component: Posts, + props: true } ] diff --git a/src/main/java/np/something/DTO/CommentDto.java b/src/main/java/np/something/DTO/CommentDto.java index 018d01f..f34b93d 100644 --- a/src/main/java/np/something/DTO/CommentDto.java +++ b/src/main/java/np/something/DTO/CommentDto.java @@ -2,12 +2,18 @@ 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(); @@ -15,6 +21,8 @@ public class CommentDto { 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() { @@ -34,4 +42,12 @@ public class CommentDto { public String getPostAuthor() { return postAuthor; } + + public long getPostAuthorId() { + return postAuthorId; + } + + public String getCreateDate() { + return createDate; + } } diff --git a/src/main/java/np/something/DTO/PostDto.java b/src/main/java/np/something/DTO/PostDto.java index 178f196..0a82e33 100644 --- a/src/main/java/np/something/DTO/PostDto.java +++ b/src/main/java/np/something/DTO/PostDto.java @@ -2,6 +2,8 @@ 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 { @@ -9,14 +11,20 @@ public class PostDto { public final String title; public final String content; public final String customerName; + + public final long customerId; public final List 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() { @@ -38,4 +46,12 @@ public class PostDto { public List getComments() { return comments; } + + public long getCustomerId() { + return customerId; + } + + public String getCreateDate() { + return createDate; + } } diff --git a/src/main/java/np/something/model/Comment.java b/src/main/java/np/something/model/Comment.java index 874af09..650e37e 100644 --- a/src/main/java/np/something/model/Comment.java +++ b/src/main/java/np/something/model/Comment.java @@ -3,6 +3,7 @@ package np.something.model; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; +import java.time.LocalDateTime; import java.util.Objects; @Entity @@ -23,6 +24,9 @@ public class Comment { @JoinColumn(name="post_fk") private Post post; + @Column(columnDefinition = "timestamp default NOW()") + private LocalDateTime createDate; + public Comment() { } @@ -67,4 +71,12 @@ public class Comment { public int hashCode() { return Objects.hash(id); } + + public LocalDateTime getCreateDate() { + return createDate; + } + + public void setCreateDate(LocalDateTime createDate) { + this.createDate = createDate; + } } diff --git a/src/main/java/np/something/model/Post.java b/src/main/java/np/something/model/Post.java index 5b668fb..ccf6d86 100644 --- a/src/main/java/np/something/model/Post.java +++ b/src/main/java/np/something/model/Post.java @@ -3,6 +3,7 @@ package np.something.model; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -27,6 +28,9 @@ public class Post { @OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL) private List comments; + @Column(columnDefinition = "timestamp default NOW()") + private LocalDateTime createDate; + public Post() { } @@ -80,4 +84,12 @@ public class Post { public Long getId() { return id; } + + public LocalDateTime getCreateDate() { + return createDate; + } + + public void setCreateDate(LocalDateTime createDate) { + this.createDate = createDate; + } } diff --git a/src/main/java/np/something/services/CommentService.java b/src/main/java/np/something/services/CommentService.java index 7fddb13..a70240e 100644 --- a/src/main/java/np/something/services/CommentService.java +++ b/src/main/java/np/something/services/CommentService.java @@ -9,6 +9,7 @@ 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; @@ -37,6 +38,7 @@ public class CommentService { @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); diff --git a/src/main/java/np/something/services/PostService.java b/src/main/java/np/something/services/PostService.java index e3d40d1..74c809c 100644 --- a/src/main/java/np/something/services/PostService.java +++ b/src/main/java/np/something/services/PostService.java @@ -14,6 +14,7 @@ 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 @@ -40,6 +41,7 @@ public class PostService { @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);