Skip to content

Commit

Permalink
feat: 회원 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youKeon committed Nov 28, 2023
1 parent e04df0c commit 68e0bd2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,9 @@ protected void initializeRecommendation() {
Long initializedCount = memberRepository.initializeRecommendation();
log.info("초기화된 사용자 계정 개수 : {}", initializedCount);
}

public void delete(Member member) {
memberRepository.delete(member);
}
}

10 changes: 10 additions & 0 deletions backend/src/main/java/com/psq/backend/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.psq.backend.member.domain;

import com.psq.backend.bookmark.domain.Bookmark;
import com.psq.backend.common.BaseEntity;
import com.psq.backend.member.exception.InvalidEmailFormatException;
import com.psq.backend.problem.domain.Problem;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor
Expand All @@ -27,6 +31,12 @@ public class Member extends BaseEntity {
@Column(nullable = false)
private String salt;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "member")
private List<Problem> problems = new ArrayList<>();

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "member")
private List<Bookmark> bookmarks = new ArrayList<>();

public Member(String email,
String password,
String salt) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.psq.backend.member.presentation;

import com.psq.backend.member.application.MemberService;
import com.psq.backend.member.domain.Member;
import com.psq.backend.member.dto.request.MemberSignInRequest;
import com.psq.backend.member.dto.request.MemberSignUpRequest;
import com.psq.backend.util.annotation.CurrentUser;
import com.psq.backend.util.annotation.RecommendationCheck;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
Expand Down Expand Up @@ -43,4 +42,11 @@ public ResponseEntity<Void> logout(HttpSession session) {
memberService.logout(session);
return ResponseEntity.ok().build();
}

@Operation(summary = "회원 삭제")
@DeleteMapping
public ResponseEntity<Void> delete(@CurrentUser Member member) {
memberService.delete(member);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.psq.backend.member.exception.NoSuchMemberException;
import com.psq.backend.problem.domain.Category;
import com.psq.backend.problem.domain.Problem;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -20,13 +19,13 @@

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.when;

public class MemberServiceTest extends ServiceTest {
private MockHttpSession session;
private SessionInfo sessionInfo;
@BeforeEach
void setup() {
member = new Member("[email protected]", encodePassword, salt);
Expand All @@ -42,7 +41,6 @@ void setup() {

request = new MockHttpServletRequest();
session = new MockHttpSession();
sessionInfo = new SessionInfo(member.getId(), member.getEmail());
}

@Test
Expand Down Expand Up @@ -126,6 +124,13 @@ void logoutTest() {
memberService.logout(session);

// then
Assertions.assertThat(session.isInvalid()).isTrue();
assertThat(session.isInvalid()).isTrue();
}

@Test
@DisplayName("계정을 삭제된다")
public void deleteMemberTest() throws NoSuchMethodException {
// given, when, then
assertDoesNotThrow(() -> memberService.delete(member));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import static com.psq.backend.common.docs.ApiDocumentUtil.getDocumentRequest;
import static com.psq.backend.common.docs.ApiDocumentUtil.getDocumentResponse;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -30,9 +32,11 @@ void setup() {
member = new Member("[email protected]", "123", "salt");
ReflectionTestUtils.setField(member, "id", 1L);

session = new MockHttpSession();
sessionInfo = new SessionInfo(member.getId(), member.getEmail());
session = new MockHttpSession();

session.setAttribute("sessionInfo", sessionInfo);
when(sessionUtil.getSessionInfo()).thenReturn(sessionInfo);
}

@Test
Expand Down Expand Up @@ -265,4 +269,19 @@ public void logoutTest() throws Exception {
getDocumentResponse()
));
}

@Test
@DisplayName("계정을 삭제한다")
public void deleteTest() throws Exception {
// when, then
mockMvc.perform(delete(baseURL)
.session(session)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent())
.andDo(print())
.andDo(document("member/delete/success",
getDocumentRequest(),
getDocumentResponse()
));
}
}

0 comments on commit 68e0bd2

Please sign in to comment.