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] issue245: 스터디 가입날짜 및 스터디 개수 #246

Merged
merged 13 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
@@ -1,10 +1,10 @@
package com.woowacourse.moamoa.member.query;

import com.woowacourse.moamoa.member.query.data.MemberData;

import com.woowacourse.moamoa.member.query.data.MemberFullData;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import java.util.Optional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -14,36 +14,72 @@
@Repository
public class MemberDao {

private static final RowMapper<MemberData> ROW_MAPPER = (rs, rn) -> {
Long githubId = rs.getLong("github_id");
String username = rs.getString("username");
String imageUrl = rs.getString("image_url");
String profileUrl = rs.getString("profile_url");
return new MemberData(githubId, username, imageUrl, profileUrl);
};
private static final RowMapper<MemberFullData> MEMBER_FULL_DATA_ROW_MAPPER = createMemberFullDataRowMapper();

private static final RowMapper<MemberData> MEMBER_DATA_ROW_MAPPER = createMemberDataRowMapper();

private final NamedParameterJdbcTemplate jdbcTemplate;

public MemberDao(final NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<MemberData> findMembersByStudyId(final Long studyId) {
final String sql = "SELECT github_id, username, image_url, profile_url "
public List<MemberFullData> findMembersByStudyId(final Long studyId) {
final String sql = "SELECT member.id, github_id, username, image_url, profile_url, "
+ "study_member.participation_date as participation_date, "
+ countStudyNumber()
+ "FROM member JOIN study_member ON member.id = study_member.member_id "
+ "JOIN study ON study_member.study_id = study.id "
+ "WHERE study_member.study_id = :id";
return jdbcTemplate.query(sql, Map.of("id", studyId), ROW_MAPPER);

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

private String countStudyNumber() {
return countParticipationStudy() + " + " + countOwnerStudy();
}

private String countParticipationStudy() {
return "((SELECT count(case when (study_member.member_id = member.id) then 1 end) "
+ "FROM study JOIN study_member ON study.id = study_member.study_id) ";
}

private String countOwnerStudy() {
return "(SELECT count(case when (study.owner_id = member.id) then 1 end) "
+ "FROM study)) as number_of_study ";
}

public Optional<MemberData> findByGithubId(final Long githubId) {
try {
final String sql = "SELECT github_id, username, image_url, profile_url "
+ "FROM member "
+ "WHERE member.github_id = :id";
final MemberData data = jdbcTemplate.queryForObject(sql, Map.of("id", githubId), ROW_MAPPER);
final MemberData data = jdbcTemplate.queryForObject(sql, Map.of("id", githubId), MEMBER_DATA_ROW_MAPPER);
return Optional.of(data);
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}

private static RowMapper<MemberFullData> createMemberFullDataRowMapper() {
return (resultSet, resultNumber) -> {
Long githubId = resultSet.getLong("github_id");
String username = resultSet.getString("username");
String imageUrl = resultSet.getString("image_url");
String profileUrl = resultSet.getString("profile_url");
LocalDate participationDate = resultSet.getDate("participation_date").toLocalDate();
int numberOfStudy = resultSet.getInt("number_of_study");
return new MemberFullData(githubId, username, imageUrl, profileUrl, participationDate, numberOfStudy);
};
}

private static RowMapper<MemberData> createMemberDataRowMapper() {
return (resultSet, resultNumber) -> {
Long githubId = resultSet.getLong("github_id");
String username = resultSet.getString("username");
String imageUrl = resultSet.getString("image_url");
String profileUrl = resultSet.getString("profile_url");
return new MemberData(githubId, username, imageUrl, profileUrl);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.woowacourse.moamoa.member.query.data;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class MemberFullData {
Copy link
Collaborator

Choose a reason for hiding this comment

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

MemberFullData보다는 다른 이름이 좋아 보입니다. 예를 들어 ParticipantData라든가


@JsonProperty("id")
private Long githubId;

private String username;

private String imageUrl;

private String profileUrl;

private LocalDate participationDate;

private int numberOfStudy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static lombok.AccessLevel.PROTECTED;

import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.AllArgsConstructor;
Expand All @@ -20,4 +21,12 @@ public class Participant {

@Column(name = "member_id", nullable = false)
private Long memberId;

@Column(updatable = false, nullable = false)
private LocalDate participationDate;
Comment on lines +25 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

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

참여일자 추가 굿


public Participant(final Long memberId) {
this.memberId = memberId;
this.participationDate = LocalDate.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = PROTECTED)
@Getter
@NoArgsConstructor(access = PROTECTED)
Comment on lines -19 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

순서를 바꾸셨는데 이건 정말 궁금해서 여쭤보는거에요! 혹시 바꾼 이유가 있나요? 중요한 순서라기엔 오름차순이나 내림차순 둘다 Getter가 처음이나 마지막이어야 할 것 같은데 궁금합니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

어.. 이건 아마 구현하는 중에 변경된 것 같습니다..!!

public class Study {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.woowacourse.moamoa.study.service;

import com.woowacourse.moamoa.member.query.MemberDao;
import com.woowacourse.moamoa.member.query.data.MemberData;
import com.woowacourse.moamoa.member.query.data.MemberFullData;
import com.woowacourse.moamoa.study.query.SearchingTags;
import com.woowacourse.moamoa.study.query.StudyDetailsDao;
import com.woowacourse.moamoa.study.query.StudySummaryDao;
Expand Down Expand Up @@ -56,7 +56,8 @@ public StudiesResponse getStudies(final String title, final SearchingTags search
public StudyDetailResponse getStudyDetails(final Long studyId) {
final StudyDetailsData content = studyDetailsDao.findBy(studyId)
.orElseThrow(StudyNotFoundException::new);
final List<MemberData> participants = memberDao.findMembersByStudyId(studyId);
final List<MemberFullData> participants = memberDao.findMembersByStudyId(studyId);

final List<TagData> attachedTags = tagDao.findTagsByStudyId(studyId);
return new StudyDetailResponse(content, participants, attachedTags);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.woowacourse.moamoa.study.service.response;

import com.woowacourse.moamoa.member.query.data.MemberData;
import com.woowacourse.moamoa.member.query.data.MemberFullData;
import com.woowacourse.moamoa.study.query.data.StudyDetailsData;
import com.woowacourse.moamoa.tag.query.response.TagData;
import java.time.LocalDate;
Expand Down Expand Up @@ -29,11 +30,11 @@ public class StudyDetailResponse {
private LocalDate startDate;
private LocalDate endDate;
private MemberData owner;
private List<MemberData> members;
private List<MemberFullData> members;
private List<TagData> tags;

public StudyDetailResponse(final StudyDetailsData study,
final List<MemberData> participants,
final List<MemberFullData> participants,
Copy link
Collaborator

Choose a reason for hiding this comment

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

클래스명을 MemberData에서 MemberFullData로 변경한 이유가 궁금해요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

스크린샷 2022-08-17 08 27 10

기존의 MemberData 를 제거하면 변경점이 많아져서 (118 곳에서 사용중) 디테일 조회에서만 사용하는 MemberFullData 를 추가하는 방향으로 작업하였습니다..!!

final List<TagData> attachedTags) {
this.id = study.getId();
this.title = study.getTitle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public void getStudyDetails() {
.body("members.username", contains(짱구_이름, 그린론_이름, 베루스_이름))
.body("members.imageUrl", contains(짱구_이미지_URL, 그린론_이미지_URL, 베루스_이미지_URL))
.body("members.profileUrl", contains(짱구_프로필_URL, 그린론_프로필_URL, 베루스_프로필_URL))
.body("members.participationDate", not(empty()))
.body("members.numberOfStudy", contains(1, 1, 1))
.body("tags.id", not(empty()))
.body("tags.name", contains("4기", "FE", "React"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void searchBySameAndDifferentKindFilters() {

@DisplayName("스터디 상세 정보를 조회할 수 있다.")
@Test
public void mentgetStudyDetails() {
public void getStudyDetails() {
StudyDetailsData expect = StudyDetailsData.builder()
// Study Content
.id(javaStudyId).title("Java 스터디").excerpt("자바 설명").thumbnail("java thumbnail")
Expand Down Expand Up @@ -309,7 +309,6 @@ public void getStudyDetailsWithOptional() {
@DisplayName("스터디 참여자와 부착된 태그가 없는 스터디의 세부사항 조회")
@Test
void getNotHasParticipantsAndAttachedTagsStudyDetails() {

final StudyDetailsData expect = StudyDetailsData.builder()
// Study Content
.id(linuxStudyId).title("Linux 스터디").excerpt("리눅스 설명").thumbnail("linux thumbnail")
Expand Down Expand Up @@ -337,6 +336,24 @@ void getNotHasParticipantsAndAttachedTagsStudyDetails() {
assertAttachedTags(actual.getTags(), expectAttachedTags);
}

@DisplayName("스터디 디테일 정보 조회 시 스터디원들이 가입한 스터디의 수와 가입날짜도 함께 조회한다.")
@Test
public void findStudyDetailsWithNumberOfStudy() {
final ResponseEntity<StudyDetailResponse> response = sut.getStudyDetails(javaStudyId);

final StudyDetailResponse responseBody = response.getBody();

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseBody.getMembers())
.filteredOn(member -> member.getParticipationDate() != null)
.hasSize(2)
.extracting("githubId", "username", "imageUrl", "profileUrl", "numberOfStudy")
.containsExactlyInAnyOrder(
tuple(dwoo.getGithubId(), dwoo.getUsername(), dwoo.getImageUrl(), dwoo.getProfileUrl(), 3),
tuple(verus.getGithubId(), verus.getUsername(), verus.getImageUrl(), verus.getProfileUrl(), 4)
);
Comment on lines +351 to +354
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 부분은 순서가 중요한 부분은 아니라서 containsExactly가 아닌 InAnyOrder를 하신거군요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵..!! 순서까지 고려하려면 너무 initDataBase 에서 순서까지 고려해줘야하고 다른 테스트들에도 영향이 있어서요..!!

}

private void assertStudyContent(final StudyDetailResponse actual, final StudyDetailsData expect) {
assertThat(actual.getId()).isEqualTo(expect.getId());
assertThat(actual.getTitle()).isEqualTo(expect.getTitle());
Expand Down
1 change: 1 addition & 0 deletions backend/src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ CREATE TABLE study_member
id BIGINT PRIMARY KEY AUTO_INCREMENT,
study_id BIGINT,
member_id BIGINT,
participation_date DATE not null,
Copy link
Collaborator

Choose a reason for hiding this comment

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

굿굿

FOREIGN KEY (study_id) REFERENCES study (id),
FOREIGN KEY (member_id) REFERENCES member (id)
);
Expand Down