Skip to content

Commit

Permalink
[SWM-412] Feat : develop member jpa repository impl
Browse files Browse the repository at this point in the history
  • Loading branch information
D-w-nJ committed Dec 16, 2023
1 parent 1036cb0 commit f396cce
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,6 +13,7 @@

@Entity
@Table(name = "COURSE")
@Getter
public class CourseEntity {

@Id
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/m9d/sroom/common/entity/jpa/MemberEntity.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,4 +37,23 @@ public class MemberEntity {
private Boolean status;

private String bio;

@OneToMany(mappedBy = "member")
private List<CourseEntity> courses = new ArrayList<CourseEntity>();

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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.m9d.sroom.common.entity.jpa.embedded;

import lombok.Data;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Embeddable;
import java.sql.Timestamp;

@Embeddable
@Data
public class Grading {

private String submittedAnswer;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +9,7 @@
import java.sql.Timestamp;

@Embeddable
@Data
public class LearningStatus {

private Integer startTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.m9d.sroom.common.entity.jpa.embedded;

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Embeddable;
Expand All @@ -8,6 +9,7 @@
import java.util.Date;

@Embeddable
@Data
public class Scheduling {

private Boolean isScheduled;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MemberEntity> findById(Long memberId) {
return Optional.ofNullable(getById(memberId));
}

public Optional<MemberEntity> findByCode(String memberCode) {
return Optional.ofNullable(getByCode(memberCode));
}

}
67 changes: 67 additions & 0 deletions src/test/java/com/m9d/sroom/member/MemberRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -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<MemberEntity> 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<MemberEntity> 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);
}
}
3 changes: 3 additions & 0 deletions src/test/java/com/m9d/sroom/util/TestConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
}
9 changes: 9 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f396cce

Please sign in to comment.