From 6eef082ee432bba2be6e5bc8b0b170d691aeb1d3 Mon Sep 17 00:00:00 2001 From: Jung Seonghun <80201773+seonghun-dev@users.noreply.github.com> Date: Sat, 20 Jul 2024 20:35:11 +0900 Subject: [PATCH] :sparkles: feat: add content search --- .../item/controller/ItemController.java | 5 +- .../item/repository/ItemRepository.java | 5 + .../domains/item/service/ItemService.java | 101 ++++++++++++------ .../src/api/domain/item/ItemApi.js | 9 ++ .../src/pages/items/ItemListPage.js | 22 +++- 5 files changed, 107 insertions(+), 35 deletions(-) diff --git a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/controller/ItemController.java b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/controller/ItemController.java index dab85cbe..da1b20b9 100644 --- a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/controller/ItemController.java +++ b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/controller/ItemController.java @@ -18,9 +18,10 @@ public class ItemController { @GetMapping public ResponseEntity getAllItems( @PageableDefault(size = 20, page = 0, sort = "createdAt", - direction = Sort.Direction.DESC) Pageable pageable + direction = Sort.Direction.DESC) Pageable pageable, + @RequestParam(value = "keyword", required = false) String keyword ) { - var response = itemService.getAllItems(pageable); + var response = itemService.getAllItems(pageable, keyword); return ResponseDto.ok(response); } diff --git a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java index 86ddcd0d..addb96d5 100644 --- a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java +++ b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java @@ -15,6 +15,11 @@ public interface ItemRepository extends JpaRepository { countQuery = "select count(i) from Item i") Page findAll(Pageable pageable); + + @Query(value = "select i from Item i join fetch i.song s join fetch s.album a join fetch a.artist ar join fetch i.itemLocation il join fetch il.villageArea va join fetch i.user u where i.content like %:keyword% ", + countQuery = "select count(i) from Item i where i.content like %:keyword%") + Page findAll(Pageable pageable, String keyword); + @Query(value = "select i from Item i join fetch i.song s join fetch s.album a join fetch a.artist ar join fetch i.itemLocation il join fetch il.villageArea va join fetch i.user u where u = :user order by i.createdAt desc ") List findByUser(User user); diff --git a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/service/ItemService.java b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/service/ItemService.java index 29d5bb7b..81555f51 100644 --- a/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/service/ItemService.java +++ b/backend/streetdrop-admin/streetdrop-admin-server/src/main/java/com/depromeet/domains/item/service/ItemService.java @@ -26,38 +26,75 @@ public class ItemService { private final ItemRepository itemRepository; @Transactional(readOnly = true) - public PageResponseDto getAllItems(Pageable pageable) { - Page itemPage = itemRepository.findAll(pageable); - PageMetaData pageMetaData = new PageMetaData( - itemPage.getNumber(), - itemPage.getSize(), - (int) itemPage.getTotalElements(), - itemPage.getTotalPages() - ); - List items = itemPage.getContent() - .stream() - .map((item) -> - { - String itemLocationName; - try { - itemLocationName = item.getItemLocation().getVillageArea().getVillageName(); - } catch (NullPointerException e){ - itemLocationName = ""; - } - - return ItemResponseDto.builder() - .id(item.getId()) - .songName(item.getSong().getName()) - .artistName(item.getSong().getAlbum().getArtist().getName()) - .dropLocationName(itemLocationName) - .userNickname(item.getUser().getNickname()) - .comment(item.getContent()) - .createdAt(item.getCreatedAt()) - .build(); - }).toList(); - - - return new PageResponseDto<>(items, pageMetaData); + public PageResponseDto getAllItems(Pageable pageable, String keyword) { + if (keyword == null) { + Page itemPage = itemRepository.findAll(pageable); + PageMetaData pageMetaData = new PageMetaData( + itemPage.getNumber(), + itemPage.getSize(), + (int) itemPage.getTotalElements(), + itemPage.getTotalPages() + ); + List items = itemPage.getContent() + .stream() + .map((item) -> + { + String itemLocationName; + try { + itemLocationName = item.getItemLocation().getVillageArea().getVillageName(); + } catch (NullPointerException e) { + itemLocationName = ""; + } + + return ItemResponseDto.builder() + .id(item.getId()) + .songName(item.getSong().getName()) + .artistName(item.getSong().getAlbum().getArtist().getName()) + .dropLocationName(itemLocationName) + .userNickname(item.getUser().getNickname()) + .comment(item.getContent()) + .createdAt(item.getCreatedAt()) + .build(); + }).toList(); + + + return new PageResponseDto<>(items, pageMetaData); + + } else { + Page itemPage = itemRepository.findAll(pageable, keyword); + PageMetaData pageMetaData = new PageMetaData( + itemPage.getNumber(), + itemPage.getSize(), + (int) itemPage.getTotalElements(), + itemPage.getTotalPages() + ); + List items = itemPage.getContent() + .stream() + .map((item) -> + { + String itemLocationName; + try { + itemLocationName = item.getItemLocation().getVillageArea().getVillageName(); + } catch (NullPointerException e) { + itemLocationName = ""; + } + + return ItemResponseDto.builder() + .id(item.getId()) + .songName(item.getSong().getName()) + .artistName(item.getSong().getAlbum().getArtist().getName()) + .dropLocationName(itemLocationName) + .userNickname(item.getUser().getNickname()) + .comment(item.getContent()) + .createdAt(item.getCreatedAt()) + .build(); + }).toList(); + + + return new PageResponseDto<>(items, pageMetaData); + + } + } @Transactional diff --git a/backend/streetdrop-admin/streetdrop-admin-web/src/api/domain/item/ItemApi.js b/backend/streetdrop-admin/streetdrop-admin-web/src/api/domain/item/ItemApi.js index 49bfc8a4..67fdad46 100644 --- a/backend/streetdrop-admin/streetdrop-admin-web/src/api/domain/item/ItemApi.js +++ b/backend/streetdrop-admin/streetdrop-admin-web/src/api/domain/item/ItemApi.js @@ -14,6 +14,15 @@ const ItemApi = { }, deleteItem: (itemId) => { return axiosAuthInstance.delete('/items/' + itemId) + }, + getItemsByKeyword : (page, size, keyword) => { + return axiosAuthInstance.get('/items', { + params: { + page: page, + size: size, + keyword: keyword + } + }) } } export default ItemApi; \ No newline at end of file diff --git a/backend/streetdrop-admin/streetdrop-admin-web/src/pages/items/ItemListPage.js b/backend/streetdrop-admin/streetdrop-admin-web/src/pages/items/ItemListPage.js index 82f7a989..b131b4fd 100644 --- a/backend/streetdrop-admin/streetdrop-admin-web/src/pages/items/ItemListPage.js +++ b/backend/streetdrop-admin/streetdrop-admin-web/src/pages/items/ItemListPage.js @@ -1,9 +1,11 @@ import React, {useEffect, useState} from "react" -import {Button, Drawer, Modal, Table} from 'antd'; +import {Button, Col, Drawer, Modal, Row, Table} from 'antd'; import BasicLayout from "../../layout/BasicLayout"; import ItemApi from "../../api/domain/item/ItemApi"; import ItemDetailPage from "../../components/items/ItemDetailPage"; import {InitialPagination} from "../../constant/PaginationConstant"; +import Search from "antd/es/input/Search"; + function ItemListPage() { const [data, setData] = useState([]); @@ -23,6 +25,12 @@ function ItemListPage() { }; + const onSearch = async (value) => { + const response = await ItemApi.getItemsByKeyword(tableParams.pagination.current - 1, tableParams.pagination.pageSize, value); + setData(response.data['data']); + } + + const showDrawer = (itemId) => { setClickedItemId(itemId) setOpenDrawer(true); @@ -127,6 +135,18 @@ function ItemListPage() {

아이템 전체 조회

아이템을 전체 조회할 수 있습니다.

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