Skip to content

Commit

Permalink
[#12] Transactional refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
giwoong01 authored Sep 10, 2023
2 parents c57d9ef + fafd507 commit 2847675
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.itcontest.skhuming.notice.domain.repository.NoticeRepository;
import com.itcontest.skhuming.notice.exception.NotFoundNoticeException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.transaction.Transactional;
import java.util.List;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.itcontest.skhuming.main.application;

import com.itcontest.skhuming.global.util.ChangeDepartmentUtil;
import com.itcontest.skhuming.global.util.ChangeDepartment;
import com.itcontest.skhuming.member.api.dto.response.DepartmentRankResDto;
import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto;
import com.itcontest.skhuming.member.domain.Member;
Expand All @@ -14,7 +14,7 @@
import java.util.List;

@Service
@Transactional
@Transactional(readOnly = true)
public class MainService {
private final MemberRepository memberRepository;

Expand Down Expand Up @@ -50,7 +50,7 @@ public List<MemberRankResDto> mainAllRanking() {
*/
public List<MemberRankResDto> mainDepartmentRanking(int departmentNumber) {
List<Member> memberList = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score"));
ChangeDepartment.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score"));

List<MemberRankResDto> memberRankingList = new ArrayList<>();
for (int i = 0; i < Math.min(3, memberList.size()); i++) {
Expand All @@ -77,7 +77,7 @@ public List<DepartmentRankResDto> departmentByRanking() {
for (int i = 1; i < 5; i++) {
int sum = 0;
List<Member> memberList = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(i), Sort.by(Sort.Direction.DESC, "score"));
ChangeDepartment.departmentNumber(i), Sort.by(Sort.Direction.DESC, "score"));
for (Member member : memberList) {
sum += member.getScore();
}
Expand All @@ -93,7 +93,7 @@ public List<DepartmentRankResDto> departmentByRanking() {
for (Integer i : scoreList) {
DepartmentRankResDto departmentRankResDto = new DepartmentRankResDto(
idx,
ChangeDepartmentUtil.departmentNumber(i),
ChangeDepartment.departmentNumber(i),
scoreMap.get(i)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.ChangeDepartment;
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;
Expand All @@ -20,7 +20,7 @@
import java.util.Collections;

@Service
@Transactional
@Transactional(readOnly = true)
public class MemberService {

private final MemberRepository memberRepository;
Expand All @@ -35,13 +35,14 @@ public MemberService(MemberRepository memberRepository, AuthorityRepository auth
this.jwtProvider = jwtProvider;
}

@Transactional
public void memberJoin(MemberSaveReqDto memberSaveReqDto) {
Member member = Member.builder()
.email(memberSaveReqDto.getEmail())
.pwd(passwordEncoder.encode(memberSaveReqDto.getPwd()))
.nickname(memberSaveReqDto.getNickname())
.memberName(memberSaveReqDto.getMemberName())
.department(ChangeDepartmentUtil.departmentNumber(memberSaveReqDto.getDepartment()))
.department(ChangeDepartment.departmentNumber(memberSaveReqDto.getDepartment()))
.studentNumber(memberSaveReqDto.getStudentNumber())
.role(Collections.singletonList(Authority.builder().name("ROLE_USER").build()))
.build();
Expand All @@ -51,6 +52,7 @@ public void memberJoin(MemberSaveReqDto memberSaveReqDto) {
memberRepository.save(member);
}

@Transactional
public MemberDto memberLogin(MemberLoginReqDto memberLoginReqDto) {
Member member = memberRepository.findByEmail(memberLoginReqDto.getEmail()).orElseThrow(NotFoundMemberException::new);
Authority authority = authorityRepository.findById(member.getMemberId()).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.itcontest.skhuming.mileage.domain.repository.MileageRepository;
import com.itcontest.skhuming.mileage.exception.ExistsMemberHistoryMileageException;
import com.itcontest.skhuming.mileage.exception.NotFoundMileageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,8 +24,7 @@
import java.util.List;

@Service
@Slf4j
@Transactional
@Transactional(readOnly = true)
public class MileageService {

private final List<String> messageList = new ArrayList<>();
Expand Down Expand Up @@ -79,6 +77,7 @@ public List<MileageResDto> mileageSelectBoxResponse() {
/**
* 마일리지 추가
*/
@Transactional
public void memberMileageRequest(MemberMileageReqDto memberMileageReqDto) {
SecurityUtil.memberTokenMatch(memberMileageReqDto.getMemberId());

Expand Down Expand Up @@ -123,6 +122,7 @@ private StringBuilder message(Member member, int historyRanking, int curRanking)
}
}
}

return message;
}

Expand All @@ -148,6 +148,7 @@ private int myRanking(int score) {
/**
* 마일리지 추가한 것 취소
*/
@Transactional
public void mileageHistoryCancel(Long memberId, Long mileageId) {
SecurityUtil.memberTokenMatch(memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.stream.Collectors;

@Service
@Transactional
@Transactional(readOnly = true)
public class NoticeService {

private static final String TILDE = "~";
Expand All @@ -43,6 +43,9 @@ public NoticeService(NoticeRepository noticeRepository, MemberRepository memberR
formatter = DateTimeFormatter.ofPattern("MMdd");
}

/**
* 세부 공지
*/
public DetailsNoticeResDto detailsNoticeResponse(Long noticeId) {
Notice notice = noticeRepository.findById(noticeId).orElseThrow(NotFoundNoticeException::new);

Expand All @@ -59,13 +62,19 @@ public DetailsNoticeResDto detailsNoticeResponse(Long noticeId) {
memberIdList);
}

/**
* 공지 리스트
*/
public Page<NoticeListResDto> noticeList(int page, int size) {
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.trim(), PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "noticeId")));
Expand All @@ -82,11 +91,15 @@ private NoticeListResDto mapToNotice(Notice notice) {

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

return new NoticeListResDto(notice.getNoticeId(),
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);
Expand All @@ -98,6 +111,9 @@ public List<NoticeListResDto> myPageScrapNoticeList(Long memberId) {
.collect(Collectors.toList());
}

/**
* 마이 스크랩 페이징 리스트
*/
public Page<NoticeListResDto> myScrapNoticeList(Long memberId, int page, int size) {
SecurityUtil.memberTokenMatch(memberId);
Member member = memberRepository.findById(memberId).orElseThrow(NotFoundMemberException::new);
Expand Down Expand Up @@ -143,6 +159,10 @@ private void updateLocalDate() {
this.now = LocalDate.now();
}

/**
* 공지 스크랩
*/
@Transactional
public void noticeScrap(Long memberId, Long noticeId) {
SecurityUtil.memberTokenMatch(memberId);

Expand All @@ -153,6 +173,10 @@ public void noticeScrap(Long memberId, Long noticeId) {
memberRepository.save(member);
}

/**
* 공지 스크랩 취소
*/
@Transactional
public void noticeScrapCancel(Long memberId, Long noticeId) {
SecurityUtil.memberTokenMatch(memberId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.itcontest.skhuming.ranking.application;

import com.itcontest.skhuming.global.util.ChangeDepartmentUtil;
import com.itcontest.skhuming.global.util.ChangeDepartment;
import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto;
import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.member.domain.repository.MemberRepository;
Expand All @@ -13,7 +13,7 @@
import java.util.List;

@Service
@Transactional
@Transactional(readOnly = true)
public class RankingService {

private final MemberRepository memberRepository;
Expand All @@ -37,7 +37,7 @@ public Page<MemberRankResDto> memberRanking(int page, int size) {
*/
public Page<MemberRankResDto> memberDepartmentRanking(int departmentNumber, int page, int size) {
Page<Member> members = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(departmentNumber), PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "score")));
ChangeDepartment.departmentNumber(departmentNumber), PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "score")));

return members.map(member -> mapToDepartmentMember(member, departmentNumber));
}
Expand Down Expand Up @@ -77,7 +77,7 @@ private MemberRankResDto mapToDepartmentMember(Member member, int departmentNumb

private int myDepartmentRanking(int departmentNumber, int score) {
List<Member> rankedMembers = memberRepository.findByDepartment(
ChangeDepartmentUtil.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score"));
ChangeDepartment.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score"));

for (int i = 0; i < rankedMembers.size(); i++) {
if (rankedMembers.get(i).getScore() == score) {
Expand Down

0 comments on commit 2847675

Please sign in to comment.