Skip to content

Commit

Permalink
[BE] feat: 동시 요청 시 발생할 수 있는 중복 회원 생성 문제 해결 (#443) (#445)
Browse files Browse the repository at this point in the history
* feat: member unique 제약조건 추가

* feat: admin unique 제약조건 추가

* feat: 회원 탈퇴시 로깅

* refactor: 회원 탈퇴시 social_id만 Null로 설정

* refactor: UNIQUE 제약조건 이름 수정

* feat: flyway 스크립트 추가

* test: Unique제약조건으로 깨지는 테스트 수정
  • Loading branch information
xxeol2 authored Sep 18, 2023
1 parent fd94534 commit 6d2b1d1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
10 changes: 10 additions & 0 deletions backend/src/main/java/com/festago/admin/domain/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;

@Entity
@Table(
uniqueConstraints = {
@UniqueConstraint(
name = "UNIQUE_USERNAME",
columnNames = {"username"}
)
}
)
public class Admin extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import com.festago.common.exception.NotFoundException;
import com.festago.member.domain.Member;
import com.festago.member.repository.MemberRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AuthService {

private static final Logger log = LoggerFactory.getLogger(AuthService.class);

private final MemberRepository memberRepository;

public AuthService(MemberRepository memberRepository) {
Expand All @@ -35,6 +39,12 @@ private Member signUp(UserInfo userInfo) {
public void deleteMember(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));
logDeleteMember(member);
memberRepository.delete(member);
}

private void logDeleteMember(Member member) {
log.info("[DELETE MEMBER] memberId: {} / socialType: {} / socialId: {}",
member.getId(), member.getSocialType(), member.getSocialId());
}
}
16 changes: 14 additions & 2 deletions backend/src/main/java/com/festago/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Entity
@SQLDelete(sql = "UPDATE member SET deleted_at = now(), nickname = '탈퇴한 회원', profile_image = '' WHERE id=?")
@SQLDelete(sql = "UPDATE member SET deleted_at = now(), nickname = '탈퇴한 회원', profile_image = '', social_id = null WHERE id=?")
@Where(clause = "deleted_at is null")
@Table(
uniqueConstraints = {
@UniqueConstraint(
name = "SOCIAL_UNIQUE",
columnNames = {
"socialId",
"socialType"
}
)
}
)
public class Member extends BaseTimeEntity {

private static final String DEFAULT_IMAGE_URL = "https://festa-go.site/images/default-profile.png";
Expand All @@ -25,7 +38,6 @@ public class Member extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(max = 255)
private String socialId;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table admin
add constraint UNIQUE_USERNAME unique (username);

alter table member
add constraint UNIQUE_SOCIAL unique (social_id, social_type);
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class 회원의_ID로_에매한_티켓을_모두_조회 {
@Test
void 성공() {
// given
Member member1 = memberRepository.save(MemberFixture.member().build());
Member member2 = memberRepository.save(MemberFixture.member().build());
Member member1 = memberRepository.save(MemberFixture.member().socialId("abc").build());
Member member2 = memberRepository.save(MemberFixture.member().socialId("def").build());

Festival festival = festivalRepository.save(FestivalFixture.festival().build());
Stage stage1 = stageRepository.save(StageFixture.stage().festival(festival).build());
Expand Down

0 comments on commit 6d2b1d1

Please sign in to comment.