Skip to content

Commit

Permalink
🗃️ :: (#10) change related database persistence architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
iqpizza6349 committed Jan 19, 2023
1 parent c46de79 commit 10c15e3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class Address {

private final Long id;
private final Long userId; // related on Member
private final String detailAddress;
private final String streetAddress;
private final short zipCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class Parent {

private final Long id;
private final Long userId; // related on Member
private final LocalDate birth;
private final String contact;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dgsw.cns.global.domain.BaseIdEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToOne;
import lombok.*;
import lombok.experimental.SuperBuilder;

Expand All @@ -26,4 +27,7 @@ public class AddressEntity extends BaseIdEntity<Long> {
@Column(nullable = false)
private short zipCode;

@OneToOne
private MemberEntity member;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MemberEntity extends BaseIdEntity<Long> implements Serializable {

/**
* Fields in a "Serializable" class should either be transient or serializable
* @see com.dgsw.cns.user.domain.embedd.EmbeddedUserId
* @see com.dgsw.cns.user.domain.embedded.EmbeddedUserId
*/
@Serial
private static final long serialVersionUID = 5811194024882600393L;
Expand All @@ -46,4 +46,13 @@ public class MemberEntity extends BaseIdEntity<Long> implements Serializable {
@Enumerated(EnumType.STRING)
private Role role = Role.STUDENT;

@OneToOne(mappedBy = "member", orphanRemoval = true)
private AddressEntity address;

@OneToOne(mappedBy = "member", orphanRemoval = true)
private ParentEntity parent;

@OneToOne(mappedBy = "member", orphanRemoval = true)
private PrivacyEntity privacy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dgsw.cns.global.domain.BaseIdEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToOne;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -33,4 +34,7 @@ public class ParentEntity extends BaseIdEntity<Long> {
@Column(length = 16, nullable = false)
private String relation;

@OneToOne
private MemberEntity member;

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.dgsw.cns.user.domain;

import com.dgsw.cns.user.domain.embedd.EmbeddedUserId;
import com.dgsw.cns.user.domain.embedded.EmbeddedUserId;
import com.dgsw.cns.user.domain.enums.Gender;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.time.LocalDate;

Expand All @@ -21,7 +18,13 @@
public class PrivacyEntity {

@EmbeddedId
private EmbeddedUserId userId;
private EmbeddedUserId memberId;

@OneToOne
@MapsId("member_id")
@JoinColumn(name = "member_id")
@ToString.Exclude
private MemberEntity member;

@Column(nullable = false)
private LocalDate birth;
Expand Down

This file was deleted.

13 changes: 13 additions & 0 deletions User-Spi/src/main/java/com/dgsw/cns/user/mapper/AddressMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
import com.dgsw.cns.global.mapper.Mapper;
import com.dgsw.cns.user.domain.Address;
import com.dgsw.cns.user.domain.AddressEntity;
import com.dgsw.cns.user.domain.MemberEntity;
import com.dgsw.cns.user.domain.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AddressMapper implements Mapper<Address, AddressEntity> {

private final MemberRepository memberRepository;

@Override
public AddressEntity domainToEntity(Address address) {
return AddressEntity.builder()
.id(address.getId())
.member(findMemberById(address.getUserId()))
.detailAddress(address.getDetailAddress())
.streetAddress(address.getStreetAddress())
.zipCode(address.getZipCode())
Expand All @@ -22,9 +29,15 @@ public AddressEntity domainToEntity(Address address) {
public Address entityToEntity(AddressEntity addressEntity) {
return Address.builder()
.id(addressEntity.getId())
.userId(addressEntity.getMember().getId())
.detailAddress(addressEntity.getDetailAddress())
.streetAddress(addressEntity.getStreetAddress())
.zipCode(addressEntity.getZipCode())
.build();
}

private MemberEntity findMemberById(long memberId) {
return memberRepository.findById(memberId)
.orElseThrow();
}
}
13 changes: 13 additions & 0 deletions User-Spi/src/main/java/com/dgsw/cns/user/mapper/ParentMapper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.dgsw.cns.user.mapper;

import com.dgsw.cns.global.mapper.Mapper;
import com.dgsw.cns.user.domain.MemberEntity;
import com.dgsw.cns.user.domain.Parent;
import com.dgsw.cns.user.domain.ParentEntity;
import com.dgsw.cns.user.domain.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ParentMapper implements Mapper<Parent, ParentEntity> {

private final MemberRepository memberRepository;

@Override
public ParentEntity domainToEntity(Parent parent) {
return ParentEntity.builder()
.id(parent.getId())
.member(findMemberById(parent.getUserId()))
.birth(parent.getBirth())
.contact(parent.getContact())
.name(parent.getName())
Expand All @@ -23,10 +30,16 @@ public ParentEntity domainToEntity(Parent parent) {
public Parent entityToEntity(ParentEntity parentEntity) {
return Parent.builder()
.id(parentEntity.getId())
.userId(parentEntity.getMember().getId())
.birth(parentEntity.getBirth())
.contact(parentEntity.getContact())
.name(parentEntity.getName())
.relation(parentEntity.getRelation())
.build();
}

private MemberEntity findMemberById(long memberId) {
return memberRepository.findById(memberId)
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.dgsw.cns.user.domain.MemberEntity;
import com.dgsw.cns.user.domain.Privacy;
import com.dgsw.cns.user.domain.PrivacyEntity;
import com.dgsw.cns.user.domain.embedd.EmbeddedUserId;
import com.dgsw.cns.user.domain.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -18,7 +17,7 @@ public class PrivacyMapper implements Mapper<Privacy, PrivacyEntity> {
@Override
public PrivacyEntity domainToEntity(Privacy privacy) {
return PrivacyEntity.builder()
.userId(new EmbeddedUserId(findMemberById(privacy.getUserId())))
.member(findMemberById(privacy.getUserId()))
.birth(privacy.getBirth())
.contact(privacy.getContact())
.meritCode(privacy.getMeritCode())
Expand All @@ -29,7 +28,7 @@ public PrivacyEntity domainToEntity(Privacy privacy) {
@Override
public Privacy entityToEntity(PrivacyEntity privacyEntity) {
return Privacy.builder()
.userId(privacyEntity.getUserId().getMember().getId())
.userId(privacyEntity.getMemberId().getMemberId())
.birth(privacyEntity.getBirth())
.contact(privacyEntity.getContact())
.meritCode(privacyEntity.getMeritCode())
Expand Down

0 comments on commit 10c15e3

Please sign in to comment.