From 60c25642297628f16536de165d5e35ba96f4c9b3 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Tue, 2 May 2023 01:57:12 +0400 Subject: [PATCH] =?UTF-8?q?LabWork05:=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 6 + lab4-vue-front/src/App.vue | 2 +- lab4-vue-front/src/components/Customers.vue | 22 ++- lab4-vue-front/src/components/Posts.vue | 40 +++-- .../ulstu/is/labwork/Lab4/DTO/CommentDto.java | 70 ++++++++- .../is/labwork/Lab4/DTO/CustomerDto.java | 54 +++++-- .../ru/ulstu/is/labwork/Lab4/DTO/PostDto.java | 55 +++++-- .../Lab4/controller/CommentController.java | 17 ++- .../Lab4/controller/CustomerController.java | 12 +- .../Lab4/controller/PostController.java | 22 +-- .../Lab4/controller/SearchController.java | 34 ----- .../ulstu/is/labwork/Lab4/mvc/Comments.java | 55 +++++++ .../ulstu/is/labwork/Lab4/mvc/Customers.java | 62 ++++++++ .../ru/ulstu/is/labwork/Lab4/mvc/Feed.java | 65 ++++++++ .../ru/ulstu/is/labwork/Lab4/mvc/Posts.java | 56 +++++++ .../ru/ulstu/is/labwork/Lab4/mvc/Session.java | 16 ++ .../Lab4/repositories/CommentRepository.java | 7 +- .../Lab4/repositories/PostRepository.java | 6 +- .../labwork/Lab4/services/CommentService.java | 6 +- .../is/labwork/Lab4/services/PostService.java | 4 +- .../ru/ulstu/is/labwork/WebConfiguration.java | 1 + src/main/resources/templates/customers.html | 103 +++++++++++++ src/main/resources/templates/default.html | 42 +++++ src/main/resources/templates/feed.html | 144 ++++++++++++++++++ 24 files changed, 784 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/controller/SearchController.java create mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java create mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java create mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java create mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java create mode 100644 src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java create mode 100644 src/main/resources/templates/customers.html create mode 100644 src/main/resources/templates/default.html create mode 100644 src/main/resources/templates/feed.html diff --git a/build.gradle b/build.gradle index 8812aa1..47c857c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,12 @@ dependencies { 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' diff --git a/lab4-vue-front/src/App.vue b/lab4-vue-front/src/App.vue index c5454d1..230227f 100644 --- a/lab4-vue-front/src/App.vue +++ b/lab4-vue-front/src/App.vue @@ -31,7 +31,7 @@ export default { }, async beforeMount() { setInterval(async () => { - const response = await axios.get('http://localhost:8080/customer'); + const response = await axios.get('http://localhost:8080/api/customer'); this.customers = []; response.data.forEach(element => { this.customers.push(element); diff --git a/lab4-vue-front/src/components/Customers.vue b/lab4-vue-front/src/components/Customers.vue index ee34025..b581ae4 100644 --- a/lab4-vue-front/src/components/Customers.vue +++ b/lab4-vue-front/src/components/Customers.vue @@ -101,27 +101,35 @@ export default { }, methods: { async createUser(){ - const response = await axios.post('http://localhost:8080/customer?username=' + this.usernameModal + '&password=' + this.passwordModal); + 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/customer/' + id); + const response = await axios.delete('http://localhost:8080/api/customer/' + id); this.refreshList(); }, async editUser() { - const response = await axios.put('http://localhost:8080/customer/' + this.selectedCustomer['id'] + '?username=' + this.usernameModal + '&password=' + this.passwordModal); + 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/customer'); + 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/customer/' + this.$route.params.id); + const response = await axios.get('http://localhost:8080/api/customer/' + this.$route.params.id); this.customers.push(response.data) } }, @@ -131,13 +139,13 @@ export default { 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'); + 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/customer/' + this.$route.params.id); + const response = await axios.get('http://localhost:8080/api/customer/' + this.$route.params.id); this.customers.push(response.data) } diff --git a/lab4-vue-front/src/components/Posts.vue b/lab4-vue-front/src/components/Posts.vue index 74a6f10..c71d1e1 100644 --- a/lab4-vue-front/src/components/Posts.vue +++ b/lab4-vue-front/src/components/Posts.vue @@ -203,47 +203,67 @@ export default { 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); + 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/comment/' + commentId); + await axios.delete('http://localhost:8080/api/comment/' + commentId); this.refreshList(); }, async editComment(){ - await axios.put('http://localhost:8080/comment/' + this.selectedCommentId + '?text=' + this.contentModal); + 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() { - const response = await axios.post('http://localhost:8080/post?title=' + this.titleModal + '&content=' + this.postContentModal + '&authorId=' + this.currentCustomerId); + 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/post/' + postId); + const response = await axios.delete('http://localhost:8080/api/post/' + postId); this.refreshList(); }, async editPost(){ - const response = await axios.put('http://localhost:8080/post/' + this.selectedPostId + '?title=' + this.titleModal + '&content=' + this.postContentModal); + 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/customer'); + 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/post'); + const responsePost = await axios.get('http://localhost:8080/api/post'); responsePost.data.forEach(element => { this.posts.splice(0, 0, element); console.log(element); @@ -253,7 +273,7 @@ export default { async beforeMount() { this.currentCustomerId = history.state; setInterval(async () => this.currentCustomerId = history.state, 50) - const responseCustomer = await axios.get('http://localhost:8080/customer'); + const responseCustomer = await axios.get('http://localhost:8080/api/customer'); responseCustomer.data.forEach(element => { this.customers.push(element); if (element['id'] == this.currentCustomerId) { @@ -261,7 +281,7 @@ export default { } console.log(element); }); - const responsePost = await axios.get('http://localhost:8080/post'); + const responsePost = await axios.get('http://localhost:8080/api/post'); responsePost.data.forEach(element => { this.posts.splice(0, 0, element); console.log(element); diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java index 0333138..ee3c29a 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CommentDto.java @@ -1,38 +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 final Long id; - public final String content; - public final String customerName; - public final String postTitle; - public final String postAuthor; - public final long postAuthorId; + 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 String getPostTitle() {return postTitle;} + + 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)); + } } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java index c132be7..1bbfda1 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/CustomerDto.java @@ -1,18 +1,19 @@ package ru.ulstu.is.labwork.Lab4.DTO; -import ru.ulstu.is.labwork.Lab4.model.Comment; +import com.fasterxml.jackson.annotation.JsonProperty; import ru.ulstu.is.labwork.Lab4.model.Customer; -import ru.ulstu.is.labwork.Lab4.model.Post; - -import java.util.ArrayList; import java.util.List; public class CustomerDto { - public final Long id; - public final String username; - public final String password; - public final List comments; - public final List posts; + public long id; + public String username; + public String password; + public List comments; + public List posts; + + public CustomerDto() { + + } public CustomerDto(Customer customer){ this.id = customer.getId(); @@ -22,9 +23,34 @@ public class CustomerDto { 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 getComments() {return comments;} - public List getPosts() {return posts;} + @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 getComments() { + return comments; + } + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public List getPosts() { + return posts; + } } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java index f3f2a2b..3dd76cd 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/DTO/PostDto.java @@ -1,18 +1,22 @@ package ru.ulstu.is.labwork.Lab4.DTO; -import ru.ulstu.is.labwork.Lab4.model.Comment; +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 final Long id; - public final String title; - public final String content; - public final String customerName; - public final long customerId; - public final List comments; + public long id; + public String title; + public String content; + public String customerName; + public long customerId; + public ArrayList comments; + + public PostDto() { + + } public PostDto(Post post){ this.id = post.getId(); @@ -20,17 +24,44 @@ public class PostDto { this.content = post.getContent(); this.customerName = post.getCustomer().getUsername(); this.customerId = post.getCustomer().getId(); - this.comments = post.getComments().stream().map(CommentDto::new).toList(); + this.comments = new ArrayList<>(post.getComments().stream().map(CommentDto::new).toList()); } - public Long getId() { return id; } - public String getTitle() { return title; } - public String getContent() { return content; } + @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; } - public List getComments() { return comments; } + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public List getComments() { + return comments; + } + public long getCustomerId() { return customerId; } + + public void setCustomerId(long customerId) { + this.customerId = customerId; + } } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CommentController.java b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CommentController.java index d6454b9..9e9cb1d 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CommentController.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CommentController.java @@ -1,15 +1,18 @@ 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("/comment") +@RequestMapping(WebConfiguration.REST_API + "/comment") public class CommentController { private final CommentService commentService; private final CustomerService customerService; @@ -34,14 +37,18 @@ public class CommentController { } @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); + 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(@RequestParam("text") String text, @PathVariable Long id) { - return new CommentDto(commentService.updateComment(id, text)); + public CommentDto updateComment(@RequestBody @Valid CommentDto commentDto, @PathVariable Long id) { + return new CommentDto(commentService.updateComment(id, commentDto.getContent())); } @DeleteMapping("/{id}") diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CustomerController.java b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CustomerController.java index 357ab85..5c8e6a8 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CustomerController.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/CustomerController.java @@ -1,14 +1,16 @@ 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("/customer") +@RequestMapping(WebConfiguration.REST_API + "/customer") public class CustomerController { private final CustomerService customerService; @@ -29,14 +31,14 @@ public class CustomerController { } @PostMapping - public CustomerDto createCustomer(@RequestParam("username") String username, @RequestParam("password") String password){ - final Customer customer = customerService.addCustomer(username, password); + 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(@RequestParam("username") String username, @RequestParam("password") String password, @PathVariable Long id) { - return new CustomerDto(customerService.updateCustomer(id, username, password)); + public CustomerDto updateCustomer(@RequestBody @Valid CustomerDto customerDto, @PathVariable Long id) { + return new CustomerDto(customerService.updateCustomer(id, customerDto.getUsername(), customerDto.getPassword())); } @DeleteMapping("/{id}") diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/PostController.java b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/PostController.java index 119c525..326cf43 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/PostController.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/PostController.java @@ -1,5 +1,6 @@ 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; @@ -8,13 +9,14 @@ 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("/post") +@RequestMapping(WebConfiguration.REST_API + "/post") public class PostController { private final PostService postService; private final CustomerService customerService; @@ -37,23 +39,13 @@ public class PostController { } @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)); + 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( - @PathVariable Long id, - @RequestParam("title") String title, - @RequestParam("content") String content - ) - { - return new PostDto(postService.updatePost(id, title, content)); + public PostDto updatePost(@RequestBody @Valid PostDto postDto, @PathVariable Long id) { + return new PostDto(postService.updatePost(id, postDto.title, postDto.content)); } @DeleteMapping("/{id}") diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/SearchController.java b/src/main/java/ru/ulstu/is/labwork/Lab4/controller/SearchController.java deleted file mode 100644 index 1e3f6b2..0000000 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/controller/SearchController.java +++ /dev/null @@ -1,34 +0,0 @@ -package ru.ulstu.is.labwork.Lab4.controller; - -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.services.CommentService; -import ru.ulstu.is.labwork.Lab4.services.PostService; - -import java.util.ArrayList; -import java.util.List; - -@RestController -@RequestMapping("/search") -public class SearchController { - private final CommentService commentService; - private final PostService postService; - - public SearchController(PostService postService, CommentService commentService){ - this.commentService = commentService; - this.postService = postService; - } - - @PostMapping - public List getResult(@RequestParam("searchStr") String searchStr) { - List result = new ArrayList<>(); - result.addAll(commentService.findFilteredComments(searchStr).stream() - .map(CommentDto::new) - .toList()); - result.addAll(postService.findFilteredPosts(searchStr).stream() - .map(PostDto::new) - .toList()); - return result; - } -} diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java new file mode 100644 index 0000000..c2e471b --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Comments.java @@ -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"; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java new file mode 100644 index 0000000..7d33374 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Customers.java @@ -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/"; + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java new file mode 100644 index 0000000..2698c68 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java @@ -0,0 +1,65 @@ +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, 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 = new ArrayList<>(postService.searchPosts(search)); + var comments = commentService.searchComments(search); + 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); + } + } + model.addAttribute("posts", posts.stream().map(PostDto::new).toList()); + } + model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); + + return "/feed"; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java new file mode 100644 index 0000000..78c9c15 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Posts.java @@ -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"; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java new file mode 100644 index 0000000..38650b9 --- /dev/null +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Session.java @@ -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 updateSession(@RequestParam("currentCustomerId") int currentCustomerId, HttpSession session) { + session.setAttribute("currentCustomerId", currentCustomerId); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java index 48d833a..701ebeb 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java @@ -2,9 +2,14 @@ package ru.ulstu.is.labwork.Lab4.repositories; 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 { - List findByContentLikeIgnoreCase(String text); + @Query("SELECT DISTINCT c FROM Comment c WHERE c.content LIKE %:tag%") + List searchComments(@Param("tag") String tag); } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java index 9adb2ef..dac2360 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java @@ -3,9 +3,13 @@ package ru.ulstu.is.labwork.Lab4.repositories; 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; import java.util.List; public interface PostRepository extends JpaRepository { - List findByContentLikeIgnoreCase(String text); + @Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%") + List searchPosts(@Param("tag") String tag); } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java b/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java index 3eb2fd1..2814a2a 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java @@ -20,9 +20,9 @@ public class CommentService { this.commentRepository = commentRepository; } - @Transactional - public List findFilteredComments(String filter) { - return commentRepository.findByContentLikeIgnoreCase("%" + filter + "%"); + @jakarta.transaction.Transactional + public List searchComments(String tag) { + return commentRepository.searchComments(tag); } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java b/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java index 7c3c339..5b05229 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java @@ -21,8 +21,8 @@ public class PostService { this.postRepository = postRepository; } @Transactional - public List findFilteredPosts(String filter) { - return postRepository.findByContentLikeIgnoreCase("%" + filter + "%"); + public List searchPosts(String tag) { + return postRepository.searchPosts(tag); } @Transactional diff --git a/src/main/java/ru/ulstu/is/labwork/WebConfiguration.java b/src/main/java/ru/ulstu/is/labwork/WebConfiguration.java index 2de704e..ec33660 100644 --- a/src/main/java/ru/ulstu/is/labwork/WebConfiguration.java +++ b/src/main/java/ru/ulstu/is/labwork/WebConfiguration.java @@ -6,6 +6,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfiguration implements WebMvcConfigurer { + public static final String REST_API = "/api"; @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedMethods("*"); diff --git a/src/main/resources/templates/customers.html b/src/main/resources/templates/customers.html new file mode 100644 index 0000000..5dc9af5 --- /dev/null +++ b/src/main/resources/templates/customers.html @@ -0,0 +1,103 @@ + + + + + +
+
+

Профили

+ + +

Список

+
+
+
+
+
+
+ +
+

Посты:

+
+
+
+
+
+
+
+ +
+

Комментарии:

+
+
+
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+ + + + + + +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..e3eaa1d --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,42 @@ + + + + + + + + + + LabWork04 - Social Network + + + +
+
+

LabWork04 - Social Network

+
+ +
+
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html new file mode 100644 index 0000000..f59da7c --- /dev/null +++ b/src/main/resources/templates/feed.html @@ -0,0 +1,144 @@ + + + + + +
+
+

Посты

+ +
+
+ + +
+
+ +
+
+ +
+

Заголовок:

+ +
+
+

Текст:

+ +
+
+ +
+
+
+ +
+
+
+
+ +
+

+
+ +
+ + +
+ +
+
+

Комментарии

+ +
+
+ + +
+
+ +
+ +
+
+
+
+ +
+ + + + +
+
+ +
+
+ +
+ +
+ +
+
+
+
+ +
+ + + + + + +
+ + + + + \ No newline at end of file