LabWork04: Работа готова (Сдана)

This commit is contained in:
Safgerd 2023-04-18 12:17:43 +04:00
parent 2918fee687
commit 8348a9bf31
7 changed files with 161 additions and 45 deletions

View File

@ -1,65 +1,102 @@
<template> <template>
<div class="ms-5"> <div class="ms-5">
<p class='h3 m-3'>Посты</p> <div class="col-7">
<div class="row">
<input type="text" v-bind:id="'searchString'" class="col-4"/>
<button type="button" v-on:click="getSearchResult()" class="button col-2 secondary outline">Поиск</button>
<button type="button" v-on:click="searchMode = -1;" class="button col-2 secondary outline">Отмена</button>
</div>
</div>
<div class="row" v-if="currentCustomerId != -1"> <div v-if="!(searchMode == -1)">
<div class="col-7"> <div v-if="!(searchResults.length == 0)" class="row">
<div class="row is-left"> <div class="col-5">
<p class="col-2 is-left mb-2">Заголовок:</p> <div class="row mb-5 card" v-for="result in searchResults">
<input type="text" class="col-5" v-model="titleModal"/> <div class="col">
</div>
<div class="row is-left"> <div v-if="result['title'] == null">
<p class="col-2 is-left mb-2">Текст:</p> <div class="row is-left mt-2">
<textarea type="textarea" class="col-5" v-model="postContentModal"/> <span class="h3">Комментарий</span>
</div> <!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
<div class="row is-left"> <span class="h3">Контент: {{ result['content'] }}</span>
<button type='button' class="button btn-primary col-7" v-on:click='createPost'> </div>
Опубликовать </div>
</button> <div v-else>
<div class="row is-left mt-2">
<span class="h3">Пост</span>
<!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
<span class="h3">Оглавление: {{ result['title'] }}</span>
<span class="h3">Контент: {{ result['content'] }}</span>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
<div v-if="!(posts.length == 0)" class="row"> <div v-if="searchMode == -1">
<div class="col-5"> <p class='h3 m-3'>Посты</p>
<div class="row mb-5 card" v-for="post in posts">
<div class="col">
<div class="row is-left my-3">
<p class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></p>
</div>
<div class="row text-left"> <div class="row" v-if="currentCustomerId != -1">
<span class="h2">{{ post['title'] }}</span> <div class="col-7">
<span class="h3">{{ post['content'] }}</span> <div class="row is-left">
</div> <p class="col-2 is-left mb-2">Заголовок:</p>
<input type="text" class="col-5" v-model="titleModal"/>
</div>
<div class="row is-left">
<p class="col-2 is-left mb-2">Текст:</p>
<textarea type="textarea" class="col-5" v-model="postContentModal"/>
</div>
<div class="row is-left">
<button type='button' class="button btn-primary col-7" v-on:click='createPost'>
Опубликовать
</button>
</div>
</div>
</div>
<div class="row"> <div v-if="!(posts.length == 0)" class="row">
<div v-if="!(post['comments'].length == 0)"> <div class="col-5">
<p class="row h3 is-left my-3">Комментарии:</p> <div class="row mb-5 card" v-for="post in posts">
<div class="col">
<div class="row is-left my-3">
<p class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></p>
</div>
<div class="row text-left mb-4 card" v-for="comment in post['comments']"> <div class="row text-left">
<div class="row is-left"> <span class="h2">{{ post['title'] }}</span>
<span class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></span> <span class="h3">{{ post['content'] }}</span>
<span class="h3">{{ comment['content'] }}</span> </div>
<div class="row">
<div v-if="!(post['comments'].length == 0)">
<p class="row h3 is-left my-3">Комментарии:</p>
<div class="row text-left mb-4 card" v-for="comment in post['comments']">
<div class="row is-left">
<span class="h2"><router-link v-bind:to="'/customers/' + post['customerId']" class="text-primary">{{ post['customerName'] }}</router-link></span>
<span class="h3">{{ comment['content'] }}</span>
</div>
<div v-if="selectedCustomerContainsComment(comment) == true" class="row mt-3">
<button v-on:click="selectedCommentId = comment['id']; selectedPostTitle = post['title']" class="button primary outline col" data-bs-toggle="modal" data-bs-target="#commentEdit">Изменить</button>
<button v-on:click="deleteComment(comment['id'])" class="button error col">Удалить</button>
</div>
</div> </div>
</div>
<div v-if="selectedCustomerContainsComment(comment) == true" class="row mt-3"> <div class="row" v-if="currentCustomerId != -1">
<button v-on:click="selectedCommentId = comment['id']; selectedPostTitle = post['title']" class="button primary outline col" data-bs-toggle="modal" data-bs-target="#commentEdit">Изменить</button> <input type="text" v-bind:id="'post-comment-' + post['id']" class="col-7"/>
<button v-on:click="deleteComment(comment['id'])" class="button error col">Удалить</button> <button type="button" v-on:click="selectedPostId = post['id']; selectedPostTitle = post['title']; createComment()" class="button col-5 secondary outline">Комментировать</button>
</div>
</div> </div>
</div> </div>
<div class="row" v-if="currentCustomerId != -1"> <div class="row" v-if="post['customerId'] == currentCustomerId">
<input type="text" v-bind:id="'post-comment-' + post['id']" class="col-7"/> <button type="button" v-on:click="deletePost(post['id'])" class="col button dark outline">Удалить пост</button>
<button type="button" v-on:click="selectedPostId = post['id']; selectedPostTitle = post['title']; createComment()" class="button col-5 secondary 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 class="row" v-if="post['customerId'] == currentCustomerId">
<button type="button" v-on:click="deletePost(post['id'])" class="col button dark outline">Удалить пост</button>
<button type="button" v-on:click="selectedPostId = post['id']" class="col button primary outline" data-bs-toggle="modal" data-bs-target="#postEdit">Изменить пост</button>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -118,6 +155,7 @@ import axios from 'axios'
export default { export default {
data() { data() {
return { return {
searchResults: [],
customers: [], customers: [],
selectedCustomerId: 0, selectedCustomerId: 0,
currentCustomer: {}, currentCustomer: {},
@ -129,9 +167,27 @@ export default {
postContentModal: '', postContentModal: '',
selectedCommentId: 0, selectedCommentId: 0,
currentCustomerId: -1, currentCustomerId: -1,
searchMode: -1,
} }
}, },
methods: { methods: {
async getSearchResult(){
const content = document.getElementById("searchString").value
if (content != ''){
this.searchMode = 0;
this.searchResults = [];
const responseSearch = await axios.post('http://localhost:8080/search?searchStr=' + content);
responseSearch.data.forEach(element => {
this.searchResults.push(element);
console.log(element);
});
}
else{
this.searchMode = -1;
this.refreshList();
}
},
selectedCustomerContainsComment(comment) { selectedCustomerContainsComment(comment) {
var customer = this.customers.find(element => element['id'] == this.currentCustomerId); var customer = this.customers.find(element => element['id'] == this.currentCustomerId);
console.log(customer); console.log(customer);

View File

@ -1,11 +1,17 @@
package ru.ulstu.is.labwork.Lab4.controller; package ru.ulstu.is.labwork.Lab4.controller;
import org.springframework.web.bind.annotation.*; 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.DTO.PostDto;
import ru.ulstu.is.labwork.Lab4.model.Comment;
import ru.ulstu.is.labwork.Lab4.model.Post;
import ru.ulstu.is.labwork.Lab4.services.CommentService;
import ru.ulstu.is.labwork.Lab4.services.CustomerService; import ru.ulstu.is.labwork.Lab4.services.CustomerService;
import ru.ulstu.is.labwork.Lab4.services.PostService; import ru.ulstu.is.labwork.Lab4.services.PostService;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/post") @RequestMapping("/post")

View File

@ -0,0 +1,34 @@
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<Object> getResult(@RequestParam("searchStr") String searchStr) {
List<Object> 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;
}
}

View File

@ -3,5 +3,8 @@ package ru.ulstu.is.labwork.Lab4.repositories;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.labwork.Lab4.model.Comment; import ru.ulstu.is.labwork.Lab4.model.Comment;
import java.util.List;
public interface CommentRepository extends JpaRepository<Comment, Long> { public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findByContentLikeIgnoreCase(String text);
} }

View File

@ -1,7 +1,11 @@
package ru.ulstu.is.labwork.Lab4.repositories; package ru.ulstu.is.labwork.Lab4.repositories;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.ulstu.is.labwork.Lab4.model.Post; import ru.ulstu.is.labwork.Lab4.model.Post;
import java.util.List;
public interface PostRepository extends JpaRepository<Post, Long> { public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByContentLikeIgnoreCase(String text);
} }

View File

@ -19,6 +19,13 @@ public class CommentService {
public CommentService(CommentRepository commentRepository) { public CommentService(CommentRepository commentRepository) {
this.commentRepository = commentRepository; this.commentRepository = commentRepository;
} }
@Transactional
public List<Comment> findFilteredComments(String filter) {
return commentRepository.findByContentLikeIgnoreCase("%" + filter + "%");
}
@Transactional @Transactional
public Comment findComment(Long id) { public Comment findComment(Long id) {
final Optional<Comment> comment = commentRepository.findById(id); final Optional<Comment> comment = commentRepository.findById(id);

View File

@ -2,10 +2,12 @@ package ru.ulstu.is.labwork.Lab4.services;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import ru.ulstu.is.labwork.Lab4.model.Comment;
import ru.ulstu.is.labwork.Lab4.model.Customer; import ru.ulstu.is.labwork.Lab4.model.Customer;
import ru.ulstu.is.labwork.Lab4.model.Post; import ru.ulstu.is.labwork.Lab4.model.Post;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.ulstu.is.labwork.Lab4.repositories.CommentRepository;
import ru.ulstu.is.labwork.Lab4.repositories.PostRepository; import ru.ulstu.is.labwork.Lab4.repositories.PostRepository;
import java.util.List; import java.util.List;
@ -18,6 +20,10 @@ public class PostService {
public PostService(PostRepository postRepository) { public PostService(PostRepository postRepository) {
this.postRepository = postRepository; this.postRepository = postRepository;
} }
@Transactional
public List<Post> findFilteredPosts(String filter) {
return postRepository.findByContentLikeIgnoreCase("%" + filter + "%");
}
@Transactional @Transactional
public Post findPost(Long id) { public Post findPost(Long id) {