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,5 +1,41 @@
<template>
<div class="ms-5">
<div class="col-7">
<div class="row">
<input type="text" v-bind:id="'searchString'" class="col-4"/>
<button type="button" v-on:click="getSearchResult()" class="button col-2 secondary outline">Поиск</button>
<button type="button" v-on:click="searchMode = -1;" class="button col-2 secondary outline">Отмена</button>
</div>
</div>
<div v-if="!(searchMode == -1)">
<div v-if="!(searchResults.length == 0)" class="row">
<div class="col-5">
<div class="row mb-5 card" v-for="result in searchResults">
<div class="col">
<div v-if="result['title'] == null">
<div class="row is-left mt-2">
<span class="h3">Комментарий</span>
<!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
<span class="h3">Контент: {{ result['content'] }}</span>
</div>
</div>
<div v-else>
<div class="row is-left mt-2">
<span class="h3">Пост</span>
<!-- <p class="text-primary h2">Пользователь: {{ result['customerName'] }}</p> -->
<span class="h3">Оглавление: {{ result['title'] }}</span>
<span class="h3">Контент: {{ result['content'] }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div v-if="searchMode == -1">
<p class='h3 m-3'>Посты</p>
<div class="row" v-if="currentCustomerId != -1">
@ -65,6 +101,7 @@
</div>
</div>
</div>
</div>
<!-- Modal -->
@ -118,6 +155,7 @@ import axios from 'axios'
export default {
data() {
return {
searchResults: [],
customers: [],
selectedCustomerId: 0,
currentCustomer: {},
@ -129,9 +167,27 @@ export default {
postContentModal: '',
selectedCommentId: 0,
currentCustomerId: -1,
searchMode: -1,
}
},
methods: {
async getSearchResult(){
const content = document.getElementById("searchString").value
if (content != ''){
this.searchMode = 0;
this.searchResults = [];
const responseSearch = await axios.post('http://localhost:8080/search?searchStr=' + content);
responseSearch.data.forEach(element => {
this.searchResults.push(element);
console.log(element);
});
}
else{
this.searchMode = -1;
this.refreshList();
}
},
selectedCustomerContainsComment(comment) {
var customer = this.customers.find(element => element['id'] == this.currentCustomerId);
console.log(customer);

View File

@ -1,11 +1,17 @@
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.model.Comment;
import ru.ulstu.is.labwork.Lab4.model.Post;
import ru.ulstu.is.labwork.Lab4.services.CommentService;
import ru.ulstu.is.labwork.Lab4.services.CustomerService;
import ru.ulstu.is.labwork.Lab4.services.PostService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@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 ru.ulstu.is.labwork.Lab4.model.Comment;
import java.util.List;
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;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.ulstu.is.labwork.Lab4.model.Post;
import java.util.List;
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) {
this.commentRepository = commentRepository;
}
@Transactional
public List<Comment> findFilteredComments(String filter) {
return commentRepository.findByContentLikeIgnoreCase("%" + filter + "%");
}
@Transactional
public Comment findComment(Long 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.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.Post;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ru.ulstu.is.labwork.Lab4.repositories.CommentRepository;
import ru.ulstu.is.labwork.Lab4.repositories.PostRepository;
import java.util.List;
@ -18,6 +20,10 @@ public class PostService {
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
@Transactional
public List<Post> findFilteredPosts(String filter) {
return postRepository.findByContentLikeIgnoreCase("%" + filter + "%");
}
@Transactional
public Post findPost(Long id) {