From 3091906407a1b4ef4287e4dd5db3d79af620961a Mon Sep 17 00:00:00 2001 From: sehui-ing <116075689+sehui-ing@users.noreply.github.com> Date: Sun, 26 May 2024 23:39:53 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20level10=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/backend/likelion/todos/goal/Goal.java | 10 ++++++++++ .../backend/likelion/todos/goal/GoalRepository.java | 11 ++++------- .../java/backend/likelion/todos/member/Member.java | 8 ++++++++ .../likelion/todos/member/MemberRepository.java | 11 ++++------- src/main/java/backend/likelion/todos/todo/Todo.java | 12 +++++++++++- .../backend/likelion/todos/todo/TodoRepository.java | 2 +- 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/main/java/backend/likelion/todos/goal/Goal.java b/src/main/java/backend/likelion/todos/goal/Goal.java index 178d14d..1f05f49 100644 --- a/src/main/java/backend/likelion/todos/goal/Goal.java +++ b/src/main/java/backend/likelion/todos/goal/Goal.java @@ -2,20 +2,30 @@ import backend.likelion.todos.common.ForbiddenException; import backend.likelion.todos.member.Member; +import jakarta.persistence.*; +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; @Getter // TODO [10 단계] : 롬봉을 통해 기본 생성자를 PROTECTED 접근 제한자로 생성하세요. +@NoArgsConstructor(access = AccessLevel.PROTECTED) + // TODO [10 단계] : Goal 객체를 Entity 로 정의하세요. +@Entity public class Goal { // TODO [10 단계] : id를 PK, Auto Increment로 설정하세요. + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String color; // TODO [10 단계] : goal 과 member와의 관계를 설정합니다. (join 하는 컬럼명은 member_id로 설정합니다.), 지연 로딩을 사용합니다. + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") private Member member; public Goal(String name, String color, Member member) { diff --git a/src/main/java/backend/likelion/todos/goal/GoalRepository.java b/src/main/java/backend/likelion/todos/goal/GoalRepository.java index 9d9ae11..dd26340 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalRepository.java +++ b/src/main/java/backend/likelion/todos/goal/GoalRepository.java @@ -1,17 +1,14 @@ package backend.likelion.todos.goal; import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface GoalRepository { +public interface GoalRepository extends JpaRepository { // TODO [10단계] JpaRepository를 상속받습니다. // TODO [10단계] 아래는 기존 코드입니다. 컴파일 오류가 발생하고 있는데, 이를 해결하세요. - public List findAllByMemberId(Long memberId) { - return goals.values() - .stream() - .filter(it -> it.getMember().getId().equals(memberId)) - .toList(); - } + List findAllByMemberId(Long memberId); } diff --git a/src/main/java/backend/likelion/todos/member/Member.java b/src/main/java/backend/likelion/todos/member/Member.java index 1b7ca66..eb8612d 100644 --- a/src/main/java/backend/likelion/todos/member/Member.java +++ b/src/main/java/backend/likelion/todos/member/Member.java @@ -1,17 +1,25 @@ package backend.likelion.todos.member; import backend.likelion.todos.common.UnAuthorizedException; +import jakarta.persistence.*; +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; @Getter // TODO [10 단계] : 롬봉을 통해 기본 생성자를 PROTECTED 접근 제한자로 생성하세요. // TODO [10 단계] : Member 객체를 Entity 로 정의하세요. +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Member { // TODO [10 단계] : id를 PK, Auto Increment로 설정하세요. + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // TODO [10 단계] : 아이디는 unique 제약 조건이 필요합니다. + @Column(unique = true) private String username; private String password; diff --git a/src/main/java/backend/likelion/todos/member/MemberRepository.java b/src/main/java/backend/likelion/todos/member/MemberRepository.java index fc49807..ab6524b 100644 --- a/src/main/java/backend/likelion/todos/member/MemberRepository.java +++ b/src/main/java/backend/likelion/todos/member/MemberRepository.java @@ -1,17 +1,14 @@ package backend.likelion.todos.member; import java.util.Optional; + +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface MemberRepository { +public interface MemberRepository extends JpaRepository { // TODO [10단계] JpaRepository를 상속받습니다. // TODO [10단계] 아래는 기존 코드입니다. 컴파일 오류가 발생하고 있는데, 이를 해결하세요. - public Optional findByUsername(String username) { - return members.values() - .stream() - .filter(it -> it.getUsername().equals(username)) - .findAny(); - } + Optional findByUserName(String username); } diff --git a/src/main/java/backend/likelion/todos/todo/Todo.java b/src/main/java/backend/likelion/todos/todo/Todo.java index 672f39e..79e2130 100644 --- a/src/main/java/backend/likelion/todos/todo/Todo.java +++ b/src/main/java/backend/likelion/todos/todo/Todo.java @@ -3,22 +3,32 @@ import backend.likelion.todos.common.ForbiddenException; import backend.likelion.todos.goal.Goal; import backend.likelion.todos.member.Member; -import jakarta.persistence.Entity; +import jakarta.persistence.*; + import java.time.LocalDate; + +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; @Getter // TODO [10 단계] : 롬봉을 통해 기본 생성자를 PROTECTED 접근 제한자로 생성하세요. // TODO [10 단계] : Todo 객체를 Entity 로 정의하세요. +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Todo { // TODO [10 단계] : id를 PK, Auto Increment로 설정하세요. + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String content; private LocalDate date; // TODO [10 단계] : todo 와 goal 과의 관계를 설정합니다. (join 하는 컬럼명은 goal_id로 설정합니다.), 지연 로딩을 사용합니다. + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "goal_id") private Goal goal; private boolean isCompleted; diff --git a/src/main/java/backend/likelion/todos/todo/TodoRepository.java b/src/main/java/backend/likelion/todos/todo/TodoRepository.java index 7609d41..cd25ad0 100644 --- a/src/main/java/backend/likelion/todos/todo/TodoRepository.java +++ b/src/main/java/backend/likelion/todos/todo/TodoRepository.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository; @Repository -public interface TodoRepository { +public interface TodoRepository extends JpaRepository { // TODO [10단계] JpaRepository를 상속받습니다. /**