diff --git a/src/main/java/com/itcontest/skhuming/member/api/MemberController.java b/src/main/java/com/itcontest/skhuming/member/api/MemberController.java new file mode 100644 index 0000000..7159a4f --- /dev/null +++ b/src/main/java/com/itcontest/skhuming/member/api/MemberController.java @@ -0,0 +1,29 @@ +package com.itcontest.skhuming.member.api; + +import com.itcontest.skhuming.member.application.MemberService; +import com.itcontest.skhuming.member.domain.Member; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; + +@Controller +public class MemberController { + + private final MemberService memberService; + + public MemberController(MemberService memberService) { + this.memberService = memberService; + } + + /** + * mainPage 1~3등 랭킹 리스트 + */ + @GetMapping("/api/main") + public ResponseEntity> mainRankingList() { + return new ResponseEntity<>(memberService.mainPageRanking(), HttpStatus.OK); + } + +} diff --git a/src/main/java/com/itcontest/skhuming/member/application/MemberService.java b/src/main/java/com/itcontest/skhuming/member/application/MemberService.java index d32f76e..fc93499 100644 --- a/src/main/java/com/itcontest/skhuming/member/application/MemberService.java +++ b/src/main/java/com/itcontest/skhuming/member/application/MemberService.java @@ -3,25 +3,51 @@ 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; +import java.util.ArrayList; import java.util.List; @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); } - // 유저본인이 스크랩한 공지 리스트 + /** + * 메인페이지의 1~3등 유저 랭킹 리스트. + * 3등까지 없더라도 리턴. + */ + public List mainPageRanking() { + List memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score")); + + List memberRankingList = new ArrayList<>(); + int count = 0; + for (Member member : memberList) { + memberRankingList.add(member); + count += 1; + + if (count == 3) { + break; + } + } + + return memberRankingList; + } + + /** + * 유저본인이 스크랩한 공지 리스트 + */ public List memberMyScrapList(Long memberId) { Member member = memberRepository.findById(memberId).get(); diff --git a/src/main/java/com/itcontest/skhuming/member/domain/Member.java b/src/main/java/com/itcontest/skhuming/member/domain/Member.java index 865d465..bcdbe34 100644 --- a/src/main/java/com/itcontest/skhuming/member/domain/Member.java +++ b/src/main/java/com/itcontest/skhuming/member/domain/Member.java @@ -1,8 +1,6 @@ package com.itcontest.skhuming.member.domain; -import com.itcontest.skhuming.member.api.dto.request.MemberSaveReqDto; import com.itcontest.skhuming.notice.domain.Notice; -import lombok.Builder; import lombok.Getter; import javax.persistence.*; @@ -14,7 +12,7 @@ public class Member { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "member_id") private Long memberId; @@ -38,31 +36,30 @@ public class Member { @OneToMany(mappedBy = "member") private List 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); } // 비즈니스로직 public void addMyScrap(Notice notice) { this.myScrap.add(notice); } + + public void plusMyScore(int score) { + this.score += score; + } } diff --git a/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java b/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java index eadf738..bebd5ab 100644 --- a/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java +++ b/src/main/java/com/itcontest/skhuming/notice/application/NoticeService.java @@ -4,7 +4,6 @@ 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; @@ -12,12 +11,16 @@ @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); diff --git a/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java b/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java index c1d4d26..a77baad 100644 --- a/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java +++ b/src/main/java/com/itcontest/skhuming/notice/domain/Notice.java @@ -1,7 +1,6 @@ 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.*; @@ -9,11 +8,10 @@ @Entity @Table(name = "Notices") @Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Notice { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "notice_id") private Long noticeId; @@ -33,9 +31,11 @@ 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; @@ -43,17 +43,5 @@ private Notice(String title, String schedule, String contents, int 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(); - } - } diff --git a/src/test/java/com/itcontest/skhuming/member/application/MemberServiceTest.java b/src/test/java/com/itcontest/skhuming/member/application/MemberServiceTest.java new file mode 100644 index 0000000..60ca7a1 --- /dev/null +++ b/src/test/java/com/itcontest/skhuming/member/application/MemberServiceTest.java @@ -0,0 +1,49 @@ +package com.itcontest.skhuming.member.application; + +import com.itcontest.skhuming.member.domain.Member; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class MemberServiceTest { + + @Autowired + MemberService memberService; + + @DisplayName("메인페이지 1~3등 랭킹 리스트") + @Test + void mainPageRanking() { + // 데이터 저장 + 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); + memberD.plusMyScore(100); + + memberService.memberSave(memberA); + memberService.memberSave(memberB); + memberService.memberSave(memberC); + memberService.memberSave(memberD); + memberService.memberSave(memberE); + + List memberList = memberService.mainPageRanking(); + + for (Member member : memberList) { + System.out.println("member.getMemberId() = " + member.getMemberId()); + System.out.println("member.getTear() = " + member.getTear()); + System.out.println("member.getNickname() = " + member.getNickname()); + System.out.println("member.getScore() = " + member.getScore()); + } + + } +} \ No newline at end of file diff --git a/src/test/java/com/itcontest/skhuming/notice/application/NoticeServiceTest.java b/src/test/java/com/itcontest/skhuming/notice/application/NoticeServiceTest.java index 31b2276..0bc4083 100644 --- a/src/test/java/com/itcontest/skhuming/notice/application/NoticeServiceTest.java +++ b/src/test/java/com/itcontest/skhuming/notice/application/NoticeServiceTest.java @@ -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; @@ -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); @@ -51,21 +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"); - Member member = Member.createMember(memberSaveReqDto); + Member member = new Member(".com", "chlrldnd","너의 집앞 골목", "최기웅", "it융합자율학부", " 202014098"); memberService.memberSave(member); System.out.println("member.getMemberId() = " + member.getMemberId());