Skip to content

Commit

Permalink
Merge pull request #5 from giwoong01/develop
Browse files Browse the repository at this point in the history
main-raking-list 기능 추가
  • Loading branch information
giwoong01 authored Jul 16, 2023
2 parents 916f820 + de7a967 commit 5b53e42
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -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<List<Member>> mainRankingList() {
return new ResponseEntity<>(memberService.mainPageRanking(), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member> mainPageRanking() {
List<Member> memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score"));

List<Member> memberRankingList = new ArrayList<>();
int count = 0;
for (Member member : memberList) {
memberRankingList.add(member);
count += 1;

if (count == 3) {
break;
}
}

return memberRankingList;
}

/**
* 유저본인이 스크랩한 공지 리스트
*/
public List<Notice> memberMyScrapList(Long memberId) {
Member member = memberRepository.findById(memberId).get();

Expand Down
29 changes: 13 additions & 16 deletions src/main/java/com/itcontest/skhuming/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -14,7 +12,7 @@
public class Member {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long memberId;

Expand All @@ -38,31 +36,30 @@ 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);
}

// 비즈니스로직
public void addMyScrap(Notice notice) {
this.myScrap.add(notice);
}

public void plusMyScore(int score) {
this.score += score;
}
}
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
22 changes: 5 additions & 17 deletions src/main/java/com/itcontest/skhuming/notice/domain/Notice.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notice_id")
private Long noticeId;

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
@@ -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<Member> 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());
}

}
}
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,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());
Expand Down

0 comments on commit 5b53e42

Please sign in to comment.