Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#130 #131

Merged
merged 7 commits into from
Jul 14, 2024
Merged

#130 #131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading