Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 학교 정보 및 그룹 정보 병합, 테스트 수정 #81

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ public List<GroupMemberResponse> findMembers(long groupId) {
.toList();
}

public List<GroupResponse> findRegisteredGroupsByUser(Long userId) {
public List<GroupResponse> findGroups(Long userId, String keyword) {
if (keyword == null) {
return findRegisteredGroups(userId);
}
return findGroupsByKeywords(keyword);
}

private List<GroupResponse> findRegisteredGroups(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND));
// TODO: 쿼리 최적화
Expand All @@ -57,4 +64,10 @@ public List<GroupResponse> findRegisteredGroupsByUser(Long userId) {
.map(each -> new GroupResponse(each.getId(), each.getName()))
.toList();
}

private List<GroupResponse> findGroupsByKeywords(String keyword) {
return groupRepository.findByNameLike(keyword).stream()
.map(each -> new GroupResponse(each.getId(), each.getName()))
.toList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,7 @@ public class UserProfileFacade {
@Transactional(readOnly = true)
public UserProfileResponse getProfile(Long userId) {
UserResponse userResponse = userService.getProfile(userId);
List<GroupResponse> groupResponses = groupService.findRegisteredGroupsByUser(userId);
List<GroupResponse> groupResponses = groupService.findGroups(userId, null);

return new UserProfileResponse(userResponse.id(), userResponse.birthday(), userResponse.name(),
userResponse.introduction(), userResponse.imageUrl(), groupResponses);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/slvtwn/khu/toyouserver/domain/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
33 changes: 0 additions & 33 deletions src/main/java/slvtwn/khu/toyouserver/domain/School.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Group, Long> {

@Query("select g from Group g where g.name like %:keyword%")
List<Group> findByNameLike(String keyword);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public ToyouResponse<GroupResponse> createGroup(@RequestBody GroupCreateRequest
}

@GetMapping("/groups")
public ToyouResponse<List<GroupResponse>> findRegisteredGroups(@UserAuthentication Long userId) {
return ToyouResponse.from(groupService.findRegisteredGroupsByUser(userId));
public ToyouResponse<List<GroupResponse>> findGroups(@UserAuthentication Long userId,
String keywords) {
return ToyouResponse.from(groupService.findGroups(userId, keywords));
}

@PostMapping("/groups/{groupId}/register")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -115,10 +115,54 @@ class GroupServiceTest {
entityManager.persist(member2);

// when
List<GroupResponse> response = groupService.findRegisteredGroupsByUser(user.getId());
List<GroupResponse> 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<GroupResponse> response = groupService.findGroups(null, keyword);

// then
assertThat(response).containsExactlyInAnyOrder(new GroupResponse(group.getId(), group.getName()));
}

@Test
void 검색어가_포함된_그룹이_없다면_빈_리스트를_반환한다() {
// given
String keyword = "섭섭어린이집";

// when
List<GroupResponse> 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<GroupResponse> response = groupService.findGroups(null, keyword);

// then
assertThat(response).containsExactlyInAnyOrder(
new GroupResponse(group1.getId(), group1.getName()),
new GroupResponse(group2.getId(), group2.getName()));
}
}

This file was deleted.