diff --git a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java index ca68172..591b92d 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java @@ -48,7 +48,14 @@ public List findMembers(long groupId) { .toList(); } - public List findRegisteredGroupsByUser(Long userId) { + public List findGroups(Long userId, String keyword) { + if (keyword == null) { + return findRegisteredGroups(userId); + } + return findGroupsByKeywords(keyword); + } + + private List findRegisteredGroups(Long userId) { User user = userRepository.findById(userId) .orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND)); // TODO: 쿼리 최적화 @@ -57,4 +64,10 @@ public List findRegisteredGroupsByUser(Long userId) { .map(each -> new GroupResponse(each.getId(), each.getName())) .toList(); } + + private List findGroupsByKeywords(String keyword) { + return groupRepository.findByNameLike(keyword).stream() + .map(each -> new GroupResponse(each.getId(), each.getName())) + .toList(); + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/application/SchoolSearchService.java b/src/main/java/slvtwn/khu/toyouserver/application/SchoolSearchService.java deleted file mode 100644 index 2dc61d5..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/application/SchoolSearchService.java +++ /dev/null @@ -1,23 +0,0 @@ -package slvtwn.khu.toyouserver.application; - -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import slvtwn.khu.toyouserver.dto.SchoolSearchResponse; -import slvtwn.khu.toyouserver.persistance.SchoolRepository; - -@RequiredArgsConstructor -@Service -@Transactional -public class SchoolSearchService { - - private final SchoolRepository schoolRepository; - - @Transactional - public List searchSchoolWithKeyword(String keyword) { - return schoolRepository.findByNameContaining(keyword).stream() - .map(SchoolSearchResponse::from) - .toList(); - } -} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/application/UserProfileFacade.java b/src/main/java/slvtwn/khu/toyouserver/application/UserProfileFacade.java index a7e53b5..4e281e6 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/UserProfileFacade.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/UserProfileFacade.java @@ -3,7 +3,6 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.dto.GroupResponse; import slvtwn.khu.toyouserver.dto.UserProfileResponse; @@ -19,7 +18,7 @@ public class UserProfileFacade { @Transactional(readOnly = true) public UserProfileResponse getProfile(Long userId) { UserResponse userResponse = userService.getProfile(userId); - List groupResponses = groupService.findRegisteredGroupsByUser(userId); + List groupResponses = groupService.findGroups(userId, null); return new UserProfileResponse(userResponse.id(), userResponse.birthday(), userResponse.name(), userResponse.introduction(), userResponse.imageUrl(), groupResponses); diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/Group.java b/src/main/java/slvtwn/khu/toyouserver/domain/Group.java index d964a41..e5e9da0 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/Group.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/Group.java @@ -23,12 +23,18 @@ public class Group extends BaseTimeEntity { private String name; + private String address; + private String region; + private String homepageUrl; + public Group(String name) { - this(null, name); + this(name, null, null, null); } - public Group(Long id, String name) { - this.id = id; + public Group(String name, String address, String region, String homepageUrl) { this.name = name; + this.address = address; + this.region = region; + this.homepageUrl = homepageUrl; } } diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/School.java b/src/main/java/slvtwn/khu/toyouserver/domain/School.java deleted file mode 100644 index 4e36112..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/domain/School.java +++ /dev/null @@ -1,33 +0,0 @@ -package slvtwn.khu.toyouserver.domain; - -import static jakarta.persistence.GenerationType.IDENTITY; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Entity -@Table(name = "schools") -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class School { - @Id - @GeneratedValue(strategy = IDENTITY) - private Long id; - private String name; - private String address; - private String region; - private String homepageUrl; - private String institutionType; - - public School(String name, String address, String region, String homepageUrl) { - this.name = name; - this.address = address; - this.region = region; - this.homepageUrl = homepageUrl; - } -} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/SchoolSearchResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/SchoolSearchResponse.java deleted file mode 100644 index 9024838..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/dto/SchoolSearchResponse.java +++ /dev/null @@ -1,9 +0,0 @@ -package slvtwn.khu.toyouserver.dto; - -import slvtwn.khu.toyouserver.domain.School; - -public record SchoolSearchResponse(Long id, String name, String address, String homepageUrl) { - public static SchoolSearchResponse from(School school) { - return new SchoolSearchResponse(school.getId(), school.getName(), school.getAddress(), school.getHomepageUrl()); - } -} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java index 48015a1..ecb2aec 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java @@ -1,7 +1,12 @@ package slvtwn.khu.toyouserver.persistance; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import slvtwn.khu.toyouserver.domain.Group; public interface GroupRepository extends JpaRepository { + + @Query("select g from Group g where g.name like %:keyword%") + List findByNameLike(String keyword); } diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/SchoolRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/SchoolRepository.java deleted file mode 100644 index 5763896..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/SchoolRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package slvtwn.khu.toyouserver.persistance; - -import java.util.List; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import slvtwn.khu.toyouserver.domain.School; - -public interface SchoolRepository extends JpaRepository { - @Query("select s from School s where s.name like %:keyword%") - List findByNameContaining(String keyword); -} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java index 8089003..66a134f 100644 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java @@ -26,8 +26,9 @@ public ToyouResponse createGroup(@RequestBody GroupCreateRequest } @GetMapping("/groups") - public ToyouResponse> findRegisteredGroups(@UserAuthentication Long userId) { - return ToyouResponse.from(groupService.findRegisteredGroupsByUser(userId)); + public ToyouResponse> findGroups(@UserAuthentication Long userId, + String keywords) { + return ToyouResponse.from(groupService.findGroups(userId, keywords)); } @PostMapping("/groups/{groupId}/register") diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/SchoolController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/SchoolController.java deleted file mode 100644 index d16f7a9..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/SchoolController.java +++ /dev/null @@ -1,22 +0,0 @@ -package slvtwn.khu.toyouserver.presentation; - -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import slvtwn.khu.toyouserver.application.SchoolSearchService; -import slvtwn.khu.toyouserver.common.response.ToyouResponse; -import slvtwn.khu.toyouserver.dto.SchoolSearchResponse; - -@RequiredArgsConstructor -@RestController -public class SchoolController { - - private final SchoolSearchService schoolSearchService; - - @GetMapping("/schools/search") - public ToyouResponse> searchSchools(@RequestParam String keyword) { - return ToyouResponse.from(schoolSearchService.searchSchoolWithKeyword(keyword)); - } -} diff --git a/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java index a2eced5..c3ccd5e 100644 --- a/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java +++ b/src/test/java/slvtwn/khu/toyouserver/application/GroupServiceTest.java @@ -8,6 +8,7 @@ import jakarta.persistence.PersistenceContext; import java.time.LocalDate; import java.util.List; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -16,7 +17,6 @@ import org.springframework.transaction.annotation.Transactional; import slvtwn.khu.toyouserver.domain.Group; import slvtwn.khu.toyouserver.domain.Member; -import slvtwn.khu.toyouserver.domain.SocialAuthProvider; import slvtwn.khu.toyouserver.domain.User; import slvtwn.khu.toyouserver.dto.GroupCreateRequest; import slvtwn.khu.toyouserver.dto.GroupMemberResponse; @@ -115,10 +115,54 @@ class GroupServiceTest { entityManager.persist(member2); // when - List response = groupService.findRegisteredGroupsByUser(user.getId()); + List response = groupService.findGroups(user.getId(), null); // then assertThat(response).extracting(GroupResponse::name) .containsExactlyInAnyOrder(group1.getName(), group2.getName()); } + + @Test + void 이름을_기준으로_그룹을_검색한다() { + // given + Group group = new Group("경희어린이집", "경기도 용인시 기흥구 덕영대로 1732", "경기도", "https://www.khu.ac.kr"); + entityManager.persist(group); + String keyword = "경희"; + + // when + List response = groupService.findGroups(null, keyword); + + // then + assertThat(response).containsExactlyInAnyOrder(new GroupResponse(group.getId(), group.getName())); + } + + @Test + void 검색어가_포함된_그룹이_없다면_빈_리스트를_반환한다() { + // given + String keyword = "섭섭어린이집"; + + // when + List response = groupService.findGroups(null, keyword); + + // then + assertThat(response).isEmpty(); + } + + @Test + void 검색어가_포함된_그룹_여러_개를_반환할_수_있다() { + // given + Group group1 = new Group("경희어린이집1", "경기도 용인시 기흥구 덕영대로 1732", "경기도", "https://www.khu.ac.kr"); + Group group2 = new Group("경희어린이집2", "경기도 용인시 기흥구 덕영대로 1732", "경기도", "https://www.khu.ac.kr"); + entityManager.persist(group1); + entityManager.persist(group2); + String keyword = "경희어린이집"; + + // when + List response = groupService.findGroups(null, keyword); + + // then + assertThat(response).containsExactlyInAnyOrder( + new GroupResponse(group1.getId(), group1.getName()), + new GroupResponse(group2.getId(), group2.getName())); + } } diff --git a/src/test/java/slvtwn/khu/toyouserver/application/SchoolSearchServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/application/SchoolSearchServiceTest.java deleted file mode 100644 index bee3494..0000000 --- a/src/test/java/slvtwn/khu/toyouserver/application/SchoolSearchServiceTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package slvtwn.khu.toyouserver.application; - -import static org.assertj.core.api.Assertions.assertThat; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import java.util.List; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.transaction.annotation.Transactional; -import slvtwn.khu.toyouserver.domain.School; -import slvtwn.khu.toyouserver.dto.SchoolSearchResponse; - -@Transactional -@SpringBootTest -class SchoolSearchServiceTest { - @PersistenceContext - private EntityManager em; - - @Autowired - private SchoolSearchService schoolSearchService; - - private School dummySchool; - private School resultExpectedSchool; - private School unRelatedSchool; - - @BeforeEach - void setUp() { - dummySchool = new School("경희어린이집", "경기도 용인시 기흥구 덕영대로 1732", "경기도", "https://www.khu.ac.kr"); - resultExpectedSchool = new School("경희대학교", "경기도 용인시 기흥구 덕영대로 1732", "경기도", "https://www.khu.ac.kr"); - unRelatedSchool = new School("효섭어린이집", "서울특별시 용산구 이태원동 회나무로", "서울특별시", "https://www.nowhere.com"); - em.persist(dummySchool); - em.persist(resultExpectedSchool); - em.persist(unRelatedSchool); - } - - @DisplayName("이름을 기준으로 학교를 검색한다.") - @Test - void searchSchoolWithKeyword() { - // given - String keyword = "경희대"; - - // when - var result = schoolSearchService.searchSchoolWithKeyword(keyword); - - // then - assertThat(result).containsExactly(SchoolSearchResponse.from(resultExpectedSchool)); - } - - @DisplayName("검색어가 포함된 학교가 없을 때 빈 리스트를 반환한다.") - @Test - void searchSchoolWithKeyword_noResult() { - // given - String keyword = "섭섭어린이집"; - - // when - var result = schoolSearchService.searchSchoolWithKeyword(keyword); - - // then - assertThat(result).isEmpty(); - } - - @DisplayName("검색어가 포함된 학교가 여러 개일 때 모두 반환한다.") - @Test - void searchSchoolWithKeyword_multipleResults() { - // given - String keyword = "어린이집"; - - // when - var result = schoolSearchService.searchSchoolWithKeyword(keyword); - - // then - List expected = List.of( - SchoolSearchResponse.from(dummySchool), - SchoolSearchResponse.from(unRelatedSchool)); - assertThat(result).isEqualTo(expected); - } -} \ No newline at end of file