diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/CourseEntity.java b/src/main/java/com/m9d/sroom/common/entity/jpa/CourseEntity.java index a7698b03..285b2365 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/CourseEntity.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/CourseEntity.java @@ -1,6 +1,7 @@ package com.m9d.sroom.common.entity.jpa; import com.m9d.sroom.common.entity.jpa.embedded.Scheduling; +import lombok.Getter; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; @@ -12,6 +13,7 @@ @Entity @Table(name = "COURSE") +@Getter public class CourseEntity { @Id diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/MemberEntity.java b/src/main/java/com/m9d/sroom/common/entity/jpa/MemberEntity.java index b9b2f08b..fbbea207 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/MemberEntity.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/MemberEntity.java @@ -1,13 +1,21 @@ package com.m9d.sroom.common.entity.jpa; import com.m9d.sroom.common.entity.jpa.embedded.MemberStats; +import lombok.*; import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.DynamicInsert; +import org.springframework.beans.factory.annotation.Autowired; import javax.persistence.*; import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; @Entity @Table(name = "MEMBER") +@Getter +@NoArgsConstructor +@DynamicInsert public class MemberEntity { @Id @@ -29,4 +37,23 @@ public class MemberEntity { private Boolean status; private String bio; + + @OneToMany(mappedBy = "member") + private List courses = new ArrayList(); + + public Integer countCompletedCourse() { + return (int) courses.stream() + .filter(courseEntity -> courseEntity.getProgress().equals(100)) + .count(); + } + + @Builder + public MemberEntity(String memberCode, String memberName) { + this.memberCode = memberCode; + this.memberName = memberName; + } + + public void updateName(String newName) { + this.memberName = newName; + } } diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/ContentInfo.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/ContentInfo.java index 31afc0eb..d840c2d1 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/ContentInfo.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/ContentInfo.java @@ -1,10 +1,13 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; + import javax.persistence.Column; import javax.persistence.Embeddable; import java.sql.Timestamp; @Embeddable +@Data public class ContentInfo { private String title; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Feedback.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Feedback.java index 49a9c1f3..05e5fdfd 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Feedback.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Feedback.java @@ -1,8 +1,11 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; + import javax.persistence.Embeddable; @Embeddable +@Data public class Feedback { private Integer positiveFeedbackCount; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Grading.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Grading.java index dd88c503..d1825981 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Grading.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Grading.java @@ -1,5 +1,6 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; @@ -7,6 +8,7 @@ import java.sql.Timestamp; @Embeddable +@Data public class Grading { private String submittedAnswer; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/LearningStatus.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/LearningStatus.java index 2d38b820..ab1bd0e6 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/LearningStatus.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/LearningStatus.java @@ -1,5 +1,6 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; import lombok.Setter; import org.hibernate.annotations.Type; import org.hibernate.annotations.UpdateTimestamp; @@ -8,6 +9,7 @@ import java.sql.Timestamp; @Embeddable +@Data public class LearningStatus { private Integer startTime; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/MemberStats.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/MemberStats.java index b39afbb7..65a1d583 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/MemberStats.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/MemberStats.java @@ -1,8 +1,11 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; + import javax.persistence.Embeddable; @Embeddable +@Data public class MemberStats { private Integer totalSolvedCount; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Review.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Review.java index bb179943..f1f40058 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Review.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Review.java @@ -1,8 +1,11 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; + import javax.persistence.Embeddable; @Embeddable +@Data public class Review { private Integer accumulatedRating; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Scheduling.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Scheduling.java index 775caa43..8a187d7e 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Scheduling.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Scheduling.java @@ -1,5 +1,6 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; import org.hibernate.annotations.CreationTimestamp; import javax.persistence.Embeddable; @@ -8,6 +9,7 @@ import java.util.Date; @Embeddable +@Data public class Scheduling { private Boolean isScheduled; diff --git a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Sequence.java b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Sequence.java index 96fbe28a..cab509c9 100644 --- a/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Sequence.java +++ b/src/main/java/com/m9d/sroom/common/entity/jpa/embedded/Sequence.java @@ -1,8 +1,11 @@ package com.m9d.sroom.common.entity.jpa.embedded; +import lombok.Data; + import javax.persistence.Embeddable; @Embeddable +@Data public class Sequence { private Integer section; diff --git a/src/main/java/com/m9d/sroom/common/repository/member/MemberJpaRepositoryImpl.java b/src/main/java/com/m9d/sroom/common/repository/member/MemberJpaRepositoryImpl.java new file mode 100644 index 00000000..96964d6c --- /dev/null +++ b/src/main/java/com/m9d/sroom/common/repository/member/MemberJpaRepositoryImpl.java @@ -0,0 +1,40 @@ +package com.m9d.sroom.common.repository.member; + +import com.m9d.sroom.common.entity.jpa.MemberEntity; +import org.springframework.stereotype.Repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.Optional; + +@Repository +public class MemberJpaRepositoryImpl { + + @PersistenceContext + EntityManager em; + + public MemberEntity save(MemberEntity member) { + em.persist(member); + return member; + } + + public MemberEntity getById(Long memberId) { + return em.find(MemberEntity.class, memberId); + } + + public MemberEntity getByCode(String memberCode) { + return em.createQuery("select m from MemberEntity m where m.memberCode = :memberCode", + MemberEntity.class) + .setParameter("memberCode", memberCode) + .getSingleResult(); + } + + public Optional findById(Long memberId) { + return Optional.ofNullable(getById(memberId)); + } + + public Optional findByCode(String memberCode) { + return Optional.ofNullable(getByCode(memberCode)); + } + +} diff --git a/src/test/java/com/m9d/sroom/member/MemberRepositoryTest.java b/src/test/java/com/m9d/sroom/member/MemberRepositoryTest.java new file mode 100644 index 00000000..7b460e6b --- /dev/null +++ b/src/test/java/com/m9d/sroom/member/MemberRepositoryTest.java @@ -0,0 +1,67 @@ +package com.m9d.sroom.member; + +import com.m9d.sroom.common.entity.jpa.MemberEntity; +import com.m9d.sroom.common.repository.member.MemberJpaRepositoryImpl; +import com.m9d.sroom.util.SroomTest; +import com.m9d.sroom.util.TestConstant; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Optional; + +public class MemberRepositoryTest extends SroomTest { + + @Autowired + private MemberJpaRepositoryImpl memberJpaRepository; + + @Test + @DisplayName("저장되지 않은 member는 Optional.isEmpty() = true 입니다.") + void nullIfNotSavedMember() { + //given + Optional memberEntityOptional = memberJpaRepository.findById(1L); + + //when + + + //then + Assertions.assertEquals(Optional.empty(), memberEntityOptional); + } + + @Test + @DisplayName("저장된 member는 조회에 성공합니다.") + void getMemberIfSaved() { + MemberEntity memberEntity = MemberEntity.builder() + .memberCode(TestConstant.MEMBER_CODE) + .memberName(TestConstant.MEMBER_PROFILE) + .build(); + memberJpaRepository.save(memberEntity); + + //when + Optional memberEntityOptional = memberJpaRepository.findById(1L); + + //then + Assertions.assertNotEquals(memberEntityOptional, Optional.empty()); + Assertions.assertEquals(memberEntityOptional.get().getMemberName(), TestConstant.MEMBER_PROFILE); + } + + @Test + @DisplayName("jpa 사용하면 멤버 이름을 수정하기만 해도 db 에 반영됩니다.") + void updateMemberName() { + //given + MemberEntity memberEntity = MemberEntity.builder() + .memberCode(TestConstant.MEMBER_CODE) + .memberName(TestConstant.MEMBER_PROFILE) + .build(); + memberJpaRepository.save(memberEntity); + + //when + String newName = "수정될 이름임 ㅇㅇ"; + memberEntity.updateName(newName); + + //then + MemberEntity member = memberJpaRepository.getById(1L); + Assertions.assertEquals(member.getMemberName(), newName); + } +} diff --git a/src/test/java/com/m9d/sroom/util/TestConstant.java b/src/test/java/com/m9d/sroom/util/TestConstant.java index c7ad8582..670920f1 100644 --- a/src/test/java/com/m9d/sroom/util/TestConstant.java +++ b/src/test/java/com/m9d/sroom/util/TestConstant.java @@ -7,6 +7,7 @@ public class TestConstant { public static final String MEMBER_PROFILE = "멤버닉네임"; + public static final String MEMBER_CODE = "106400356559989163494"; public static final String PLAYLIST_CHANNEL = "정보처리기사 전문가 손경식"; public static final String PLAYLIST_DESCRIPTION = "잠실을 거쳐 선릉 소마센터까지"; public static final String THUMBNAIL = "https://i.ytimg.com/vi/Pc6n6HgWU5c/mqdefault.jpg"; @@ -15,4 +16,6 @@ public class TestConstant { public static final Timestamp PUBLISHED_AT = new Timestamp(System.currentTimeMillis() - 10 * 60 * 60 * 1000); public static final String LANGUAGE_KO = "KO"; public static final String LICENSE_YOUTUBE = "youtube"; + + public static final String MEMBER_REFRESH_TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzIiwiaWF0IjoxNzAyMzQzNDAyLCJleHAiOjE3MDI2MDI2MDIsInByb2ZpbGUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BQ2c4b2NJN2dJTElxazFZRHdFS2lsOXA3SmZ5VU5IYXF5S0xpbEdtWTNBUFpVeUc9czk2LWMifQ.mfsJ3lRXjTjaz_VseAr9xRvbZmjTWbioKUpnkhdcohY"; } diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 08a9205e..1a9ace04 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -19,6 +19,15 @@ spring: url: jdbc:h2:mem:test;MODE=MySQL username: sa password: + jpa: + hibernate: + ddl-auto: none + properties: + hibernate: + database-platform: org.hibernate.dialect.MariaDB1011Dialect + show_sql: true # sql 쿼리를 보여줍니다. + format_sql: true # sql query formatting + use_sql_comments: true # sql 쿼리의 추가정보를 보여줍니다. youtube: base-url: https://www.googleapis.com/youtube/v3