Skip to content

Commit

Permalink
Merge pull request #99 from 9oormthon-univ/fix/#97
Browse files Browse the repository at this point in the history
feat: Quest 연관관계 수정
  • Loading branch information
koosco authored Nov 23, 2024
2 parents c610cbc + d0c571f commit efd725f
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 46 deletions.
3 changes: 2 additions & 1 deletion sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CREATE TABLE interest_job
CREATE TABLE resume
(
resume_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
category VARCHAR(10) NOT NULL,
content VARCHAR(50) NOT NULL,
start_date DATE,
Expand Down Expand Up @@ -86,7 +87,7 @@ CREATE TABLE quest
sequence INT NOT NULL,
member_id BIGINT NOT NULL,
goal_id BIGINT NOT NULL,
CONSTRAINT quest_member FOREIGN KEY (member_id, goal_id) REFERENCES member_goal (member_id, goal_id)
CONSTRAINT fk_quest_member_goal FOREIGN KEY (member_id, goal_id) REFERENCES member_goal (member_id, goal_id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/groom/orbit/goal/app/MemberGoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ public List<GetCompletedGoalResponseDto> findCompletedGoals(Long memberId) {
List<MemberGoal> memberGoals = memberGoalRepository.findByIsComplete(memberId, true);
return memberGoals.stream()
.map(
memberGoal -> {
List<Quest> quests =
questQueryService.findQuestsByMemberAndGoal(memberId, memberGoal.getGoalId());
return new GetCompletedGoalResponseDto(
memberGoal.getTitle(), quests.stream().map(Quest::getTitle).toList());
})
memberGoal ->
new GetCompletedGoalResponseDto(
memberGoal.getTitle(),
memberGoal.getQuests().stream().map(Quest::getTitle).toList()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public List<Quest> findByQuestIdIn(List<Long> questIds) {
}

public long getTotalQuestCount(Long goalId) {
return questRepository.countByGoal_GoalId(goalId);
return questRepository.countByMemberGoal_GoalId(goalId);
}

public long getFinishQuestCount(Long goalId) {
return questRepository.countCompletedByGoal_GoalId(goalId);
return questRepository.countCompletedByMemberGoal_GoalId(goalId);
}
}
19 changes: 11 additions & 8 deletions src/main/java/com/groom/orbit/goal/dao/QuestRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ public interface QuestRepository extends JpaRepository<Quest, Long> {

@Query(
"select q from Quest q"
+ " join fetch q.member m"
+ " join fetch q.goal g"
+ " where q.member.id=:member_id and q.goal.goalId=:goal_id")
+ " join fetch q.memberGoal mg"
+ " where mg.memberId=:member_id and mg.goalId=:goal_id")
List<Quest> findByMemberIdAndGoalId(
@Param("member_id") Long memberId, @Param("goal_id") Long goalId);

@Query("select count(*) from Quest q" + " join q.goal g" + " where g.goalId=:goal_id")
@Query("select count(*) from Quest q" + " join q.memberGoal mg" + " where mg.goalId=:goal_id")
int getCountByGoalId(@Param("goal_id") Long goalId);

List<Quest> findByQuestIdIn(List<Long> ids);

long countByGoal_GoalId(Long goalId);
@Query("select count(*) from Quest q" + " join q.memberGoal mg" + " where mg.goalId=:goal_id")
long countByMemberGoal_GoalId(@Param("goal_id") Long goalId);

@Query("select count(*) from Quest q" + " where q.goal.goalId=:goal_id and q.isComplete=true")
long countCompletedByGoal_GoalId(@Param("goal_id") Long goalId);
@Query(
"select count(*) from Quest q"
+ " join q.memberGoal mg"
+ " where q.isComplete=true and mg.goalId=:goal_id")
long countCompletedByMemberGoal_GoalId(@Param("goal_id") Long goalId);

@Query(
"SELECT q FROM Quest q WHERE MONTH(q.deadline) = :month AND q.member.id = :memberId ORDER BY q.deadline ASC")
"SELECT q FROM Quest q join fetch q.memberGoal mg WHERE MONTH(q.deadline) = :month AND mg.memberId = :memberId ORDER BY q.deadline ASC")
List<Quest> findAllByMonthAndMemberId(Long memberId, Integer month);
}
8 changes: 8 additions & 0 deletions src/main/java/com/groom/orbit/goal/dao/entity/MemberGoal.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.groom.orbit.goal.dao.entity;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import org.hibernate.annotations.ColumnDefault;
Expand Down Expand Up @@ -41,6 +46,9 @@ public class MemberGoal {
@Column(nullable = false)
private Boolean isComplete;

@OneToMany(mappedBy = "memberGoal", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Quest> quests = new ArrayList<>();

public static MemberGoal create(Member member, Goal goal) {
MemberGoal memberGoal = new MemberGoal();
memberGoal.member = member;
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/com/groom/orbit/goal/dao/entity/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

Expand All @@ -17,7 +18,6 @@
import com.groom.orbit.common.dao.entity.BaseTimeEntity;
import com.groom.orbit.common.exception.CommonException;
import com.groom.orbit.common.exception.ErrorCode;
import com.groom.orbit.member.dao.jpa.entity.Member;

import lombok.Getter;

Expand All @@ -44,19 +44,17 @@ public class Quest extends BaseTimeEntity {
private Integer sequence;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "goal_id", nullable = false)
private Goal goal;
@JoinColumns({
@JoinColumn(name = "member_id", referencedColumnName = "member_id"),
@JoinColumn(name = "goal_id", referencedColumnName = "goal_id")
})
private MemberGoal memberGoal;

public static Quest create(
String title, MemberGoal memberGoal, LocalDate deadline, int newSequence) {
Quest quest = new Quest();
quest.title = title;
quest.member = memberGoal.getMember();
quest.goal = memberGoal.getGoal();
quest.memberGoal = memberGoal;
quest.sequence = newSequence;
quest.deadline = deadline;

Expand All @@ -71,7 +69,7 @@ public void decreaseSequence() {
}

public void validateMember(Long memberId) {
this.member.validateId(memberId);
this.memberGoal.getMemberId().equals(memberId);
}

public int compareWithId(Long questId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import org.hibernate.annotations.ColumnDefault;

import com.groom.orbit.common.exception.CommonException;
import com.groom.orbit.common.exception.ErrorCode;
import com.groom.orbit.job.dao.jpa.entity.InterestJob;
import com.groom.orbit.job.dao.jpa.entity.Job;
import com.groom.orbit.member.app.dto.request.UpdateMemberRequestDto;
Expand Down Expand Up @@ -79,10 +77,4 @@ public void updateMember(UpdateMemberRequestDto requestDto, String newProfileUrl
this.isNotification = requestDto.isNotification();
this.isProfile = requestDto.isProfile();
}

public void validateId(Long id) {
if (!this.id.equals(id)) {
throw new CommonException(ErrorCode.ACCESS_DENIED);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.groom.orbit.resume.app.dto;

import java.util.Date;
import java.time.LocalDate;

import com.groom.orbit.member.dao.jpa.entity.Member;
import com.groom.orbit.resume.dao.entity.Resume;
import com.groom.orbit.resume.dao.entity.ResumeCategory;

public record ResumeRequestDto(
ResumeCategory resumeCategory, String title, String content, Date startDate, Date endDate) {
ResumeCategory resumeCategory,
String title,
String content,
LocalDate startDate,
LocalDate endDate) {

public Resume toResume(Member member) {
return Resume.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.groom.orbit.resume.app.dto;

import java.util.Date;
import java.time.LocalDate;

import com.groom.orbit.resume.dao.entity.Resume;
import com.groom.orbit.resume.dao.entity.ResumeCategory;
Expand All @@ -9,7 +9,11 @@

@Builder
public record ResumeResponseDto(
ResumeCategory resumeCategory, String title, String content, Date startDate, Date endDate) {
ResumeCategory resumeCategory,
String title,
String content,
LocalDate startDate,
LocalDate endDate) {

public static ResumeResponseDto toResumeResponseDto(Resume resume) {
return ResumeResponseDto.builder()
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/com/groom/orbit/resume/dao/entity/Resume.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.groom.orbit.resume.dao.entity;

import java.util.Date;
import java.time.LocalDate;

import jakarta.persistence.*;
import jakarta.persistence.Column;
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 jakarta.persistence.Table;

import com.groom.orbit.member.dao.jpa.entity.Member;
import com.groom.orbit.resume.app.dto.ResumeRequestDto;
Expand All @@ -25,21 +35,21 @@ public class Resume {
@Column(name = "resume_id")
private Long resumeId;

@Column(name = "category")
@Column(name = "category", length = 10)
@Enumerated(EnumType.STRING)
private ResumeCategory resumeCategory;

@Column(name = "title")
@Column(name = "title", length = 50)
private String title;

@Column(name = "content")
@Column(name = "content", length = 50)
private String content;

@Column(name = "start_date")
private Date startDate;
private LocalDate startDate;

@Column(name = "end_date")
private Date endDate;
private LocalDate endDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
Expand Down

0 comments on commit efd725f

Please sign in to comment.