Skip to content
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

Feat : 회원 엔티티 추가 및 회원조회기능 추가 #8

Merged
merged 6 commits into from
Jul 23, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/


2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package umc.kkijuk.server.member.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import umc.kkijuk.server.member.service.MemberService;

@Controller
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/main/java/umc/kkujuk/server/common/converter에 동일한 동작을 하는 StringListToStringConverter가 존재하니 혹여나 동일한 동작을 한다면 수정해주세요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 확인 못하고 만든거였는데 확인 했습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package umc.kkijuk.server.member.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.List;

@Converter
public class StringListConverter implements AttributeConverter<List<String>, String> {

private final ObjectMapper mapper = new ObjectMapper();


@Override
public String convertToDatabaseColumn(List<String> entityData) {
try {
return mapper.writeValueAsString(entityData);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Override
public List<String> convertToEntityAttribute(String dbData) {
try {
return mapper.readValue(dbData, List.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/umc/kkijuk/server/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package umc.kkijuk.server.member.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import umc.kkijuk.server.member.converter.StringListConverter;

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;

@Entity
@Builder
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class Member {

@Id @GeneratedValue
@Column(name = "member_id")
private Long id;

@NotNull
private String email;

@NotNull
private String name;

@NotNull
private String phoneNumber;

@NotNull
private LocalDate birthDate;

@NotNull
private String password;

@Convert(converter = StringListConverter.class)
private List<String> field = new ArrayList<>();

@NotNull
private Boolean marketingAgree;

@NotNull
@Enumerated(EnumType.STRING)
private State userState;

public Member(String email, String name, String phoneNumber, LocalDate birthDate, String password, Boolean marketingAgree, State userState) {
this.email = email;
this.name = name;
this.phoneNumber = phoneNumber;
this.birthDate = birthDate;
this.password = password;
this.marketingAgree = marketingAgree;
this.userState = userState;
}
}

6 changes: 6 additions & 0 deletions src/main/java/umc/kkijuk/server/member/domain/State.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package umc.kkijuk.server.member.domain;

public enum State {
ACTIVATE ,
INACTIVATE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package umc.kkijuk.server.member.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.kkijuk.server.member.domain.Member;

import java.util.Optional;

public interface MemberJpaRepository extends JpaRepository<Member, Long>{
Optional<Member> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package umc.kkijuk.server.member.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.repository.MemberJpaRepository;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.infrastructure.RecruitEntity;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberService {
private final MemberJpaRepository memberJpaRepository;

public Member findOne(Long memberId) {
return memberJpaRepository.findById(memberId).get();
}


//아직 인터페이스 구현 x, 추후에 구현 후 MemberService -> MemberServiceImpl로 수정 예정.
public Long join(Member member) {
memberJpaRepository.save(member);
return member.getId();
}

}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ spring.application.name=server
# spring data source
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.username=test
spring.datasource.password=testPW

# spring jpa
spring.jpa.database=mysql
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package umc.kkijuk.server.member.service;


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.State;
import umc.kkijuk.server.member.repository.MemberJpaRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;


@SpringBootTest
@Transactional
public class MemberServiceTest {

@Autowired MemberService memberService;
@Autowired
MemberJpaRepository memberJpaRepository;

@Test
public void 유저정보db저장및조회() throws Exception {
//given
Member member1 = new Member("[email protected]", "홍길동", "010-7444-1768", LocalDate.now(), "passwordTest", Boolean.TRUE, State.ACTIVATE);
//when
Long savedId = memberService.join(member1);
Optional<Member> member2 = memberJpaRepository.findById(savedId);
//then
assertEquals(member1, member2.get());
}
}