Skip to content

Commit

Permalink
feat: 회원가입 직후 프로필 사진, 관심사, 닉네임 삽입 로직 (#51)
Browse files Browse the repository at this point in the history
기존 소셜로그인 - originName으로 변경, 관심사 Member(1) : Interest(N)로 매핑, 기존 값 수정 Editor 사용
  • Loading branch information
toychip committed Feb 10, 2024
1 parent 8cddc90 commit 6a3fb57
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static CommentResponse getCommentResponse(final Comment questionComment,
.commentId(questionComment.getId())
.commentContent(questionComment.getContent())
.parentId(parentCommentId)
.writer(questionComment.getMember().getName())
.writer(questionComment.getMember().getOriginName())
.writtenTime(formatCreatedDate)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static HoneyTipSingleResponse of(final HoneyTip honeyTip, final List<Hone
.honeyTipId(honeyTip.getId())
.title(honeyTip.getTitle())
.content(honeyTip.getContent())
.writer(honeyTip.getMember().getName())
.writer(honeyTip.getMember().getOriginName())
.writtenTime(formattedCreatedDate)
.category(honeyTip.getCategory()) // 한글 카테고리 이름으로 반환
.likeCount(likeCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TitleResponse questionOf(final Question question) {
LocalDateTime createdDate = question.getCreatedDate();
String formattedCreatedDate = getFormattedCreatedDate(createdDate);
int commentCount = question.getQuestionComments().size();
String writer = question.getMember().getName();
String writer = question.getMember().getOriginName();

return TitleResponse.builder()
.honeyTipId(question.getId())
Expand All @@ -41,7 +41,7 @@ public static TitleResponse honeyTipFrom(final HoneyTip honeyTip) {
String formattedCreatedDate = getFormattedCreatedDate(createdDate);
int commentCount = honeyTip.getHoneyTipComments().size();
int likeCount = honeyTip.getHoneyTipLikes().size();
String writer = honeyTip.getMember().getName();
String writer = honeyTip.getMember().getOriginName();

return TitleResponse.builder()
.honeyTipId(honeyTip.getId())
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/com/api/ttoklip/domain/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.api.ttoklip.domain.common.report.domain.Report;
import com.api.ttoklip.domain.honeytip.like.domain.HoneyTipLike;
import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip;
import com.api.ttoklip.domain.profile.domain.Profile;
import com.api.ttoklip.domain.member.editor.MemberEditor;
import com.api.ttoklip.domain.member.editor.MemberEditor.MemberEditorBuilder;
import com.api.ttoklip.domain.privacy.domain.Interest;
import com.api.ttoklip.domain.privacy.domain.Profile;
import com.api.ttoklip.domain.question.post.domain.Question;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -36,7 +39,7 @@ public class Member {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String originName;

@Email
private String naverEmail;
Expand All @@ -45,26 +48,56 @@ public class Member {

private String provider;

private String nickname;
// private String street;
private int independentYear;
private int independentMonth;

@Enumerated(EnumType.STRING)
private Role role;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "member", cascade = CascadeType.REMOVE)
private Profile profile;

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Interest> interests = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Report> reports = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<HoneyTip> honeyTips = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Question> questions = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<HoneyTipLike> honeyTipLikes = new ArrayList<>();

public MemberEditorBuilder toEditor() {
return MemberEditor.builder()
.independentYear(independentYear)
.independentMonth(independentMonth)
// .street()
.nickname(nickname);
}

public void insertPrivacy(MemberEditor memberEditor) {
System.out.println("Member.insertPrivacy");
independentYear = memberEditor.getIndependentYear();
independentMonth = memberEditor.getIndependentMonth();
nickname = memberEditor.getNickname();
System.out.println("independentYear = " + independentYear);
System.out.println("independentMonth = " + independentMonth);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.api.ttoklip.domain.member.editor;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;

@Getter
@RequiredArgsConstructor
public class MemberEditor {
private final String nickname;
private final String street;
private final int independentYear;
private final int independentMonth;

public static MemberEditorBuilder builder() {
return new MemberEditorBuilder();
}

public static class MemberEditorBuilder {
private String nickname;
private String street;
private int independentYear;
private int independentMonth;

MemberEditorBuilder(){
}

public MemberEditorBuilder nickname(final String nickname) {
if (StringUtils.hasText(nickname)) {
this.nickname = nickname;
}
return this;
}

public MemberEditorBuilder street(final String street) {
if (StringUtils.hasText(street)) {
this.street = street;
}
return this;
}

public MemberEditorBuilder independentYear(final int independentYear) {
this.independentYear = independentYear;
return this;
}

public MemberEditorBuilder independentMonth(final int independentMonth) {
this.independentMonth = independentMonth;
return this;
}

public MemberEditor build() {
return new MemberEditor(nickname, street, independentYear, independentMonth);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.member.domain.QMember;
import com.api.ttoklip.domain.profile.domain.QProfile;
import com.api.ttoklip.domain.privacy.domain.QProfile;
import com.api.ttoklip.global.exception.ApiException;
import com.api.ttoklip.global.exception.ErrorType;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.api.ttoklip.domain.privacy;

import com.api.ttoklip.domain.privacy.dto.PrivacyCreateRequest;
import com.api.ttoklip.domain.privacy.service.ProfileService;
import com.api.ttoklip.global.success.Message;
import com.api.ttoklip.global.success.SuccessResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
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;

@Tag(name = "AFTER AUTH LOGIN API", description = "꿀팁공유해요 소셜 로그인 후 회원가입 API입니다.")
@RestController
@RequestMapping("/api/v1/privacy")
@RequiredArgsConstructor
public class ProfileController {

private final ProfileService profileService;

@PostMapping("/insert")
public SuccessResponse<Long> register(@RequestBody @Validated final PrivacyCreateRequest request) {
profileService.insert(request);

return new SuccessResponse<>(1000L);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/api/ttoklip/domain/privacy/domain/Interest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.api.ttoklip.domain.privacy.domain;

import com.api.ttoklip.domain.common.Category;
import com.api.ttoklip.domain.member.domain.Member;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Interest {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Enumerated(EnumType.STRING)
private Category category;

public static Interest of(final Member member, final Category category) {
return Interest.builder()
.member(member)
.category(category)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.ttoklip.domain.profile.domain;
package com.api.ttoklip.domain.privacy.domain;

import com.api.ttoklip.domain.member.domain.Member;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.api.ttoklip.domain.privacy.dto;

import com.api.ttoklip.domain.common.Category;
import com.api.ttoklip.global.exception.ApiException;
import com.api.ttoklip.global.exception.ErrorType;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PrivacyCreateRequest {

private String street;
private String nickname;
private List<String> categories;

@Min(0)
@Max(99)
private int independentYear;

@Min(0)
@Max(11)
private int independentMonth;

public List<Category> getCategories() {
validCategorySize();
return categories.stream()
.map(Category::findCategoryByValue)
.toList();
}

private void validCategorySize() {
if (categories.size() > 3) {
throw new ApiException(ErrorType.INVALID_CATEGORIES_SIZE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.api.ttoklip.domain.privacy.repository;

import com.api.ttoklip.domain.privacy.domain.Interest;
import org.springframework.data.jpa.repository.JpaRepository;

public interface InterestRepository extends JpaRepository <Interest, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.api.ttoklip.domain.profile.repository;
package com.api.ttoklip.domain.privacy.repository;

import com.api.ttoklip.domain.profile.domain.Profile;
import com.api.ttoklip.domain.privacy.domain.Profile;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProfileRepository extends JpaRepository<Profile, Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.api.ttoklip.domain.privacy.service;

import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;

import com.api.ttoklip.domain.common.Category;
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.member.editor.MemberEditor;
import com.api.ttoklip.domain.member.editor.MemberEditor.MemberEditorBuilder;
import com.api.ttoklip.domain.member.repository.MemberRepository;
import com.api.ttoklip.domain.member.service.MemberService;
import com.api.ttoklip.domain.privacy.domain.Interest;
import com.api.ttoklip.domain.privacy.domain.Profile;
import com.api.ttoklip.domain.privacy.dto.PrivacyCreateRequest;
import com.api.ttoklip.domain.privacy.repository.InterestRepository;
import com.api.ttoklip.domain.privacy.repository.ProfileRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ProfileService {

private final ProfileRepository profileRepository;
private final InterestRepository interestRepository;
private final MemberService memberService;

// 회원가입시 자동으로 프로필 이미지 기입
@Transactional
public Long register(final Profile profile) {
profileRepository.save(profile);
return profile.getId();
}

// 회원 가입 후 입력 받을 닉네임, 우리동네 설정, 나의 동릭 경험, 관심 카테고리 선택
@Transactional
public void insert(final PrivacyCreateRequest request) {
Member currentMember = memberService.findByIdWithProfile(getCurrentMember().getId());
MemberEditor editor = getEditor(currentMember, request);
currentMember.insertPrivacy(editor);
registerInterest(request, currentMember);
}

private MemberEditor getEditor(final Member currentMember, final PrivacyCreateRequest request) {
int independentYear = request.getIndependentYear();
int independentMonth = request.getIndependentMonth();
String nickname = request.getNickname(); // ToDo 추후에 닉네임 중복되는지 검사하는 로직 처리
// String street = request.getStreet(); // ToDo 추후에 Embeded로 주소 처리
MemberEditorBuilder editorBuilder = currentMember.toEditor();
return editorBuilder
.independentYear(independentYear)
.independentMonth(independentMonth)
.nickname(nickname)
.build();
}

private void registerInterest(final PrivacyCreateRequest request, final Member currentMember) {
List<Category> requestCategories = request.getCategories();
List<Interest> interests = requestCategories
.stream()
.map(category -> Interest.of(currentMember, category))
.toList();
interestRepository.saveAll(interests);
}
}
Loading

0 comments on commit 6a3fb57

Please sign in to comment.