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

[BE] issue221: 내가 참여한 스터디 조회 오류 #233

Merged
merged 7 commits into from
Aug 10, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.woowacourse.moamoa.member.query.data.MemberData;
import com.woowacourse.moamoa.study.domain.StudyStatus;
import com.woowacourse.moamoa.study.query.data.MyStudySummaryData;
import com.woowacourse.moamoa.study.query.data.StudyOwnerAndTagsData;
import com.woowacourse.moamoa.tag.query.response.TagSummaryData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -32,16 +32,16 @@ public class MyStudyDao {
final String title = rs.getString("title");
final String studyStatus = rs.getString("study_status");
final int currentMemberCount = rs.getInt("current_member_count");
final int maxMemberCount = rs.getInt("max_member_count");
final Integer maxMemberCount = rs.getObject("max_member_count", Integer.class);
final String startDate = rs.getString("start_date");
final String endDate = rs.getString("end_date");

return new MyStudySummaryData(id, title, StudyStatus.valueOf(studyStatus),
currentMemberCount, maxMemberCount, startDate, endDate);
};

private static final ResultSetExtractor<Map<Long, StudyOwnerAndTagsData>> OWNER_WITH_TAG_ROW_MAPPER = rs -> {
Map<Long, StudyOwnerAndTagsData> result = new LinkedHashMap<>();
private static final ResultSetExtractor<Map<Long, MemberData>> OWNER_ROW_MAPPER = rs -> {
Map<Long, MemberData> result = new LinkedHashMap<>();

Long studyId;
while (rs.next()) {
Expand All @@ -53,40 +53,69 @@ public class MyStudyDao {
String imageUrl = rs.getString("image_url");
String profileUrl = rs.getString("profile_url");

result.put(studyId, new StudyOwnerAndTagsData(new MemberData(githubId, username, imageUrl, profileUrl),
new ArrayList<>()));
result.put(studyId, new MemberData(githubId, username, imageUrl, profileUrl));
}
}
return result;
};

private static final ResultSetExtractor<Map<Long, List<TagSummaryData>>> TAG_ROW_MAPPER = rs -> {
Map<Long, List<TagSummaryData>> result = new LinkedHashMap<>();

while (rs.next()) {
Long studyId = rs.getLong("study.id");

if (!result.containsKey(studyId)) {
result.put(studyId, new ArrayList<>());
}

final Long tagId = rs.getLong("tag.id");
final String tagName = rs.getString("tag.name");
result.get(studyId)
.addTag(new TagSummaryData(tagId, tagName));

result.get(studyId).add(new TagSummaryData(tagId, tagName));
}
return result;
};

public List<MyStudySummaryData> findMyStudyByMemberId(Long id) {
String sql = "SELECT DISTINCT study.id, study.title, study.study_status, study.current_member_count, "
+ "study.max_member_count, study.start_date, study.end_date "
+ "FROM study_member JOIN study ON study_member.study_id = study.id "
+ "FROM study LEFT JOIN study_member ON study_member.study_id = study.id "
Comment on lines -71 to +83
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

+ "WHERE study_member.member_id = :id OR study.owner_id = :id";

return jdbcTemplate.query(sql, Map.of("id", id), MY_STUDY_SUMMARY_ROW_MAPPER);
}

public Map<Long, StudyOwnerAndTagsData> findStudyOwnerWithTags(List<Long> studyIds) {
public Map<Long, MemberData> findOwners(List<Long> studyIds) {
if (studyIds.isEmpty()) {
return new HashMap<>();
}
Comment on lines +90 to +92
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


SqlParameterSource parameters = new MapSqlParameterSource("ids", studyIds);

String sql = "SELECT study.id, member.github_id, member.username, member.image_url, member.profile_url "
+ "FROM study JOIN member ON member.id = study.owner_id "
+ "WHERE study.id IN (:ids)";

return jdbcTemplate.query(sql, parameters, OWNER_ROW_MAPPER);
}

public Map<Long, List<TagSummaryData>> findTags(List<Long> studyIds) {
if (studyIds.isEmpty()) {
return new HashMap<>();
}

List<String> ids = studyIds.stream()
.map(Object::toString)
.collect(Collectors.toList());

SqlParameterSource parameters = new MapSqlParameterSource("ids", ids);

String sql = "SELECT study.id, member.github_id, member.username, member.image_url, member.profile_url, tag.id, tag.name "
+ "FROM study JOIN member ON member.id = study.owner_id "
+ "JOIN study_tag ON study.id = study_tag.study_id "
String sql = "SELECT study.id, tag.id, tag.name "
+ "FROM study JOIN study_tag ON study.id = study_tag.study_id "
+ "JOIN tag ON tag.id = study_tag.tag_id "
+ "WHERE study.id IN (:ids)";

return jdbcTemplate.query(sql, parameters, OWNER_WITH_TAG_ROW_MAPPER);
return jdbcTemplate.query(sql, parameters, TAG_ROW_MAPPER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.woowacourse.moamoa.study.domain.StudyStatus;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Getter
public class MyStudySummaryData {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ public class StudyOwnerAndTagsData {

private MemberData owner;
private List<TagSummaryData> tags;

public void addTag(TagSummaryData tagSummaryData) {
this.tags.add(tagSummaryData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.woowacourse.moamoa.member.domain.Member;
import com.woowacourse.moamoa.member.domain.repository.MemberRepository;
import com.woowacourse.moamoa.member.query.data.MemberData;
import com.woowacourse.moamoa.study.domain.Study;
import com.woowacourse.moamoa.study.domain.repository.StudyRepository;
import com.woowacourse.moamoa.study.query.MyStudyDao;
Expand All @@ -13,6 +14,8 @@
import com.woowacourse.moamoa.study.query.data.MyStudySummaryData;
import com.woowacourse.moamoa.study.service.response.MyStudiesResponse;

import com.woowacourse.moamoa.tag.query.response.TagSummaryData;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -43,9 +46,30 @@ public MyStudiesResponse getStudies(final Long githubId) {
.map(MyStudySummaryData::getId)
.collect(Collectors.toList());

final Map<Long, StudyOwnerAndTagsData> studyOwnerWithTags = myStudyDao.findStudyOwnerWithTags(studyIds);
Map<Long, StudyOwnerAndTagsData> ownerWithTags = getOwnerWithTags(studyIds);

return new MyStudiesResponse(mapToResponse(myStudySummaryData, studyOwnerWithTags));
return new MyStudiesResponse(mapToResponse(myStudySummaryData, ownerWithTags));
}

private Map<Long, StudyOwnerAndTagsData> getOwnerWithTags(final List<Long> studyIds) {
final Map<Long, MemberData> owners = myStudyDao.findOwners(studyIds);
final Map<Long, List<TagSummaryData>> tags = myStudyDao.findTags(studyIds);

return mapOwnerWithTags(studyIds, owners, tags);
}

private Map<Long, StudyOwnerAndTagsData> mapOwnerWithTags(final List<Long> studyIds,
final Map<Long, MemberData> owners,
final Map<Long, List<TagSummaryData>> tags) {
Map<Long, StudyOwnerAndTagsData> result = new HashMap<>();
for (Long id : studyIds) {
if (tags.get(id) == null) {
result.put(id, new StudyOwnerAndTagsData(owners.get(id), List.of()));
continue;
}
result.put(id, new StudyOwnerAndTagsData(owners.get(id), tags.get(id)));
}
return result;
}

private List<MyStudyResponse> mapToResponse(final List<MyStudySummaryData> myStudySummaryData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class MyStudiesResponse {

private final List<MyStudyResponse> studies;
private List<MyStudyResponse> studies;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import com.woowacourse.moamoa.tag.query.response.TagSummaryData;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class MyStudyResponse {

private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class TagSummaryData {

private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import static com.woowacourse.acceptance.fixture.StudyFixtures.리액트_스터디_제목;
import static com.woowacourse.acceptance.fixture.StudyFixtures.자바_스터디_제목;
import static com.woowacourse.acceptance.fixture.TagFixtures.BE_태그_ID;
import static com.woowacourse.acceptance.fixture.TagFixtures.BE_태그_설명;
import static com.woowacourse.acceptance.fixture.TagFixtures.BE_태그명;
import static com.woowacourse.acceptance.fixture.TagFixtures.리액트_태그_ID;
import static com.woowacourse.acceptance.fixture.TagFixtures.리액트_태그_설명;
import static com.woowacourse.acceptance.fixture.TagFixtures.자바_태그_ID;
import static com.woowacourse.acceptance.fixture.TagFixtures.자바_태그_설명;
import static com.woowacourse.acceptance.fixture.TagFixtures.자바_태그명;
import static com.woowacourse.acceptance.steps.LoginSteps.그린론이;
import static com.woowacourse.acceptance.steps.LoginSteps.디우가;
import static com.woowacourse.moamoa.study.domain.StudyStatus.IN_PROGRESS;
Expand Down Expand Up @@ -49,7 +52,6 @@

public class GettingMyStudiesAcceptanceTest extends AcceptanceTest {

@Disabled // 그린론이 해결할 버그 관련 인수 테스트
@DisplayName("내가 참여한 스터디를 조회한다.")
@Test
void getMyStudies() {
Expand All @@ -75,10 +77,10 @@ void getMyStudies() {
new MyStudySummaryData(자바_스터디_ID, 자바_스터디_제목, IN_PROGRESS, 1,
null, 지금.toString(), null),
new MemberData(그린론_깃허브_ID, 그린론_이름, 그린론_이미지_URL, 그린론_프로필_URL),
List.of(new TagSummaryData(자바_태그_ID, 자바_태그_설명), new TagSummaryData(리액트_태그_ID, 리액트_태그_설명)));
List.of(new TagSummaryData(자바_태그_ID, 자바_태그명), new TagSummaryData(BE_태그_ID, BE_태그명)));

MyStudyResponse expectedReact = new MyStudyResponse(
new MyStudySummaryData(리액트_스터디_ID, 리액트_스터디_제목, StudyStatus.PREPARE, 1,
new MyStudySummaryData(리액트_스터디_ID, 리액트_스터디_제목, StudyStatus.PREPARE, 2,
null, 지금.plusDays(10).toString(), null),
new MemberData(디우_깃허브_ID, 디우_이름, 디우_이미지_URL, 디우_프로필_URL),
List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ void setUp() {
final LocalDateTime now = LocalDateTime.now();

jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, current_member_count, max_member_count, created_at, start_date, owner_id) "
+ "VALUES (1, 'Java 스터디', '자바 설명', 'java thumbnail', 'RECRUITMENT_START', 'PREPARE', '그린론의 우당탕탕 자바 스터디입니다.', 3, 10, '" + now + "', '2021-12-08', 2)");
+ "VALUES (1, 'Java 스터디', '자바 설명', 'java thumbnail', 'RECRUITMENT_START', 'PREPARE', '그린론의 우당탕탕 자바 스터디입니다.', 3, 10, '"
+ now + "', '2021-12-08', 2)");
jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, current_member_count, max_member_count, created_at, enrollment_end_date, start_date, end_date, owner_id) "
+ "VALUES (2, 'React 스터디', '리액트 설명', 'react thumbnail', 'RECRUITMENT_START', 'PREPARE', '디우의 뤼액트 스터디입니다.', 4, 5, '" + now + "', '2021-11-09', '2021-11-10', '2021-12-08', 3)");
+ "VALUES (2, 'React 스터디', '리액트 설명', 'react thumbnail', 'RECRUITMENT_START', 'PREPARE', '디우의 뤼액트 스터디입니다.', 4, 5, '"
+ now + "', '2021-11-09', '2021-11-10', '2021-12-08', 3)");
jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, current_member_count, max_member_count, created_at, start_date, owner_id) "
+ "VALUES (3, 'javaScript 스터디', '자바스크립트 설명', 'javascript thumbnail', 'RECRUITMENT_START', 'PREPARE', '그린론의 자바스크립트 접해보기', 3, 20, '" + now + "', '2022-08-03', 2)");
+ "VALUES (3, 'javaScript 스터디', '자바스크립트 설명', 'javascript thumbnail', 'RECRUITMENT_START', 'PREPARE', '그린론의 자바스크립트 접해보기', 3, 20, '"
+ now + "', '2022-08-03', 2)");
jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, max_member_count, created_at, start_date, owner_id) "
+ "VALUES (4, 'HTTP 스터디', 'HTTP 설명', 'http thumbnail', 'RECRUITMENT_END', 'PREPARE', '디우의 HTTP 정복하기', 5, '" + now + "', '2022-08-03', 3)");
+ "VALUES (4, 'HTTP 스터디', 'HTTP 설명', 'http thumbnail', 'RECRUITMENT_END', 'PREPARE', '디우의 HTTP 정복하기', 5, '"
+ now + "', '2022-08-03', 3)");
jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, current_member_count, created_at, owner_id, start_date) "
+ "VALUES (5, '알고리즘 스터디', '알고리즘 설명', 'algorithm thumbnail', 'RECRUITMENT_END', 'PREPARE', '알고리즘을 TDD로 풀자의 베루스입니다.', 1, '" + now + "', 4, '2021-12-06')");
+ "VALUES (5, '알고리즘 스터디', '알고리즘 설명', 'algorithm thumbnail', 'RECRUITMENT_END', 'PREPARE', '알고리즘을 TDD로 풀자의 베루스입니다.', 1, '"
+ now + "', 4, '2021-12-06')");
jdbcTemplate.update("INSERT INTO study(id, title, excerpt, thumbnail, recruitment_status, study_status, description, current_member_count, created_at, owner_id, start_date, enrollment_end_date, end_date) "
+ "VALUES (6, 'Linux 스터디', '리눅스 설명', 'linux thumbnail', 'RECRUITMENT_END', 'PREPARE', 'Linux를 공부하자의 베루스입니다.', 1, '" + now + "', 4, '2021-12-06', '2021-12-07', '2022-01-07')");
+ "VALUES (6, 'Linux 스터디', '리눅스 설명', 'linux thumbnail', 'RECRUITMENT_END', 'PREPARE', 'Linux를 공부하자의 베루스입니다.', 1, '"
+ now + "', 4, '2021-12-06', '2021-12-07', '2022-01-07')");

jdbcTemplate.update("INSERT INTO study_tag(study_id, tag_id) VALUES (1, 1)");
jdbcTemplate.update("INSERT INTO study_tag(study_id, tag_id) VALUES (1, 2)");
Expand Down Expand Up @@ -100,12 +106,14 @@ void getMyStudies() {
assertThat(myStudies.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(myStudies.getBody()).isNotNull();
assertThat(myStudies.getBody().getStudies())
.hasSize(3)
.hasSize(5)
.extracting("id", "title", "studyStatus", "currentMemberCount", "maxMemberCount")
.containsExactlyElementsOf(List.of(
tuple(1L, "Java 스터디", PREPARE, 3, 10),
tuple(2L, "React 스터디", PREPARE, 4, 5),
tuple(3L, "javaScript 스터디", PREPARE, 3, 20))
tuple(3L, "javaScript 스터디", PREPARE, 3, 20),
tuple(5L, "알고리즘 스터디", PREPARE, 1, null),
tuple(6L, "Linux 스터디", PREPARE, 1, null))
);

final List<MemberData> owners = myStudies.getBody()
Expand All @@ -115,13 +123,15 @@ void getMyStudies() {
.collect(Collectors.toList());

assertThat(owners)
.hasSize(3)
.hasSize(5)
.extracting("githubId", "username", "imageUrl", "profileUrl")
.containsExactlyElementsOf(List.of(
tuple(2L, "greenlawn", "https://image", "github.com"),
tuple(3L, "dwoo", "https://image", "github.com"),
tuple(2L, "greenlawn", "https://image", "github.com"))
);
tuple(2L, "greenlawn", "https://image", "github.com"),
tuple(4L, "verus", "https://image", "github.com"),
tuple(4L, "verus", "https://image", "github.com")
));

final List<List<TagSummaryData>> tags = myStudies.getBody()
.getStudies()
Expand Down Expand Up @@ -150,5 +160,9 @@ void getMyStudies() {
.extracting("id", "name")
.contains(tuple(2L, "4기"),
tuple(4L, "FE"));

assertThat(tags.get(3).size()).isZero();

assertThat(tags.get(4).size()).isZero();
}
}
Loading