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

커넥션풀 관련 설정 추가 + 회원탈퇴시 fk 관련 삭제 로직 추가 #784

Merged
merged 7 commits into from
Oct 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

public interface ReportActionAlarmRepository extends JpaRepository<ReportActionAlarm, Long> {

List<ReportActionAlarm> findByMember(final Member member, final Pageable pageable);
List<ReportActionAlarm> findByMemberOrderByIdDesc(final Member member, final Pageable pageable);

Optional<ReportActionAlarm> findByIdAndMember(final Long Id, final Member member);

List<ReportActionAlarm> findAllByMember(final Member member);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -55,13 +54,9 @@ private String makeNicknameBy(final Alarm alarm) {
}

public List<ReportActionAlarmResponse> getReportActionAlarms(final Member member, final int page) {
final PageRequest pageRequest = PageRequest.of(
page,
BASIC_PAGE_SIZE,
Sort.by(Sort.Direction.DESC, "createdAt")
);
final PageRequest pageRequest = PageRequest.of(page, BASIC_PAGE_SIZE);
final List<ReportActionAlarm> reportActionAlarms = reportActionAlarmRepository
.findByMember(member, pageRequest);
.findByMemberOrderByIdDesc(member, pageRequest);

return reportActionAlarms.stream()
.map(ReportActionAlarmResponse::from)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.votogether.domain.member.service;

import com.votogether.domain.alarm.entity.Alarm;
import com.votogether.domain.alarm.entity.ReportActionAlarm;
import com.votogether.domain.alarm.repository.AlarmRepository;
import com.votogether.domain.alarm.repository.ReportActionAlarmRepository;
import com.votogether.domain.member.dto.request.MemberDetailRequest;
import com.votogether.domain.member.dto.response.MemberInfoResponse;
import com.votogether.domain.member.entity.Member;
Expand All @@ -12,6 +14,8 @@
import com.votogether.domain.member.repository.MemberCategoryRepository;
import com.votogether.domain.member.repository.MemberMetricRepository;
import com.votogether.domain.member.repository.MemberRepository;
import com.votogether.domain.notice.entity.Notice;
import com.votogether.domain.notice.repository.NoticeRepository;
import com.votogether.domain.post.entity.Post;
import com.votogether.domain.post.entity.comment.Comment;
import com.votogether.domain.post.repository.CommentRepository;
Expand Down Expand Up @@ -43,6 +47,8 @@ public class MemberService {
private final ReportRepository reportRepository;
private final CommentRepository commentRepository;
private final AlarmRepository alarmRepository;
private final ReportActionAlarmRepository reportActionAlarmRepository;
private final NoticeRepository noticeRepository;

@Transactional
public Member register(final Member member) {
Expand Down Expand Up @@ -120,6 +126,8 @@ public void deleteMember(final Member member) {
deleteMemberCategories(member);
deleteReports(member, posts, comments);
deleteAlarms(member);
deleteReportActionAlarms(member);
deleteNotices(member);

memberMetricRepository.deleteByMember(member);
memberRepository.delete(member);
Expand Down Expand Up @@ -216,4 +224,20 @@ private void deleteAlarms(final Member member) {
alarmRepository.deleteAllById(alarmIds);
}

private void deleteReportActionAlarms(final Member member) {
final List<Long> reportActionAlarmIds = reportActionAlarmRepository.findAllByMember(member)
.stream()
.map(ReportActionAlarm::getId)
.toList();
reportActionAlarmRepository.deleteAllById(reportActionAlarmIds);
}

private void deleteNotices(final Member member) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

공지사항까지 챙겨주셔서 감사합니다 👍🏻

final List<Long> noticeIds = noticeRepository.findAllByMember(member)
.stream()
.map(Notice::getId)
.toList();
noticeRepository.deleteAllById(noticeIds);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.votogether.domain.notice.repository;

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.notice.entity.Notice;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -13,4 +14,6 @@ public interface NoticeRepository extends JpaRepository<Notice, Long> {

List<Notice> findAllByOrderByIdDesc(final Pageable pageable);

List<Notice> findAllByMember(final Member member);

}
4 changes: 4 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ spring:
url: ${DATASOURCE_URL}
username: ${DATASOURCE_USERNAME}
password: ${DATASOURCE_PASSWORD}
hikari:
maximumPoolSize: ${MAXIMUM_POOL_SIZE}
connectionTimeout: ${CONNECTION_TIMEOUT}
maxLifetime: ${MAX_LIFETIME}
Comment on lines +7 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

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

이외의 설정도 많았던 것 같은데 3개의 설정만 해주시는 이유가 있나요?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

그 이외의 설정값들은 default값이여서 따로 설정은 하지 않았습니다!


jpa:
database-platform: org.hibernate.dialect.MySQLDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

Expand Down Expand Up @@ -90,12 +89,14 @@ void getReportAlarmActions() {

// when
List<ReportActionAlarmResponse> results = RestAssuredMockMvc
.given().log().all()
.queryParam("page", 0)
.when().get("/alarms/report?page=0")
.then().log().all()
.status(HttpStatus.OK)
.extract()
.as(new ParameterizedTypeReference<List<ReportActionAlarmResponse>>() {
}.getType());
.as(new TypeRef<>() {
});
Comment on lines -97 to +99
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 👍


// then
assertThat(results.get(0)).isEqualTo(response);
Expand All @@ -117,6 +118,7 @@ void getReportAlarmAction() {

// when
ReportActionResponse result = RestAssuredMockMvc
.given().log().all()
.when().get("/alarms/report/{id}", 1)
.then().log().all()
.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.votogether.domain.alarm.repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.votogether.domain.alarm.entity.ReportActionAlarm;
import com.votogether.domain.member.entity.Member;
Expand All @@ -12,7 +11,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

class ReportActionAlarmRepositoryTest extends RepositoryTest {

Expand Down Expand Up @@ -54,19 +52,20 @@ void getInLatestOrder() {
reportActionAlarmRepository.save(reportActionAlarmC);

// when
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
List<ReportActionAlarm> reportActionAlarms = reportActionAlarmRepository.findByMember(member, pageRequest);
PageRequest pageRequest = PageRequest.of(0, 10);
List<ReportActionAlarm> reportActionAlarms = reportActionAlarmRepository
.findByMemberOrderByIdDesc(member, pageRequest);

// then
assertAll(
() -> assertThat(reportActionAlarms).hasSize(3),
() -> assertThat(reportActionAlarms.get(0).getReportType()).isEqualTo(
reportActionAlarmC.getReportType()),
() -> assertThat(reportActionAlarms.get(1).getReportType()).isEqualTo(
reportActionAlarmB.getReportType()),
() -> assertThat(reportActionAlarms.get(2).getReportType()).isEqualTo(
reportActionAlarmA.getReportType())
);
assertSoftly(softly -> {
softly.assertThat(reportActionAlarms).hasSize(3);
softly.assertThat(reportActionAlarms.get(0).getReportType()).isEqualTo(
reportActionAlarmC.getReportType());
softly.assertThat(reportActionAlarms.get(1).getReportType()).isEqualTo(
reportActionAlarmB.getReportType());
softly.assertThat(reportActionAlarms.get(2).getReportType()).isEqualTo(
reportActionAlarmA.getReportType());
});
}

@Test
Expand All @@ -87,16 +86,19 @@ void getWithTen() {
}

// when
PageRequest pageRequestA = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
List<ReportActionAlarm> reportActionAlarmsA = reportActionAlarmRepository.findByMember(member, pageRequestA);
PageRequest pageRequestA = PageRequest.of(0, 10);
List<ReportActionAlarm> reportActionAlarmsA = reportActionAlarmRepository
.findByMemberOrderByIdDesc(member, pageRequestA);

PageRequest pageRequestB = PageRequest.of(1, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
List<ReportActionAlarm> reportActionAlarmsB = reportActionAlarmRepository.findByMember(member, pageRequestB);
PageRequest pageRequestB = PageRequest.of(1, 10);
List<ReportActionAlarm> reportActionAlarmsB = reportActionAlarmRepository
.findByMemberOrderByIdDesc(member, pageRequestB);

// then
assertAll(
() -> assertThat(reportActionAlarmsA).hasSize(10),
() -> assertThat(reportActionAlarmsB).hasSize(1)
assertSoftly(softly -> {
softly.assertThat(reportActionAlarmsA).hasSize(10);
softly.assertThat(reportActionAlarmsB).hasSize(1);
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.votogether.domain.alarm.entity.ReportActionAlarm;
import com.votogether.domain.alarm.repository.ReportActionAlarmRepository;
import com.votogether.domain.category.entity.Category;
import com.votogether.domain.category.repository.CategoryRepository;
import com.votogether.domain.member.dto.request.MemberDetailRequest;
Expand All @@ -16,6 +18,8 @@
import com.votogether.domain.member.repository.MemberCategoryRepository;
import com.votogether.domain.member.repository.MemberMetricRepository;
import com.votogether.domain.member.repository.MemberRepository;
import com.votogether.domain.notice.entity.Notice;
import com.votogether.domain.notice.repository.NoticeRepository;
import com.votogether.domain.post.entity.Post;
import com.votogether.domain.post.entity.comment.Comment;
import com.votogether.domain.post.repository.CommentRepository;
Expand Down Expand Up @@ -67,6 +71,12 @@ class MemberServiceTest extends ServiceTest {
@Autowired
PostTestPersister postTestPersister;

@Autowired
ReportActionAlarmRepository reportActionAlarmRepository;

@Autowired
NoticeRepository noticeRepository;

@Autowired
EntityManager em;

Expand Down Expand Up @@ -481,6 +491,55 @@ void deleteWithReportedNickname() {
);
}

@Test
@DisplayName("회원과 신고조치알림 모두 삭제된다.")
void deleteWithReportActionAlarms() {
// given
Member member = memberRepository.save(MemberFixtures.MALE_20.get());

ReportActionAlarm reportActionAlarm = ReportActionAlarm.builder()
.reportType(ReportType.POST)
.member(member)
.isChecked(false)
.target("1")
.reasons("광고성, 부적합성")
.build();
reportActionAlarmRepository.save(reportActionAlarm);

// when
memberService.deleteMember(member);

// then
assertAll(
() -> assertThat(memberRepository.findAll()).isEmpty(),
() -> assertThat(reportActionAlarmRepository.findAll()).isEmpty()
);
Comment on lines +513 to +516
Copy link
Collaborator

Choose a reason for hiding this comment

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

assertAll 대신 assertSoftly 써보는 것은 어떨까요.
개인적으로 그게 더 친절하게 안내해주더라구요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

네 수정하도록 할께요~

}

@Test
@DisplayName("회원과 작성한 공지사항 모두 삭제된다.")
void deleteWithNotices() {
// given
Member member = memberRepository.save(MemberFixtures.MALE_20.get());

Notice notice = Notice.builder()
.member(member)
.title("title")
.content("content")
.deadline(LocalDateTime.now().plusDays(1))
.build();
noticeRepository.save(notice);

// when
memberService.deleteMember(member);

// then
assertAll(
() -> assertThat(memberRepository.findAll()).isEmpty(),
() -> assertThat(noticeRepository.findAll()).isEmpty()
);
}

}

}
4 changes: 4 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ server:
max-connections: 8192
threads:
max: 200
hikari:
maximumPoolSize: 10
connectionTimeout: 30000
maxLifetime: 180000

springdoc:
swagger-ui:
Expand Down