Skip to content

Commit

Permalink
Merge pull request #4 from giwoong01/feature/refactor
Browse files Browse the repository at this point in the history
코드 리펙토링
  • Loading branch information
giwoong01 authored Jul 16, 2023
2 parents e9c97d7 + c4354dd commit de7a967
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.member.domain.repository.MemberRepository;
import com.itcontest.skhuming.notice.domain.Notice;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,11 +12,13 @@

@Service
@Transactional
@RequiredArgsConstructor
public class MemberService {

private final MemberRepository memberRepository;

public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public void memberSave(Member member) {
memberRepository.save(member);
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/com/itcontest/skhuming/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.itcontest.skhuming.member.domain;

import com.itcontest.skhuming.member.api.dto.request.MemberSaveReqDto;
import com.itcontest.skhuming.notice.domain.Notice;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member {

@Id
Expand Down Expand Up @@ -41,27 +36,22 @@ public class Member {
@OneToMany(mappedBy = "member")
private List<Notice> myScrap = new ArrayList<>();

@Builder
private Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber) {
protected Member() {
}

private Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber, int score, Tear tear) {
this.email = email;
this.pwd = pwd;
this.nickname = nickname;
this.memberName = memberName;
this.department = department;
this.studentNumber = studentNumber;
this.score = 0;
this.tear = Tear.Un;
this.score = score;
this.tear = tear;
}

public static Member createMember(MemberSaveReqDto memberSaveReqDto) {
return Member.builder()
.email(memberSaveReqDto.getEmail())
.pwd(memberSaveReqDto.getPwd())
.nickname(memberSaveReqDto.getNickname())
.memberName(memberSaveReqDto.getMemberName())
.department(memberSaveReqDto.getDepartment())
.studentNumber(memberSaveReqDto.getStudentNumber())
.build();
public Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber) {
this(email, pwd, nickname, memberName, department, studentNumber, 0, Tear.Un);
}

// 비즈니스로직
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import com.itcontest.skhuming.member.domain.repository.MemberRepository;
import com.itcontest.skhuming.notice.domain.Notice;
import com.itcontest.skhuming.notice.domain.repository.NoticeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class NoticeService {

private final NoticeRepository noticeRepository;
private final MemberRepository memberRepository;

public NoticeService(NoticeRepository noticeRepository, MemberRepository memberRepository) {
this.noticeRepository = noticeRepository;
this.memberRepository = memberRepository;
}


public void noticeSave(Notice notice) {
noticeRepository.save(notice);
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/com/itcontest/skhuming/notice/domain/Notice.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.itcontest.skhuming.notice.domain;

import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.notice.api.dto.request.NoticeSaveReqDto;
import lombok.*;

import javax.persistence.*;

@Entity
@Table(name = "Notices")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Notice {

@Id
Expand All @@ -33,27 +31,17 @@ public class Notice {
@JoinColumn(name = "member_id")
private Member member;

protected Notice() {

@Builder
private Notice(String title, String schedule, String contents, int mileageScore, String img, int scrap) {
}

public Notice(String title, String schedule, String contents, int mileageScore, String img, int scrap) {
this.title = title;
this.schedule = schedule;
this.contents = contents;
this.mileageScore = mileageScore;
this.img = img;
this.scrap = scrap;
}

public static Notice createNotice(NoticeSaveReqDto noticeSaveReqDto) {
return Notice.builder()
.title(noticeSaveReqDto.getTitle())
.schedule(noticeSaveReqDto.getSchedule())
.contents(noticeSaveReqDto.getContents())
.mileageScore(noticeSaveReqDto.getMileageScore())
.img(noticeSaveReqDto.getImg())
.scrap(noticeSaveReqDto.getScrap())
.build();
}

}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.itcontest.skhuming.member.application;

import com.itcontest.skhuming.member.api.dto.request.MemberSaveReqDto;
import com.itcontest.skhuming.member.domain.Member;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -21,17 +20,11 @@ class MemberServiceTest {
@Test
void mainPageRanking() {
// 데이터 저장
MemberSaveReqDto member1 = new MemberSaveReqDto("member1", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
MemberSaveReqDto member2 = new MemberSaveReqDto("member2", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
MemberSaveReqDto member3 = new MemberSaveReqDto("member3", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
MemberSaveReqDto member4 = new MemberSaveReqDto("member4", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
MemberSaveReqDto member5 = new MemberSaveReqDto("member5", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");

Member memberA = Member.createMember(member1);
Member memberB = Member.createMember(member2);
Member memberC = Member.createMember(member3);
Member memberD = Member.createMember(member4);
Member memberE = Member.createMember(member5);
Member memberA = new Member("member1", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
Member memberB = new Member("member2", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
Member memberC = new Member("member3", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
Member memberD = new Member("member4", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
Member memberE = new Member("member5", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
memberA.plusMyScore(10);
memberB.plusMyScore(80);
memberC.plusMyScore(40);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.itcontest.skhuming.notice.application;

import com.itcontest.skhuming.member.api.dto.request.MemberSaveReqDto;
import com.itcontest.skhuming.member.application.MemberService;
import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.notice.api.dto.request.NoticeSaveReqDto;
import com.itcontest.skhuming.notice.domain.Notice;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -27,11 +25,8 @@ class NoticeServiceTest {
@DisplayName("공지리스트 출력")
@Test
void noticeListPrint() {
NoticeSaveReqDto noticeSaveReqDto = new NoticeSaveReqDto("title", "schedule", "contents", 20, "img", 0);
NoticeSaveReqDto noticeSaveReqDto1 = new NoticeSaveReqDto("title1", "schedule1", "contents1", 30, "img1", 0);

Notice notice = Notice.createNotice(noticeSaveReqDto);
Notice notice1 = Notice.createNotice(noticeSaveReqDto1);
Notice notice = new Notice("title", "schedule", "contents", 20, "img", 0);
Notice notice1 = new Notice("title1", "schedule1", "contents1", 30, "img1", 0);

// // when
// noticeService.noticeSave(notice);
Expand All @@ -51,23 +46,16 @@ void noticeListPrint() {
@Test
void noticeScarp() {
// controller에서 매개변수로 요청을 받아서
NoticeSaveReqDto noticeSaveReqDto = new NoticeSaveReqDto("title", "schedule", "contents", 20, "img", 0);
NoticeSaveReqDto noticeSaveReqDto1 = new NoticeSaveReqDto("title1", "schedule1", "contents1", 30, "img1", 0);

// 컨트롤러에서 공지 만들어서
Notice notice = Notice.createNotice(noticeSaveReqDto);
Notice notice1 = Notice.createNotice(noticeSaveReqDto1);
Notice notice = new Notice("title", "schedule", "contents", 20, "img", 0);
Notice notice1 = new Notice("title1", "schedule1", "contents1", 30, "img1", 0);

// 컨트롤러에서 서비스로 넘김
noticeService.noticeSave(notice);
noticeService.noticeSave(notice1);

// 이 위가 모두 컨트롤러에서 일어나는 일

MemberSaveReqDto memberSaveReqDto = new MemberSaveReqDto(".com", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");
MemberSaveReqDto memberSaveReqDto1 = new MemberSaveReqDto(".com", "chlrldnd","몽지우", "명지우", "소프트웨어공학과", " 202014098");
Member member = Member.createMember(memberSaveReqDto);
Member member1 = Member.createMember(memberSaveReqDto1);
Member member = new Member(".com", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098");

memberService.memberSave(member);
System.out.println("member.getMemberId() = " + member.getMemberId());
Expand Down

0 comments on commit de7a967

Please sign in to comment.