diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index d0d08fe..f0845cf 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -1,7 +1,9 @@ package com.javajober.member.service; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; import javax.transaction.Transactional; @@ -19,6 +21,10 @@ import com.javajober.core.refreshToken.repository.RefreshTokenRepository; import com.javajober.core.security.JwtTokenizer; import com.javajober.core.refreshToken.domain.RefreshToken; +import com.javajober.space.domain.AddSpace; +import com.javajober.space.domain.SpaceType; +import com.javajober.space.dto.request.SpaceSaveRequest; +import com.javajober.space.repository.AddSpaceRepository; @Service public class MemberService { @@ -26,13 +32,15 @@ public class MemberService { private final PasswordEncoder passwordEncoder; private final JwtTokenizer jwtTokenizer; private final RefreshTokenRepository refreshTokenRepository; + private final AddSpaceRepository addSpaceRepository; public MemberService(MemberRepository memberRepository, PasswordEncoder passwordEncoder, JwtTokenizer jwtTokenizer, - RefreshTokenRepository refreshTokenRepository) { + RefreshTokenRepository refreshTokenRepository, AddSpaceRepository addSpaceRepository) { this.memberRepository = memberRepository; this.passwordEncoder = passwordEncoder; this.jwtTokenizer = jwtTokenizer; this.refreshTokenRepository = refreshTokenRepository; + this.addSpaceRepository = addSpaceRepository; } @Transactional @@ -47,9 +55,39 @@ public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest) { member.setPassword(passwordEncoder.encode(memberSignupRequest.getPassword())); Member saveMember = memberRepository.save(member); + initializeAndSaveNewMemberSpaces(member); + return new MemberSignupResponse(saveMember); } + private void initializeAndSaveNewMemberSpaces(Member member) { + + SpaceSaveRequest personalSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.PERSONAL.getEngTitle(), member.getMemberName()); + SpaceSaveRequest organizationSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.ORGANIZATION.getEngTitle(), "임시회사명"); + + Set spaces = new HashSet<>(); + + AddSpace personalSpace = SpaceSaveRequest.toEntity(personalSpaceRequest, member); + spaces.add(personalSpace); + + AddSpace organizationSpace = SpaceSaveRequest.toEntity(organizationSpaceRequest, member); + spaces.add(organizationSpace); + + saveSpaces(spaces); + } + + private SpaceSaveRequest createSpaceSaveRequest(String spaceTitle, String spaceType, String representativeName) { + return SpaceSaveRequest.builder() + .spaceTitle(spaceTitle) + .spaceType(spaceType) + .representativeName(representativeName) + .build(); + } + + private void saveSpaces(Set spaces) { + addSpaceRepository.saveAll(spaces); + } + @Transactional public MemberLoginResponse login(MemberLoginRequest loginDto) { Member member = memberRepository.findMember(loginDto.getEmail()).orElseThrow(() diff --git a/src/main/java/com/javajober/space/dto/request/SpaceSaveRequest.java b/src/main/java/com/javajober/space/dto/request/SpaceSaveRequest.java index b274d5c..6ddb9d6 100644 --- a/src/main/java/com/javajober/space/dto/request/SpaceSaveRequest.java +++ b/src/main/java/com/javajober/space/dto/request/SpaceSaveRequest.java @@ -3,6 +3,8 @@ import com.javajober.member.domain.Member; import com.javajober.space.domain.AddSpace; import com.javajober.space.domain.SpaceType; + +import lombok.Builder; import lombok.Getter; @Getter @@ -13,7 +15,15 @@ public class SpaceSaveRequest { private String representativeName; private String spaceType; - public SpaceSaveRequest() { + private SpaceSaveRequest() { + } + + @Builder + public SpaceSaveRequest(Long memberId, String spaceTitle, String representativeName, String spaceType) { + this.memberId = memberId; + this.spaceTitle = spaceTitle; + this.representativeName = representativeName; + this.spaceType = spaceType; } public static AddSpace toEntity(SpaceSaveRequest request, Member member) { diff --git a/src/main/java/com/javajober/space/repository/AddSpaceRepository.java b/src/main/java/com/javajober/space/repository/AddSpaceRepository.java index ab681ed..b2c7ec2 100644 --- a/src/main/java/com/javajober/space/repository/AddSpaceRepository.java +++ b/src/main/java/com/javajober/space/repository/AddSpaceRepository.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Optional; +import java.util.Set; import com.javajober.core.exception.ApiStatus; import com.javajober.core.exception.ApplicationException; @@ -26,6 +27,8 @@ public interface AddSpaceRepository extends Repository { @Query("SELECT s.id FROM AddSpace s WHERE s.spaceType = :spaceType AND s.member.id = :memberId") List findAddSpaceIdBySpaceTypeAndMemberId(@Param("spaceType") final SpaceType spaceType, @Param("memberId") final Long memberId); + Set saveAll(final Iterable addSpaces); + default AddSpace findAddSpace (final Long id) { return findById(id) .orElseThrow(() -> new ApplicationException(ApiStatus.NOT_FOUND, "존재하지 않는 스페이스입니다."));