Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
giwoong01 committed Sep 8, 2023
1 parent 329dac7 commit 80674d2
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.itcontest.skhuming.error.dto;
package com.itcontest.skhuming.global.error.dto;

import lombok.Getter;

@Getter
public class ErrorResponse {
private final String message;

public ErrorResponse(final String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class MemberSaveReqDto {

private String memberName;

private String department;
private int department;

private String studentNumber;

public MemberSaveReqDto(String email, String pwd, String nickname, String memberName, String department, String studentNumber) {
public MemberSaveReqDto(String email, String pwd, String nickname, String memberName, int department, String studentNumber) {
this.email = email;
this.pwd = pwd;
this.nickname = nickname;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.itcontest.skhuming.member.api.dto.response;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

private Long departmentId;
private String department;
private int score;

public DepartmentRankResDto(Long departmentId, String department, int score) {
this.departmentId = departmentId;
this.department = department;
this.score = score;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package com.itcontest.skhuming.member.api.dto.response;

import com.itcontest.skhuming.member.domain.Tier;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

private Long memberId;
private Tier tier;
private int score;
private String nickname;
private String department;
private int myRanking;

public MemberRankResDto(Long memberId, Tier tier, int score, String nickname, String department) {
public MemberRankResDto(Long memberId, Tier tier, int score, String nickname, String department, int myRanking) {
this.memberId = memberId;
this.tier = tier;
this.score = score;
this.nickname = nickname;
this.department = department;
this.myRanking = myRanking;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.itcontest.skhuming.member.application;

import com.itcontest.skhuming.jwt.domain.Authority;
import com.itcontest.skhuming.jwt.JwtProvider;
import com.itcontest.skhuming.jwt.SecurityUtil;
import com.itcontest.skhuming.jwt.domain.repository.AuthorityRepository;
import com.itcontest.skhuming.global.jwt.domain.Authority;
import com.itcontest.skhuming.global.jwt.JwtProvider;
import com.itcontest.skhuming.global.util.ChangeDepartmentUtil;
import com.itcontest.skhuming.global.util.SecurityUtil;
import com.itcontest.skhuming.global.jwt.domain.repository.AuthorityRepository;
import com.itcontest.skhuming.member.api.dto.request.MemberLoginReqDto;
import com.itcontest.skhuming.member.api.dto.request.MemberSaveReqDto;
import com.itcontest.skhuming.member.api.dto.response.MemberDto;
Expand All @@ -17,7 +18,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Optional;

@Service
@Transactional
Expand All @@ -41,12 +41,11 @@ public void memberJoin(MemberSaveReqDto memberSaveReqDto) {
.pwd(passwordEncoder.encode(memberSaveReqDto.getPwd()))
.nickname(memberSaveReqDto.getNickname())
.memberName(memberSaveReqDto.getMemberName())
.department(memberSaveReqDto.getDepartment())
.department(ChangeDepartmentUtil.departmentNumber(memberSaveReqDto.getDepartment()))
.studentNumber(memberSaveReqDto.getStudentNumber())
.role(Collections.singletonList(Authority.builder().name("ROLE_USER").build()))
.build();

member.setRoles(Collections.singletonList(Authority.builder().name("ROLE_USER").build()));

validateDuplicateEmail(member);
validateDuplicateNickname(member);
memberRepository.save(member);
Expand Down
34 changes: 14 additions & 20 deletions src/main/java/com/itcontest/skhuming/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.itcontest.skhuming.member.domain;

import com.itcontest.skhuming.email.exception.InvalidEmailAddressException;
import com.itcontest.skhuming.jwt.domain.Authority;
import com.itcontest.skhuming.global.jwt.domain.Authority;
import com.itcontest.skhuming.member.exception.InvalidMemberException;
import com.itcontest.skhuming.mileage.domain.Mileage;
import com.itcontest.skhuming.notice.domain.Notice;
Expand Down Expand Up @@ -53,15 +53,10 @@ public class Member {
@OneToMany(mappedBy = "member", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Authority> roles = new ArrayList<>();

public void setRoles(List<Authority> role) {
this.roles = role;
role.forEach(o -> o.setMember(this));
}

protected Member() {
}

private Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber, int score, Tier tier) {
private Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber, List<Authority> role, int score, Tier tier) {
validateEmail(email);
validateNickname(nickname);

Expand All @@ -71,13 +66,14 @@ private Member(String email, String pwd, String nickname, String memberName, Str
this.memberName = memberName;
this.department = department;
this.studentNumber = studentNumber;
addRoles(role);
this.score = score;
this.tier = tier;
}

@Builder
public Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber) {
this(email, pwd, nickname, memberName, department, studentNumber, 0, Tier.Un);
public Member(String email, String pwd, String nickname, String memberName, String department, String studentNumber, List<Authority> role) {
this(email, pwd, nickname, memberName, department, studentNumber, role, 0, Tier.Un);
}

private void validateEmail(String email) {
Expand All @@ -93,12 +89,17 @@ private void validateNickname(String nickname) {
}
}

public void addRoles(List<Authority> role) {
this.roles = role;
role.forEach(o -> o.setMember(this));
}

public void addScrapNotice(Notice notice) {
MemberScrapNotice memberScrapNotice = new MemberScrapNotice(this, notice);
myScrap.add(memberScrapNotice);
}

public void cancelScrapNotice(Notice notice) {
public void cancelScrapNotice(Notice notice) {
MemberScrapNotice memberScrapNotice = findScrapNotice(notice);
myScrap.remove(memberScrapNotice);
}
Expand All @@ -110,12 +111,6 @@ private MemberScrapNotice findScrapNotice(Notice notice) {
.orElse(null);
}

public List<Notice> getScrapNotices() {
return myScrap.stream()
.map(MemberScrapNotice::getNotice)
.collect(Collectors.toList());
}

public void addMileageHistory(Mileage mileage, String systemDate) {
MemberHistoryMileage memberAddMileage = new MemberHistoryMileage(this, mileage, systemDate);
mileageHistory.add(memberAddMileage);
Expand All @@ -139,7 +134,6 @@ public List<Mileage> getMileageHistory() {
.collect(Collectors.toList());
}


public void plusMyScore(int score) {
this.score += score;
updateTier();
Expand All @@ -151,11 +145,11 @@ public void minusMyScore(int score) {
}

private void updateTier() {
if (this.score >= 500) {
if (this.score >= 800) {
this.tier = Tier.SS;
} else if (this.score >= 400) {
} else if (this.score >= 600) {
this.tier = Tier.S;
} else if (this.score >= 300) {
} else if (this.score >= 400) {
this.tier = Tier.A;
} else if (this.score >= 200) {
this.tier = Tier.B;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.itcontest.skhuming.member.domain.repository;

import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.member.domain.MemberHistoryMileage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -15,4 +20,13 @@ public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByEmail(String email);

@Query("select m " +
"from Member m " +
"where m.department = :department")
List<Member> findByDepartment(@Param("department") String department, Sort sort);

@Query("select m " +
"from Member m " +
"where m.department = :department")
Page<Member> findByDepartment(@Param("department") String department, PageRequest pageRequest);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package com.itcontest.skhuming.member.domain.repository;

import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.member.domain.MemberScrapNotice;
import com.itcontest.skhuming.notice.domain.Notice;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberScrapNoticeRepository extends JpaRepository<MemberScrapNotice, Long> {
List<MemberScrapNotice> findByNotice(Notice notice);

@Query("select m " +
"from MemberScrapNotice m " +
"where m.member = :member " )
List<MemberScrapNotice> findByMyScrapNotice(@Param("member") Member member);

@Query("select m " +
"from MemberScrapNotice m " +
"where m.member = :member ")
Page<MemberScrapNotice> findByMyScrapNotice(@Param("member") Member member, PageRequest pageRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.util.List;


@RestController
public class NoticeController {

Expand All @@ -29,7 +28,6 @@ public ResponseEntity<DetailsNoticeResDto> detailsNoticeResponse(@RequestParam("
public ResponseEntity<Page<NoticeListResDto>> noticeSearchList(@RequestParam(value = "searchKeyword", required = false) String searchKeyword,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {

Page<NoticeListResDto> noticeSearchPage;
if (searchKeyword == null) {
noticeSearchPage = noticeService.noticeList(page, size);
Expand All @@ -40,6 +38,11 @@ public ResponseEntity<Page<NoticeListResDto>> noticeSearchList(@RequestParam(val
return new ResponseEntity<>(noticeSearchPage, HttpStatus.OK);
}

@GetMapping("/user/api/my-page/scrap/list")
public ResponseEntity<List<NoticeListResDto>> myPageScrapList(@RequestParam("memberId") Long memberId) {
return new ResponseEntity<>(noticeService.myPageScrapNoticeList(memberId), HttpStatus.OK);
}

@GetMapping("/user/api/scrap/list")
public ResponseEntity<Page<NoticeListResDto>> myScrapList(@RequestParam("memberId") Long memberId,
@RequestParam(value = "page", defaultValue = "0") int page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.itcontest.skhuming.notice.domain.Notice;
import com.itcontest.skhuming.notice.domain.repository.NoticeRepository;
import com.itcontest.skhuming.notice.exception.NotFoundNoticeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
Expand All @@ -22,9 +21,9 @@
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class NoticeService {

Expand Down Expand Up @@ -61,13 +60,15 @@ public DetailsNoticeResDto detailsNoticeResponse(Long noticeId) {
}

public Page<NoticeListResDto> noticeList(int page, int size) {
Page<Notice> noticeSearchPage = noticeRepository.findAll(PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));
Page<Notice> noticeSearchPage = noticeRepository.findAll(
PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));

return noticeSearchPage.map(this::mapToNotice);
}

public Page<NoticeListResDto> noticeSearchList(String searchKeyword, int page, int size) {
Page<Notice> noticeSearchPage = noticeRepository.findByTitleContaining(searchKeyword, PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));
Page<Notice> noticeSearchPage = noticeRepository.findByTitleContaining(
searchKeyword.trim(), PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));

return noticeSearchPage.map(this::mapToNotice);
}
Expand All @@ -81,14 +82,28 @@ private NoticeListResDto mapToNotice(Notice notice) {

boolean end = Integer.parseInt(date) < Integer.parseInt(systemTime);

return new NoticeListResDto(notice.getNoticeId(), notice.getTitle(), end);
return new NoticeListResDto(notice.getNoticeId(),
notice.getTitle(),
end);
}

public List<NoticeListResDto> myPageScrapNoticeList(Long memberId) {
SecurityUtil.memberTokenMatch(memberId);
Member member = memberRepository.findById(memberId).orElseThrow(NotFoundMemberException::new);

List<MemberScrapNotice> memberScrapNoticeList = memberScrapNoticeRepository.findByMyScrapNotice(member);

return memberScrapNoticeList.stream()
.map(this::mapToMyScrapNotice)
.collect(Collectors.toList());
}

public Page<NoticeListResDto> myScrapNoticeList(Long memberId, int page, int size) {
SecurityUtil.memberTokenMatch(memberId);
Member member = memberRepository.findById(memberId).orElseThrow(NotFoundMemberException::new);

Page<MemberScrapNotice> myScrapNoticePage = memberScrapNoticeRepository.findByMyScrapNotice(member, PageRequest.of(page, size));
Page<MemberScrapNotice> myScrapNoticePage = memberScrapNoticeRepository.findByMyScrapNotice(
member, PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id")));

return myScrapNoticePage.map(this::mapToMyScrapNotice);
}
Expand All @@ -102,7 +117,10 @@ private NoticeListResDto mapToMyScrapNotice(MemberScrapNotice MemberScrapNotice)

boolean end = Integer.parseInt(date) < Integer.parseInt(systemTime);

return new NoticeListResDto(MemberScrapNotice.getNotice().getNoticeId(), MemberScrapNotice.getNotice().getTitle(), end);
return new NoticeListResDto(
MemberScrapNotice.getNotice().getNoticeId(),
MemberScrapNotice.getNotice().getTitle(),
end);
}

private String getToStringDate(String schedule) {
Expand Down
Loading

0 comments on commit 80674d2

Please sign in to comment.