Skip to content

Commit

Permalink
Merge pull request #131 from HandTris/#130
Browse files Browse the repository at this point in the history
  • Loading branch information
thun0514 authored Jul 14, 2024
2 parents 62871d8 + 672de52 commit 15bbe5b
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.Transactional;
import jungle.HandTris.application.service.AuthService;
import jungle.HandTris.application.service.BCryptPasswordService;
import jungle.HandTris.application.service.MemberService;
import jungle.HandTris.domain.Member;
import jungle.HandTris.domain.MemberRecord;
import jungle.HandTris.domain.exception.*;
Expand All @@ -21,7 +21,7 @@
@Service
@RequiredArgsConstructor
@Transactional
public class MemberServiceImpl implements MemberService {
public class AuthServiceImpl implements AuthService {
private final MemberRepository memberRepository;
private final MemberRecordRepository memberRecordRepository;
private final BCryptPasswordService bCryptPasswordService;
Expand All @@ -30,7 +30,7 @@ public class MemberServiceImpl implements MemberService {
private String defaultImage;

@Override
public Pair<Member, String> signin (MemberRequest memberRequest) {
public Pair<Member, String> signin(MemberRequest memberRequest) {
String username = memberRequest.username();
String password = memberRequest.password();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import jungle.HandTris.domain.repo.MemberRepository;
import jungle.HandTris.global.jwt.JWTUtil;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberDetailRes;
import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

Expand Down Expand Up @@ -46,50 +44,14 @@ public Pair<String, MemberRecordDetailRes> getMemberProfileWithStatsByNickname(S
}

@Override
public MemberDetailRes loadMemberProfileByToken(HttpServletRequest request) {
String accessToken = request.getHeader("Authorization").substring(7);

String nickname = jwtUtil.getNickname(accessToken);

Member member = memberRepository.findByNickname(nickname)
.orElseThrow(UserNotFoundException::new);

String username = member.getUsername();
return new MemberDetailRes(username, nickname);
}

@Override
public Pair<MemberProfileDetailsRes, MemberRecordDetailRes> myPage(HttpServletRequest request, String username) {
String token = jwtUtil.resolveAccessToken(request);
String nickname = jwtUtil.getNickname(token);
Member member = memberRepository.findByNickname(nickname)
.orElseThrow(MemberNotFoundException::new);

// ์š”์ฒญํ•œ ์œ ์ €์™€ ํ† ํฐ์˜ ์ฃผ์ธ์ด ๊ฐ™์€์ง€ ๊ฒ€์ฆ
if (!username.equals(member.getUsername())) {
throw new UnauthorizedAccessException();
}

MemberProfileDetailsRes memberInfoDetails = new MemberProfileDetailsRes(member.getNickname(), member.getProfileImageUrl());
MemberRecordDetailRes memberRecordDetails = new MemberRecordDetailRes(memberRecordService.getMemberRecord(member.getNickname()));

return Pair.of(memberInfoDetails, memberRecordDetails);
}


@Override
public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username) {
public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage) {

Boolean nicknameChanged = false;
String token = jwtUtil.resolveAccessToken(request);
String nickname = jwtUtil.getNickname(token);
Member member = memberRepository.findByNickname(nickname)
.orElseThrow(MemberNotFoundException::new);

// ์š”์ฒญํ•œ ์œ ์ €์™€ ํ† ํฐ์˜ ์ฃผ์ธ์ด ๊ฐ™์€์ง€ ๊ฒ€์ฆ
if (!username.equals(member.getUsername())) {
throw new UnauthorizedAccessException();
}

// ๋ณ€๊ฒฝํ•  ๋‹‰๋„ค์ž„์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ
if (memberUpdateReq.nickname() != null && !memberUpdateReq.nickname().isEmpty()) {
Expand All @@ -110,7 +72,7 @@ public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest requ
if (deleteProfileImage) {
member.updateProfileImageUrl(defaultImage);
} else if (profileImage != null && profileImage.getSize() > 0) {
// ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ
// ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ
validateImage(profileImage);
uploadImage(profileImage, member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import jungle.HandTris.presentation.dto.request.MemberRequest;
import org.springframework.data.util.Pair;

public interface MemberService {
public interface AuthService {
Pair<Member, String> signin(MemberRequest memberRequest);

void signup(MemberRequest memberRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Component
public class ExpirationListener implements MessageListener {

private final MemberService memberService;
private final AuthService authService;

@Override
public void onMessage(Message message, byte[] pattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import jakarta.servlet.http.HttpServletRequest;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberDetailRes;
import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import org.springframework.data.util.Pair;
Expand All @@ -12,9 +10,5 @@
public interface MemberProfileService {
Pair<String, MemberRecordDetailRes> getMemberProfileWithStatsByNickname(String nickname);

MemberDetailRes loadMemberProfileByToken(HttpServletRequest request);

Pair<MemberProfileDetailsRes, MemberRecordDetailRes> myPage(HttpServletRequest request, String username);

MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage, String username);
MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage);
}
10 changes: 5 additions & 5 deletions src/main/java/jungle/HandTris/presentation/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jungle.HandTris.application.service.MemberService;
import jungle.HandTris.application.service.AuthService;
import jungle.HandTris.domain.Member;
import jungle.HandTris.global.dto.ResponseEnvelope;
import jungle.HandTris.presentation.dto.request.MemberRequest;
Expand All @@ -17,19 +17,19 @@
@RequiredArgsConstructor
public class AuthController {

private final MemberService memberService;
private final AuthService authService;

@PostMapping("/signup")
@ResponseStatus(HttpStatus.CREATED)
public ResponseEnvelope<String> signup(@RequestBody @Valid MemberRequest memberRequest) {
memberService.signup(memberRequest);
authService.signup(memberRequest);

return ResponseEnvelope.of("Signup Successful");
}

@PostMapping("/signin")
public ResponseEnvelope<MemberDetailResWithTokenRes> signin(@RequestBody MemberRequest memberRequest) {
Pair<Member, String> result = memberService.signin(memberRequest);
Pair<Member, String> result = authService.signin(memberRequest);

Member member = result.getFirst();
String accessToken = result.getSecond();
Expand All @@ -46,7 +46,7 @@ public ResponseEnvelope<MemberDetailResWithTokenRes> signin(@RequestBody MemberR

@PostMapping("/signout")
public ResponseEnvelope<String> signout(HttpServletRequest request) {
memberService.signout(request);
authService.signout(request);

return ResponseEnvelope.of("Signout Successful");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,40 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jungle.HandTris.application.service.MemberProfileService;
import jungle.HandTris.application.service.MemberRecordService;
import jungle.HandTris.domain.MemberRecord;
import jungle.HandTris.global.dto.ResponseEnvelope;
import jungle.HandTris.global.validation.UserNicknameFromJwt;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberProfileDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberProfileRes;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import lombok.RequiredArgsConstructor;
import org.springframework.data.util.Pair;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/member")
public class MemberController {
public class MemberProfileController {
private final MemberProfileService memberProfileService;
private final MemberRecordService memberRecordService;

@GetMapping("/{username}/mypage")
public ResponseEnvelope<Pair<MemberProfileDetailsRes, MemberRecordDetailRes>> myPage(HttpServletRequest request, @PathVariable("username") String username) {
Pair<MemberProfileDetailsRes, MemberRecordDetailRes> result = memberProfileService.myPage(request, username);

return ResponseEnvelope.of(result);
@GetMapping("/mypage")
public ResponseEnvelope<MemberProfileRes> myPage(@UserNicknameFromJwt String nickname) {
MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
return ResponseEnvelope.of(memberProfileRes);
}

@PatchMapping("/{username}/mypage")
@PatchMapping("/mypage")
public ResponseEnvelope<MemberProfileUpdateDetailsRes> updateInfo(
HttpServletRequest request,
@Valid MemberUpdateReq memberUpdateReq,
@RequestPart(value = "profileImage", required = false) MultipartFile profileImage,
@RequestParam(value = "deleteProfileImage", required = false, defaultValue = "false") boolean deleteProfileImage,
@PathVariable("username") String username) {
@RequestParam(value = "deleteProfileImage", required = false, defaultValue = "false") boolean deleteProfileImage
) {

MemberProfileUpdateDetailsRes updateMemberDetails = memberProfileService.updateMemberProfile(request, memberUpdateReq, profileImage, deleteProfileImage, username);
MemberProfileUpdateDetailsRes updateMemberDetails = memberProfileService.updateMemberProfile(request, memberUpdateReq, profileImage, deleteProfileImage);

return ResponseEnvelope.of(updateMemberDetails);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import jungle.HandTris.global.dto.ResponseEnvelope;
import jungle.HandTris.global.validation.UserNicknameFromJwt;
import jungle.HandTris.presentation.dto.request.GameResultReq;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import jungle.HandTris.presentation.dto.response.ParticipantRes;
import jungle.HandTris.presentation.dto.response.MemberProfileRes;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -22,37 +21,37 @@ public class MemberRecordController {

private final MemberRecordService memberRecordService;

@GetMapping("/{nickname}")
public ResponseEnvelope<MemberRecordDetailRes> getMemberRecord(@PathVariable("nickname") String nickname) {
@GetMapping
public ResponseEnvelope<MemberProfileRes> getMyRecord(@UserNicknameFromJwt String nickname) {
MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname);
MemberRecordDetailRes memberRecordDetailRes = new MemberRecordDetailRes(memberRecord);
return ResponseEnvelope.of(memberRecordDetailRes);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
return ResponseEnvelope.of(memberProfileRes);
}

@PutMapping
public ResponseEnvelope<MemberRecordDetailRes> updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) {
public ResponseEnvelope<MemberProfileRes> updateMemberRecord(@Valid @RequestBody GameResultReq gameResultReq, @UserNicknameFromJwt String nickname) {
MemberRecord memberRecord = memberRecordService.updateMemberRecord(gameResultReq, nickname);
MemberRecordDetailRes memberRecordDetailRes = new MemberRecordDetailRes(memberRecord);
return ResponseEnvelope.of(memberRecordDetailRes);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
return ResponseEnvelope.of(memberProfileRes);
}

@GetMapping("/{nickname}")
public ResponseEnvelope<MemberProfileRes> getMemberRecord(@PathVariable("nickname") String nickname) {
MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
return ResponseEnvelope.of(memberProfileRes);
}

@GetMapping("/participant/{roomCode}")
public ResponseEnvelope<List<ParticipantRes>> getParticipants(@PathVariable("roomCode") String roomCode, @UserNicknameFromJwt String nickname) {
public ResponseEnvelope<List<MemberProfileRes>> getParticipants(@PathVariable("roomCode") String roomCode, @UserNicknameFromJwt String nickname) {
List<MemberRecord> participantsMemberRecord = memberRecordService.getParticipants(roomCode, nickname);
List<ParticipantRes> participantResList = new ArrayList<>();
List<MemberProfileRes> memberProfileResList = new ArrayList<>();
for (MemberRecord memberRecord : participantsMemberRecord) {
if (memberRecord == null)
continue;
ParticipantRes participantRes = new ParticipantRes(memberRecord);
participantResList.add(participantRes);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
memberProfileResList.add(memberProfileRes);
}
return ResponseEnvelope.of(participantResList);
}

@GetMapping
public ResponseEnvelope<ParticipantRes> getMyRecord(@UserNicknameFromJwt String nickname) {
MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname);
ParticipantRes participantRes = new ParticipantRes(memberRecord);
return ResponseEnvelope.of(participantRes);
return ResponseEnvelope.of(memberProfileResList);
}
}
16 changes: 10 additions & 6 deletions src/main/java/jungle/HandTris/presentation/TetrisController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jungle.HandTris.application.service.GameRoomService;
import jungle.HandTris.application.service.TetrisService;
import jungle.HandTris.domain.GameRoom;
import jungle.HandTris.presentation.dto.request.RoomStateReq;
import jungle.HandTris.presentation.dto.request.TetrisMessageReq;
import jungle.HandTris.presentation.dto.response.RoomOwnerRes;
Expand Down Expand Up @@ -54,6 +55,7 @@ public void roomOwnerInfo(@DestinationVariable("roomCode") String roomCode, Simp
@MessageMapping("/{roomCode}/disconnect")
public void handleDisconnect(SimpMessageHeaderAccessor headerAccessor, @DestinationVariable(value = "roomCode") String roomCode) {
System.out.println("\n========================================= controller disconnect send =========================================");

// playing ์ค‘์ธ ๊ฒŒ์ž„์—์„œ ํƒˆ์ฃผํ•œ ๊ฒฝ์šฐ
// message์—์„œ isStart ํ™•์ธ
boolean isStart = headerAccessor.getFirstNativeHeader("isStart").equals("true");
Expand All @@ -68,13 +70,15 @@ public void handleDisconnect(SimpMessageHeaderAccessor headerAccessor, @Destinat
// DB ์ตœ์‹ ํ™”
System.out.println("DB ์ตœ์‹ ํ™”");
String user = headerAccessor.getHeader("User").toString();
gameRoomService.exitGameRoom(user, roomCode);

// ๋ฐฉ์žฅ ์ตœ์‹ ํ™” : DB ์ตœ์‹ ํ™” ์•„๋ž˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
System.out.println("๋ฐฉ์žฅ ์ตœ์‹ ํ™”");
RoomOwnerRes roomOwnerRes = tetrisService.checkRoomOwnerAndReady(roomCode);
messagingTemplate.convertAndSend("/topic/owner/" + roomCode, roomOwnerRes);
GameRoom gameRoom = gameRoomService.exitGameRoom(user, roomCode);

// ์ฐธ์—ฌ์ž๊ฐ€ ๋‚˜๊ฐ”์ง€๋งŒ ๋ˆ„๊ตฐ๊ฐ€ ๋‚จ์•„ ์žˆ๋‹ค๋ฉด
if (gameRoom.getParticipantCount() != 0) {
// ๋ฐฉ์žฅ ์ตœ์‹ ํ™” : DB ์ตœ์‹ ํ™” ์•„๋ž˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
System.out.println("๋ฐฉ์žฅ ์ตœ์‹ ํ™”");
RoomOwnerRes roomOwnerRes = tetrisService.checkRoomOwnerAndReady(roomCode);
messagingTemplate.convertAndSend("/topic/owner/" + roomCode, roomOwnerRes);
}

System.out.println("\n========================================= disconnect send ์ข…๋ฃŒ =========================================");
}
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/jungle/HandTris/presentation/TokenController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import java.math.BigDecimal;

public record ParticipantRes(
public record MemberProfileRes(
String nickname,
String profileImageUrl,
long win,
long lose,
BigDecimal winRate
) {
public ParticipantRes(MemberRecord memberRecord) {
public MemberProfileRes(MemberRecord memberRecord) {
this(
memberRecord.getMember().getNickname(),
memberRecord.getMember().getProfileImageUrl(),
Expand Down
Loading

0 comments on commit 15bbe5b

Please sign in to comment.