Skip to content

프로필 수정 로직 변경 및 페이징 목록 섬네일 응답 추가 #82

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

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.gdyd.gdydapi.request.member;

import com.gdyd.gdydsupport.annotation.ValidEmail;
import com.gdyd.gdydcore.domain.member.Grade;
import com.gdyd.gdydcore.domain.member.UniversityMajorCategory;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

@Schema(description = "프로필 업데이트 요청")
public record UpdateProfileRequest(
@NotBlank(message = "이메일은 필수 입력 값입니다.")
@ValidEmail(message = "올바르지 않은 이메일 형식입니다.")
@Schema(description = "이메일", example = "[email protected]")
String email,

@NotBlank(message = "닉네임은 필수 입력 값입니다.")
@NotBlank(message = "닉네임은 필수 입력 값입니다.(닉네임은 다른 유저와 중복될 수 없습니다.)")
@Schema(description = "닉네임", example = "인천교회출신스님")
String nickname
String nickname,

@NotBlank(message = "학년은 필수 입력 값입니다.")
@Schema(description = "학년", example = "SECOND")
Grade grade,

@NotBlank(message = "프로필 이미지 URL은 필수 입력 값입니다.")
@Schema(description = "프로필 이미지 URL", example = "DEFAULT")
String profileImageUrl,

@NotNull(message = "대학교 학과 카테고리는 필수 입력 값입니다.(고등학생은 DEFAULT로 설정하면 됩니다.)")
@Schema(description = "대학교 학과 카테고리", example = "COMPUTER_SW")
UniversityMajorCategory universityMajorCategory
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ public record GetPostSummaryResponse(
Long likeCount,

@Schema(description = "Post 댓글 수")
Long comments
Long comments,

@Schema(description = "Post 썸네일 URL", example = "https://gdyd.s3.ap-northeast-2.amazonaws.com/...")
String thumbnailUrl
) {
public static GetPostSummaryResponse from(Post post) {
String thumbnailUrl = post.getPostMedias().isEmpty() ? "DEFAULT" : post.getPostMedias().get(0).getUrl();
return GetPostSummaryResponse.builder()
.postId(post.getId())
.memberNickname(post.getMember().getNickname())
.title(post.getTitle())
.content(post.getContent())
.likeCount(post.getLikeCount())
.comments((long)post.getComments().size())
.thumbnailUrl(thumbnailUrl)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.gdyd.gdydapi.response.member;

import com.gdyd.gdydcore.domain.member.*;
import com.gdyd.gdydsupport.exception.BusinessException;
import com.gdyd.gdydsupport.exception.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import org.hibernate.Hibernate;
Expand All @@ -28,17 +26,22 @@ public record ProfileResponse(
@Schema(description = "과 이름", example = "컴퓨터공학과")
String major,

@Schema(description = "학과 카테고리", example = "COMPUTER_SW")
UniversityMajorCategory universityMajorCategory,

@Schema(description = "학년", example = "SECOND")
Grade grade,

@Schema(description = "입학년도", example = "2021")
Long enterYear
Long enterYear,

@Schema(description = "프로필 이미지 URL", example = "DEFAULT")
String profileImageUrl
) {
public static ProfileResponse from(Member member) {
return switch (member.getType()) {
case UNIVERSITY_STUDENT -> from((UniversityStudent) Hibernate.unproxy(member));
case HIGH_SCHOOL_STUDENT -> from((HighSchoolStudent) Hibernate.unproxy(member));
default -> throw new BusinessException(ErrorCode.INVALID_MEMBER_TYPE);
};
}
public static ProfileResponse from(UniversityStudent student) {
Expand All @@ -49,8 +52,10 @@ public static ProfileResponse from(UniversityStudent student) {
.name(student.getName())
.schoolName(student.getUniversityName())
.major(student.getUniversityMajor())
.universityMajorCategory(student.getUniversityMajorCategory())
.grade(student.getUniversityGrade())
.enterYear(student.getEnterYearUniversity())
.profileImageUrl(student.getProfileImage())
.build();
}

Expand All @@ -62,8 +67,10 @@ public static ProfileResponse from(HighSchoolStudent student) {
.name(student.getName())
.schoolName(student.getHighSchoolName())
.major(student.getHighSchoolMajor().toString())
.universityMajorCategory(UniversityMajorCategory.DEFAULT)
.grade(student.getHighSchoolGrade())
.enterYear(student.getEnterYearHighSchool())
.profileImageUrl(student.getProfileImage())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public record HighSchoolStudentQuestionResponse(
@Schema(description = "고등학생 질문에서 답변을 원하는 대학생의 학년", example = "1학년")
String universityGradeTag,

@Schema(description = "고등학생 질문 썸네일 URL", example = "https://gdyd.s3.ap-northeast-2.amazonaws.com/...")
String thumbnailUrl,

@Schema(description = "고등학생 질문에 대한 좋아요 수", example = "3")
Long likeCount,

Expand All @@ -40,6 +43,10 @@ public record HighSchoolStudentQuestionResponse(
public static HighSchoolStudentQuestionResponse from(
HighSchoolStudentQuestion highSchoolStudentQuestion
) {
String thumbnailUrl = highSchoolStudentQuestion.getHighSchoolStudentQuestionMedias().isEmpty()
? "DEFAULT"
: highSchoolStudentQuestion.getHighSchoolStudentQuestionMedias().get(0).getUrl();

return HighSchoolStudentQuestionResponse.builder()
.id(highSchoolStudentQuestion.getId())
.highSchoolStudentNickname(highSchoolStudentQuestion.getHighSchoolStudent().getNickname())
Expand All @@ -48,6 +55,7 @@ public static HighSchoolStudentQuestionResponse from(
.universityNameTag(highSchoolStudentQuestion.getUniversityNameTag())
.universityMajorTag(highSchoolStudentQuestion.getUniversityMajorTag().getValue())
.universityGradeTag(highSchoolStudentQuestion.getUniversityGradeTag().getValue())
.thumbnailUrl(thumbnailUrl)
.likeCount(highSchoolStudentQuestion.getLikeCount())
.answerCount(highSchoolStudentQuestion.getAnswerCount())
.createdAt(highSchoolStudentQuestion.getCreatedAt().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
import com.gdyd.gdydapi.request.member.UpdateProfileRequest;
import com.gdyd.gdydapi.response.member.ProfileResponse;
import com.gdyd.gdydauth.utils.PrincipalUtil;
import com.gdyd.gdydcore.domain.member.HighSchoolStudent;
import com.gdyd.gdydcore.domain.member.Member;
import com.gdyd.gdydcore.domain.member.MemberType;
import com.gdyd.gdydcore.domain.member.UniversityStudent;
import com.gdyd.gdydcore.service.member.HighSchoolStudentService;
import com.gdyd.gdydcore.service.member.MemberService;
import com.gdyd.gdydcore.service.member.UniversityStudentService;
import com.gdyd.gdydsupport.exception.BusinessException;
import com.gdyd.gdydsupport.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -14,12 +21,31 @@
@Transactional
public class MemberCommandService {
private final MemberService memberService;
private final UniversityStudentService universityStudentService;
private final HighSchoolStudentService highSchoolStudentService;

/**
* 프로필 업데이트
* -> 닉네임, 학년, 프로필 이미지 URL, 대학교 학과 카테고리(고등학생은 비우면 됩니다.)
*/
public ProfileResponse updateProfile(UpdateProfileRequest request) {
Long memberId = PrincipalUtil.getMemberIdByPrincipal();
Member member = memberService.getMemberById(memberId);
member.updateEmail(request.email());

if (memberService.existingNickname(request.nickname())) {
throw new BusinessException(ErrorCode.INVALID_NICKNAME);
}

member.updateNickname(request.nickname());
member.updateProfileImage(request.profileImageUrl());
if (member.getType() == MemberType.UNIVERSITY_STUDENT) {
UniversityStudent universityStudent = universityStudentService.getUniversityStudentByMemberId(memberId);
universityStudent.updateGrade(request.grade());
universityStudent.updateUniversityMajorCategory(request.universityMajorCategory());
} else {
HighSchoolStudent highSchoolStudent = highSchoolStudentService.getHighSchoolStudentByMemberId(memberId);
highSchoolStudent.updateGrade(request.grade());
}
return ProfileResponse.from(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ public HighSchoolStudent(String email, String password, String nickname, String
this.highSchoolStudentId = highSchoolStudentId;
}

public void updateGrade(Grade grade) {
this.highSchoolGrade = grade;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import com.gdyd.gdydcore.domain.common.BaseTimeEntity;
import com.gdyd.gdydcore.domain.report.Report;
import jakarta.persistence.*;
import lombok.*;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

import java.util.ArrayList;
Expand Down Expand Up @@ -42,6 +45,9 @@ public class Member extends BaseTimeEntity {
@Column(nullable = false)
String name;

@Column(nullable = false)
String profileImage;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "member", orphanRemoval = true)
List<Post> posts = new ArrayList<>();

Expand All @@ -63,17 +69,18 @@ public Member(MemberType type, String email, String password, String nickname, S
this.password = password;
this.nickname = nickname;
this.name = name;
this.profileImage = "DEFAULT";
}

public void updatePassword(String password) {
this.password = password;
}

public void updateEmail(String email) {
this.email = email;
}

public void updateNickname(String nickname) {
this.nickname = nickname;
}

public void updateProfileImage(String profileImage) {
this.profileImage = profileImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public UniversityStudent(
this.universityMajor = universityMajor;
this.universityStudentId = universityStudentId;
}

public void updateGrade(Grade grade) {
this.universityGrade = grade;
}

public void updateUniversityMajorCategory(UniversityMajorCategory universityMajorCategory) {
this.universityMajorCategory = universityMajorCategory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ErrorCode {
NOT_FOUND_MEMBER(HttpStatus.NOT_FOUND, "NOT_FOUND_MEMBER", "해당 회원을 찾을 수 없습니다."),
INVALID_MEMBER_TYPE(HttpStatus.BAD_REQUEST, "INVALID_MEMBER_TYPE", "유효하지 않은 회원 타입입니다."),
INVALID_MEMBER_REQUEST(HttpStatus.BAD_REQUEST, "INVALID_MEMBER_REQUEST", "해당 회원은 요청할 수 없습니다."),
INVALID_NICKNAME(HttpStatus.BAD_REQUEST, "INVALID_NICKNAME", "이미 사용중인 닉네임입니다."),

// LIKELIST
ALREADY_LIKED(HttpStatus.BAD_REQUEST, "ALREADY_LIKED", "이미 좋아요를 누른 글입니다."),
Expand Down
Loading