Skip to content

Commit

Permalink
Merge pull request #53 from Gamegoo-repo/feat/49
Browse files Browse the repository at this point in the history
[Feat/49] ์ฐจ๋‹จํ•œ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ API ๊ตฌํ˜„
  • Loading branch information
rimi3226 authored Jul 2, 2024
2 parents b87dad4 + d1a4af8 commit 443037a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public enum ErrorStatus implements BaseErrorCode {
// ํ…Œ์ŠคํŠธ
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "ํ…Œ์ŠคํŠธ"),

// ํŽ˜์ด์ง• ๊ด€๋ จ ์—๋Ÿฌ
PAGE_NOT_VALID(HttpStatus.BAD_REQUEST, "PAGE401", "ํŽ˜์ด์ง€ ๊ฐ’์€ 1 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."),

// Member ๊ด€๋ จ ์—๋Ÿฌ
PASSWORD_INVALID(HttpStatus.BAD_REQUEST, "MEMBER400", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ถˆ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "MEMBER404", "ํ•ด๋‹น ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gamegoo.apiPayload.exception.handler;

import com.gamegoo.apiPayload.code.BaseErrorCode;
import com.gamegoo.apiPayload.exception.GeneralException;

public class PageHandler extends GeneralException {
public PageHandler(BaseErrorCode code) {
super(code);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/gamegoo/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration c

@Bean
public JWTFilter jwtFilter() {
List<String> excludedPaths = Arrays.asList("/api/member/join", "/api/member/login", "/api/member/email", "/api/member/refresh");
List<String> excludedPaths = Arrays.asList("/swagger-ui/", "/v3/api-docs", "/api/member/join", "/api/member/login", "/api/member/email", "/api/member/refresh");
return new JWTFilter(jwtUtil, excludedPaths, customUserDetailService);

}
Expand All @@ -58,6 +58,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

.authorizeHttpRequests((auth) -> auth
.antMatchers("/api/member/join", "/api/member/login", "/api/member/email/**", "/api/member/refresh").permitAll()
.antMatchers("/", "/swagger-ui/**", "/v3/api-docs/**").permitAll()
.anyRequest().authenticated())
.addFilterBefore(new JWTExceptionHandlerFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterAt(new LoginFilter(authenticationManager(authenticationConfiguration), jwtUtil, memberRepository), UsernamePasswordAuthenticationFilter.class)
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/gamegoo/controller/member/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.gamegoo.controller.member;

import com.gamegoo.apiPayload.ApiResponse;
import com.gamegoo.converter.MemberConverter;
import com.gamegoo.domain.Member;
import com.gamegoo.service.member.MemberService;
import com.gamegoo.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "Member", description = "ํšŒ์› ๊ด€๋ จ API")
Expand All @@ -31,4 +31,15 @@ public ApiResponse<String> blockMember(@PathVariable(name = "memberId") Long tar

return ApiResponse.onSuccess("ํšŒ์› ์ฐจ๋‹จ ์„ฑ๊ณต");
}

@Operation(summary = "์ฐจ๋‹จํ•œ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ API", description = "๋‚ด๊ฐ€ ์ฐจ๋‹จํ•œ ํšŒ์›์˜ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
@Parameter(name = "page", description = "ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ, 1 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
@GetMapping("/block")
public ApiResponse<Object> getBlockList(@RequestParam(name = "page") Integer page) {
Long memberId = JWTUtil.getCurrentUserId();

Page<Member> blockList = memberService.getBlockList(memberId, page - 1);

return ApiResponse.onSuccess(MemberConverter.toBlockListDto(blockList));
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/gamegoo/converter/MemberConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.gamegoo.converter;

import com.gamegoo.domain.Member;
import com.gamegoo.dto.member.MemberResponse;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

public class MemberConverter {

public static MemberResponse.blockListDto toBlockListDto(Page<Member> blockList) {
List<MemberResponse.blockedMemberDto> blockedMemberDtoList = blockList.stream()
.map(MemberConverter::toBlockedMemberDto)
.collect(Collectors.toList());

return MemberResponse.blockListDto.builder()
.blockedMemberDtoList(blockedMemberDtoList)
.listSize(blockedMemberDtoList.size())
.totalPage(blockList.getTotalPages())
.totalElements(blockList.getTotalElements())
.isFirst(blockList.isFirst())
.isLast(blockList.isLast())
.build();
}

public static MemberResponse.blockedMemberDto toBlockedMemberDto(Member membr) {
return MemberResponse.blockedMemberDto.builder()
.memberId(membr.getId())
.profileImg(membr.getProfileImage())
.email(membr.getEmail())
.name(membr.getGameuserName())
.build();
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/gamegoo/dto/member/MemberResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.gamegoo.dto.member;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

public class MemberResponse {
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class blockListDto {
List<blockedMemberDto> blockedMemberDtoList;
Integer listSize;
Integer totalPage;
Long totalElements;
Boolean isFirst;
Boolean isLast;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class blockedMemberDto {
Long memberId;
String profileImg;
String email;
String name;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.gamegoo.repository.member;

import com.gamegoo.domain.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

Expand All @@ -12,4 +16,8 @@ public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByRefreshToken(String refresh_token);

@Query("SELECT m FROM Member m INNER JOIN Block b ON m.id = b.blockedMember.id WHERE b.blockerMember.id = :blockerId AND m.blind = false ORDER BY b.createdAt DESC")
Page<Member> findBlockedMembersByBlockerIdAndNotBlind(@Param("blockerId") Long blockerId, Pageable pageable);


}
28 changes: 27 additions & 1 deletion src/main/java/com/gamegoo/service/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.gamegoo.apiPayload.code.status.ErrorStatus;
import com.gamegoo.apiPayload.exception.handler.BlockHandler;
import com.gamegoo.apiPayload.exception.handler.MemberHandler;
import com.gamegoo.apiPayload.exception.handler.PageHandler;
import com.gamegoo.domain.Block;
import com.gamegoo.domain.Member;
import com.gamegoo.repository.member.BlockRepository;
import com.gamegoo.repository.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,6 +22,8 @@ public class MemberService {
private final MemberRepository memberRepository;
private final BlockRepository blockRepository;

Integer pageSize = 10;

/**
* memberId์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์›์ด targetMemberId์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์›์„ ์ฐจ๋‹จ
*
Expand Down Expand Up @@ -51,14 +56,35 @@ public Member blockMember(Long memberId, Long targetMemberId) {
return member;
}

/**
* memberId์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์›์ด ์ฐจ๋‹จํ•œ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ
*
* @param memberId
* @param pageIdx 0 ์ด์ƒ์˜ ๊ฐ’์ด์–ด์•ผ ํ•จ
* @return
*/
public Page<Member> getBlockList(Long memberId, Integer pageIdx) {
// ํŽ˜์ด์ง€ ๊ฐ’ ๊ฒ€์ฆ
if (pageIdx < 0) {
throw new PageHandler(ErrorStatus.PAGE_NOT_VALID);
}

// member ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
PageRequest pageRequest = PageRequest.of(pageIdx, pageSize);

return memberRepository.findBlockedMembersByBlockerIdAndNotBlind(member.getId(), pageRequest);
}

/**
* ํ•ด๋‹น ํšŒ์›์ด ํƒˆํ‡ดํ–ˆ๋Š”์ง€ ๊ฒ€์ฆ
*
* @param member
*/
void checkBlind(Member member) {
public static boolean checkBlind(Member member) {
if (member.getBlind()) {
throw new MemberHandler(ErrorStatus.USER_DEACTIVATED);
}
return false;
}
}

0 comments on commit 443037a

Please sign in to comment.