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

feat/31-withdraw-member #32

Merged
merged 12 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
34 changes: 34 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.7.6'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}

group = 'com.sptp'
Expand Down Expand Up @@ -46,8 +53,35 @@ dependencies {
//gson
implementation 'com.google.code.gson:gson:2.9.0'

//QueryDsl
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
implementation "com.querydsl:querydsl-apt:${queryDslVersion}"

//H2
runtimeOnly 'com.h2database:h2'
}

tasks.named('test') {
useJUnitPlatform()
}

//querydsl ์ถ”๊ฐ€ ์‹œ์ž‘
def querydslDir = "$buildDir/generated/querydsl"
querydsl {
jpa = true
querydslSourcesDir = querydslDir
}
sourceSets {
main.java.srcDir querydslDir
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
querydsl.extendsFrom compileClasspath
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
//querydsl ์ถ”๊ฐ€ ๋
15 changes: 15 additions & 0 deletions src/main/java/com/sptp/backend/common/config/DBConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sptp.backend.common.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;

@Configuration
public class DBConfig {
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ public void saveKeyword(Member member, List<String> keywordList) {
memberKeywordRepository.save(memberKeyword);
}
}
}

@Transactional
public void updateUser(Long loginMemberId, MemberUpdateRequest dto) {
Expand All @@ -199,5 +198,12 @@ public void updateUser(Long loginMemberId, MemberUpdateRequest dto) {

findMember.updateUser(dto);
}

@Transactional
public void withdrawUser(Long loginMemberId) {

memberKeywordRepository.deleteByMemberId(loginMemberId);
memberRepository.deleteById(loginMemberId);
}
}

11 changes: 11 additions & 0 deletions src/main/java/com/sptp/backend/member/web/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,15 @@ public ResponseEntity<?> updateUser(@AuthenticationPrincipal CustomUserDetails u

return new ResponseEntity(HttpStatus.OK);
}

@DeleteMapping("/members")
public ResponseEntity<PasswordChangeRequest> withdrawUser(
@RequestHeader("accessToken") String accessToken,
@AuthenticationPrincipal CustomUserDetails userDetails) {

memberService.logout(accessToken);
memberService.withdrawUser(userDetails.getMember().getId());

return ResponseEntity.status(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sptp.backend.memberkeyword.repository;

public interface MemberKeywordCustomRepository {
void deleteByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sptp.backend.memberkeyword.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import static com.sptp.backend.memberkeyword.repository.QMemberKeyword.*;

@RequiredArgsConstructor
public class MemberKeywordCustomRepositoryImpl implements MemberKeywordCustomRepository {

private final JPAQueryFactory queryFactory;

@Override
public void deleteByMemberId(Long memberId) {
queryFactory.delete(memberKeyword)
.where(memberKeyword.member.id.eq(memberId))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberKeywordRepository extends JpaRepository<MemberKeyword, Long> {
public interface MemberKeywordRepository extends JpaRepository<MemberKeyword, Long>, MemberKeywordCustomRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.sptp.backend.memberkeyword.repository;

import com.sptp.backend.common.config.DBConfig;
import com.sptp.backend.common.config.PropertyConfig;
import com.sptp.backend.member.repository.Member;
import com.sptp.backend.memberkeyword.MemberKeywordMap;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.List;

@DataJpaTest
@Import({DBConfig.class, PropertyConfig.class})
class MemberKeywordRepositoryTest {

@Autowired
EntityManager entityManager;

@Autowired
MemberKeywordRepository memberKeywordRepository;

@Nested
class deleteByMemberIdTest {

Member member = createMember();

@BeforeEach
void init() {
// ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
entityManager.persist(member);
for (int keywordId = 1; keywordId < 3; keywordId++) {
entityManager.persist(createMemberKeyword(member, keywordId));
}
}

@Test
void success() {
//given
long memberId = member.getId();
long count = memberKeywordRepository.count();

//when
memberKeywordRepository.deleteByMemberId(memberId);
long currentCount = memberKeywordRepository.count();

//then
Assertions.assertThat(count).isNotEqualTo(currentCount);
Assertions.assertThat(currentCount).isEqualTo(0L);
}
}

private static MemberKeyword createMemberKeyword(Member member, int keywordId) {
return MemberKeyword.builder()
.member(member)
.keywordId(keywordId)
.build();
}

private static Member createMember() {

return Member.builder()
.userId("test")
.password("12345678")
Copy link
Member

Choose a reason for hiding this comment

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

Member์˜ ์—ฌ๋Ÿฌ ํ•„๋“œ ์ค‘ userId๋ž‘ passwordํ•„๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์‹  ๊ฑด ์ถ”ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•จ์ธ๊ฐ€์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์ „๋ถ€ null๊ฐ’์„ ๊ฐ€์ ธ๋„ ๋˜์ง€๋งŒ, userId์™€ password ์ •๋„๋งŒ ์ž„์˜๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฑฐํ•ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹น

Copy link
Member

Choose a reason for hiding this comment

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

๋„ต!

.build();
}
}
17 changes: 17 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
test:
database:
# replace: none // ์ธ๋ฉ”๋ชจ๋ฆฌ DB๊ฐ€ ์•„๋‹Œ MySQL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •

datasource:
url: ${application.spring.datasource.url}
username: ${application.spring.datasource.username}
password: ${application.spring.datasource.password}
Copy link
Member

Choose a reason for hiding this comment

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

์ด๊ฑฐ test/resources์— env.propertiesํŒŒ์ผ ์ถ”๊ฐ€ํ•˜์…”์„œ ์ ์šฉํ•˜์‹ ๊ฑด๊ฐ€์š”? ๊ทธ๋ ‡๋‹ค๋ฉด ํ˜น์‹œ ๊ธฐ์กด์— main/resources์— ์žˆ๋Š” env.properties์˜ ๋ณ€์ˆ˜๋ช…๊ณผ ๊ฐ™์€๋ฐ ๋ฌธ์ œ ์—†๋‚˜์š”?? ์ „ env.propertiesํŒŒ์ผ์˜ ๋ณ€์ˆ˜๊ฐ€ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋“ฑ๋ก๋˜๋Š” ๊ฑธ๋กœ ์•Œ๊ณ  ์žˆ์–ด์„œ์š”!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์•„ test/resources์— ํŒŒ์ผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๊ฑฐ ๋งž์Šต๋‹ˆ๋‹ค! main์— ์žˆ๋Š” ํด๋”์™€ ๋‹ค๋ฅธ ์ ์€ url์ด h2๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค๋Š” ์ ์ด์—์šฉ

Copy link
Member

Choose a reason for hiding this comment

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

main์˜ envํŒŒ์ผ์—์„œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•œ ๊ฒƒ๊ณผ ๋ณ€์ˆ˜๋ช…์ด ๊ฐ™์€๋ฐ ๊ดœ์ฐฎ๋‚˜์šฉ? application.spring.datasource.url ๋ณ€์ˆ˜๋ช…์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์•ผํ•˜์ง€ ์•Š๋‚˜ ์‹ถ์–ด์„œ์š”! ๋‘˜ ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ’์ด ๋ฎ์–ด ์”Œ์–ด์ง€์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”

Copy link
Member

Choose a reason for hiding this comment

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

๋ณ€์ˆ˜๋ช…์€ ์‚ฌ์‹ค ์–ด๋–ค ๊ฑธ ์จ๋„ ๋ฌด๋ฐฉํ•ด์„œ ๊ธฐ์กด main์˜ envํŒŒ์ผ์—์„œ ๋“ฑ๋กํ–ˆ๋˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ช…๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋„ค์ด๋ฐ ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„๊ฑฐ๊ฐ™์•„์š”-! test.application.spring.datasource.url์ฒ˜๋Ÿผ ์•ž์— test๋งŒ ๋ถ™์—ฌ์ฃผ์…”๋„ ๊ดœ์ฐฎ๊ตฌ์šฉ

Copy link
Contributor Author

@JunYoung-C JunYoung-C Jan 5, 2023

Choose a reason for hiding this comment

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

๋ฐ”๊พผ๋‹ค๋ฉด url ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ Oauth์™€ ๊ฐ™์€ ๊ฒƒ๋“ค๋„ ๋ชจ๋‘ test๋ฅผ ๋ถ™์ธ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
image
ํ•˜์ง€๋งŒ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด @value์—์„œ test๊ฐ€ ์—†๋Š” ๋ณ€์ˆ˜๋ช…์„ ์š”๊ตฌํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— main/resources/application.yml๊ณผ ๋ณ€์ˆ˜๋ช…์„ ๋‹ฌ๋ฆฌํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋„ค์š”.
๋˜ํ•œ, test/resouces ํ•˜์œ„์— application.yml์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š”, main/resources/application.yml์ด ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์•„ํ•˜ ๊ทธ๋Ÿฐ ์˜๋ฏธ์˜€๊ตฐ์š”
image
์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์œ„์ฒ˜๋Ÿผ datasource๋ฅผ ๋”ฐ๋กœ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด, ์ž๋™์œผ๋กœ h2๋ฅผ ๊ตฌ๋™ํ•ด์„œ ๋Œ๋ฆฐ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ๋˜์—ˆ๋„ค์š”. ์•„์˜ˆ ์ œ๊ฑฐํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?

Copy link
Member

Choose a reason for hiding this comment

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

๊ทธ๊ฒƒ๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™์•„์šฉ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €๋ ‡๊ฒŒ ์ž…๋ ฅ ์•ˆ ํ•ด์ค˜๋„ ์•Œ์•„์„œ h2 ๊ตฌ๋™์ด ๋˜๋Š”๊ฑด๊ฐ€์š”??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

๋„ต ๋งž์Šต๋‹ˆ๋‹ค

Copy link
Member

Choose a reason for hiding this comment

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

๊ทธ๋Ÿผ h2๊ด€๋ จ ๋ถ€๋ถ„์€ ๋นผ๋Š” ๊ฑธ๋กœ ํ•ฉ์‹œ๋‹น

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์ˆ˜์ •ํ•˜๊ณ  ๋ฐ”๋กœ ๋จธ์ง€ํ• ๊ป˜์šฅ

driver-class-name: org.h2.Driver

jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
format_sql: true