Skip to content

Commit

Permalink
Merge pull request #20 from giwoong01/develop
Browse files Browse the repository at this point in the history
페이징 기능 구현
  • Loading branch information
giwoong01 authored Sep 2, 2023
2 parents 7679ce8 + 0dae848 commit 8e1ce4a
Show file tree
Hide file tree
Showing 23 changed files with 368 additions and 39 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'

runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'

compileOnly 'org.projectlombok:lombok'

runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import com.itcontest.skhuming.jwt.exception.NotMatchTokenException;
import com.itcontest.skhuming.member.exception.InvalidMemberException;
import com.itcontest.skhuming.member.exception.NotFoundMemberException;
import com.itcontest.skhuming.mileage.exception.ExistsMemberHistoryMileageException;
import com.itcontest.skhuming.mileage.exception.NotFoundMileageException;
import com.itcontest.skhuming.notice.exception.NotFoundNoticeException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestControllerAdvice
public class ControllerAdvice {

Expand All @@ -18,7 +24,9 @@ public class ControllerAdvice {
InvalidEmailAddressException.class,
NotFoundMemberException.class,
NotFoundNoticeException.class,
NotMatchTokenException.class
NotFoundMileageException.class,
NotMatchTokenException.class,
ExistsMemberHistoryMileageException.class
})
public ResponseEntity<ErrorResponse> handleInvalidate(RuntimeException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RestController
public class MainController {

private final MainService mainService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ public ResponseEntity<MemberProfileResDto> memberProfileResponse(@RequestParam("
return new ResponseEntity<>(memberService.memberProfileResponse(memberId), HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class MemberDto {

private String token;

private String authorityName;

protected MemberDto() {

}

@Builder
private MemberDto(Long memberId, String email, String nickname, String memberName, String department, String studentNumber, int score, Tier tier, String token) {
private MemberDto(Long memberId, String email, String nickname, String memberName, String department, String studentNumber, int score, Tier tier, String token, String authorityName) {
this.memberId = memberId;
this.email = email;
this.nickname = nickname;
Expand All @@ -40,6 +42,7 @@ private MemberDto(Long memberId, String email, String nickname, String memberNam
this.score = score;
this.tier = tier;
this.token = token;
this.authorityName = authorityName;
}

public MemberDto(Member member) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.itcontest.skhuming.member.application;

import com.itcontest.skhuming.jwt.Authority;
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.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 @@ -16,17 +17,20 @@
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class MemberService {

private final MemberRepository memberRepository;
private final AuthorityRepository authorityRepository;
private final PasswordEncoder passwordEncoder;
private final JwtProvider jwtProvider;

public MemberService(MemberRepository memberRepository, PasswordEncoder passwordEncoder, JwtProvider jwtProvider) {
public MemberService(MemberRepository memberRepository, AuthorityRepository authorityRepository, PasswordEncoder passwordEncoder, JwtProvider jwtProvider) {
this.memberRepository = memberRepository;
this.authorityRepository = authorityRepository;
this.passwordEncoder = passwordEncoder;
this.jwtProvider = jwtProvider;
}
Expand All @@ -43,12 +47,14 @@ public void memberJoin(MemberSaveReqDto memberSaveReqDto) {

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

validateDuplicateEmail(member);
validateDuplicateNickname(member);
memberRepository.save(member);
}

public MemberDto memberLogin(MemberLoginReqDto memberLoginReqDto) {
Member member = memberRepository.findByEmail(memberLoginReqDto.getEmail()).orElseThrow(NotFoundMemberException::new);
Authority authority = authorityRepository.findById(member.getMemberId()).orElseThrow();

validatePassword(memberLoginReqDto.getPwd(), member.getPwd());

Expand All @@ -62,9 +68,16 @@ public MemberDto memberLogin(MemberLoginReqDto memberLoginReqDto) {
.score(member.getScore())
.tier(member.getTier())
.token(jwtProvider.createToken(member.getEmail(), member.getRoles()))
.authorityName(authority.getName())
.build();
}

private void validateDuplicateEmail(Member member) {
if (memberRepository.existsByEmail(member.getEmail())) {
throw new InvalidMemberException("이미 사용중인 이메일 입니다.");
}
}

private void validateDuplicateNickname(Member member) {
if (memberRepository.existsByNickname(member.getNickname())) {
throw new InvalidMemberException("이미 사용중인 닉네임 입니다.");
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/com/itcontest/skhuming/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.itcontest.skhuming.member.domain;

import com.itcontest.skhuming.email.exception.InvalidEmailAddressException;
import com.itcontest.skhuming.jwt.Authority;
import com.itcontest.skhuming.jwt.domain.Authority;
import com.itcontest.skhuming.member.exception.InvalidMemberException;
import com.itcontest.skhuming.mileage.domain.Mileage;
import com.itcontest.skhuming.notice.domain.Notice;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -43,12 +44,13 @@ public class Member {
@Enumerated(EnumType.STRING)
private Tier tier;


@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberScrapNotice> myScrap = new ArrayList<>();

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberHistoryMileage> mileageHistory = new ArrayList<>();

@OneToMany(mappedBy = "member", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Builder.Default
private List<Authority> roles = new ArrayList<>();

public void setRoles(List<Authority> role) {
Expand Down Expand Up @@ -96,7 +98,7 @@ public void addScrapNotice(Notice notice) {
myScrap.add(memberScrapNotice);
}

public void cancelScrapNotice(Notice notice) {
public void cancelScrapNotice(Notice notice) {
MemberScrapNotice memberScrapNotice = findScrapNotice(notice);
myScrap.remove(memberScrapNotice);
}
Expand All @@ -114,11 +116,40 @@ public List<Notice> getScrapNotices() {
.collect(Collectors.toList());
}

public void addMileageHistory(Mileage mileage, String systemDate) {
MemberHistoryMileage memberAddMileage = new MemberHistoryMileage(this, mileage, systemDate);
mileageHistory.add(memberAddMileage);
}

public void cancelMileageHistory(Mileage mileage) {
MemberHistoryMileage memberHistoryMileage = findMileageHistory(mileage);
mileageHistory.remove(memberHistoryMileage);
}

private MemberHistoryMileage findMileageHistory(Mileage mileage) {
return mileageHistory.stream()
.filter(memberHistoryMileage -> memberHistoryMileage.getMileage().equals(mileage))
.findFirst()
.orElse(null);
}

public List<Mileage> getMileageHistory() {
return mileageHistory.stream()
.map(MemberHistoryMileage::getMileage)
.collect(Collectors.toList());
}


public void plusMyScore(int score) {
this.score += score;
updateTier();
}

public void minusMyScore(int score) {
this.score -= score;
updateTier();
}

private void updateTier() {
if (this.score >= 500) {
this.tier = Tier.SS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.itcontest.skhuming.member.domain;

import com.itcontest.skhuming.mileage.domain.Mileage;
import lombok.Getter;

import javax.persistence.*;

@Entity
@Getter
public class MemberHistoryMileage {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String systemDate;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne
@JoinColumn(name = "mileage_id")
private Mileage mileage;

protected MemberHistoryMileage() {

}

public MemberHistoryMileage(Member member, Mileage mileage, String systemDate) {
this.member = member;
this.mileage = mileage;
this.systemDate = systemDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.itcontest.skhuming.member.domain.repository;

import com.itcontest.skhuming.member.domain.Member;
import com.itcontest.skhuming.member.domain.MemberHistoryMileage;
import com.itcontest.skhuming.mileage.domain.Mileage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberHistoryMileageRepository extends JpaRepository<MemberHistoryMileage, Long> {
MemberHistoryMileage findByMemberAndMileage(Member member, Mileage mileage);
boolean existsByMemberAndMileage(Member member, Mileage mileage);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -9,7 +10,9 @@
@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {

boolean existsByEmail(String email);
boolean existsByNickname(String nickname);

Optional<Member> findByEmail(String email);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.itcontest.skhuming.member.domain.repository;

import com.itcontest.skhuming.member.domain.MemberScrapNotice;
import com.itcontest.skhuming.notice.domain.Notice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberScrapNoticeRepository extends JpaRepository<MemberScrapNotice, Long> {
List<MemberScrapNotice> findByNotice(Notice notice);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.itcontest.skhuming.mileage.api.response.MemberMileageResDto;
import com.itcontest.skhuming.mileage.api.request.MemberMileageReqDto;
import com.itcontest.skhuming.mileage.api.response.MileageHistoryResDto;
import com.itcontest.skhuming.mileage.api.response.MileageResDto;
import com.itcontest.skhuming.mileage.application.MileageService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MileageController {

Expand All @@ -16,13 +20,29 @@ public MileageController(MileageService mileageService) {
this.mileageService = mileageService;
}

@GetMapping("/api/mileage/select-box")
public ResponseEntity<List<MileageResDto>> mileageSelectBoxResponse() {
return new ResponseEntity<>(mileageService.mileageSelectBoxResponse(), HttpStatus.OK);
}

@PostMapping("/user/api/mileage/post")
public ResponseEntity<String> memberMileageRequest(@RequestBody MemberMileageReqDto memberMileageReqDto) {
mileageService.memberMileageRequest(memberMileageReqDto);
memberMileageResponse(memberMileageReqDto.getMemberId());
return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
}

@PostMapping("/user/api/mileage/history/cancel")
public ResponseEntity<String> cancelMileageHistory(@RequestParam("memberId") Long memberId, @RequestParam("mileageId") Long mileageId) {
mileageService.mileageHistoryCancel(memberId, mileageId);
return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
}

@GetMapping("/user/api/mileage/history/list")
public ResponseEntity<List<MileageHistoryResDto>> myMileageHistoryList(@RequestParam("memberId") Long memberId) {
return new ResponseEntity<>(mileageService.mileageHistory(memberId), HttpStatus.OK);
}

@GetMapping("/user/api/mileage/get")
public ResponseEntity<MemberMileageResDto> memberMileageResponse(@RequestParam("memberId") Long memberId) {
return new ResponseEntity<>(mileageService.memberMileageResponse(memberId), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberMileageReqDto {
private Long memberId;
private String title;
private int score;
private Long mileageId;

public MemberMileageReqDto(Long memberId, String title, int score) {
public MemberMileageReqDto(Long memberId,Long mileageId) {
this.memberId = memberId;
this.title = title;
this.score = score;
this.mileageId = mileageId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.itcontest.skhuming.mileage.api.response;

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

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

private Long mileageId;

private String title;

private int mileageScore;

private String systemDate;

public MileageHistoryResDto(Long mileageId, String title, int mileageScore, String systemDate) {
this.mileageId = mileageId;
this.title = title;
this.mileageScore = mileageScore;
this.systemDate = systemDate;
}
}
Loading

0 comments on commit 8e1ce4a

Please sign in to comment.