From 4da6ef705b750ac2cd1fd25c5b61febf9a4b7e9b Mon Sep 17 00:00:00 2001 From: kwonssshyeon Date: Sun, 24 Dec 2023 05:46:40 +0900 Subject: [PATCH] =?UTF-8?q?fear:=20account=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=EC=82=AC=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit authenticate 체크 함수 적용 프로필 이미지 변경 막아두기 메세지 작성 dto 수정 --- .../controller/AccountController.java | 11 +++++++---- .../controller/MessageController.java | 7 ++----- .../example/HappyNewHere/domain/Account.java | 4 ++-- .../example/HappyNewHere/dto/MessageDto.java | 18 +++++++++++++----- .../HappyNewHere/service/AccountService.java | 2 +- .../HappyNewHere/service/CalenderService.java | 3 +-- .../HappyNewHere/service/MessageService.java | 2 +- .../HappyNewHere/utils/AuthenticateUtils.java | 5 +---- 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/example/HappyNewHere/controller/AccountController.java b/src/main/java/com/example/HappyNewHere/controller/AccountController.java index b4f6602..6e7c72b 100644 --- a/src/main/java/com/example/HappyNewHere/controller/AccountController.java +++ b/src/main/java/com/example/HappyNewHere/controller/AccountController.java @@ -6,6 +6,7 @@ import com.example.HappyNewHere.dto.request.AccountRequestDto; import com.example.HappyNewHere.service.AccountService; import com.example.HappyNewHere.service.KakaoService; +import com.example.HappyNewHere.utils.AuthenticateUtils; import com.example.HappyNewHere.utils.JwtUtils; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -21,7 +22,7 @@ @RequiredArgsConstructor @CrossOrigin public class AccountController { - + private final AuthenticateUtils authenticateUtils; private final AccountService accountService; private final KakaoService kakaoService; @@ -39,7 +40,8 @@ public ResponseEntity login( @GetMapping("/userInfo") public ResponseEntity personalInfo(Authentication authentication ) { - return ResponseEntity.ok().body(accountService.findAccount(Long.parseLong(authentication.getName()))); + Long accountId = authenticateUtils.getLongId(authentication); + return ResponseEntity.ok().body(accountService.findAccount(accountId)); } // 유저 id 뿐만 아니라 발급한 토큰 또한 필요함. @@ -49,8 +51,9 @@ public ResponseEntity personalInfo( @RequestParam String userId, @RequestParam String stateMsg ) { - accountService.addUserId(Long.parseLong(authentication.getName()), userId); - accountService.addStateMsg(Long.parseLong(authentication.getName()), stateMsg); + Long accountId = authenticateUtils.getLongId(authentication); + accountService.addUserId(accountId, userId); + accountService.addStateMsg(accountId, stateMsg); return ResponseEntity.ok().build(); } diff --git a/src/main/java/com/example/HappyNewHere/controller/MessageController.java b/src/main/java/com/example/HappyNewHere/controller/MessageController.java index 90e7674..176dcda 100644 --- a/src/main/java/com/example/HappyNewHere/controller/MessageController.java +++ b/src/main/java/com/example/HappyNewHere/controller/MessageController.java @@ -6,21 +6,18 @@ import com.example.HappyNewHere.utils.AuthenticateUtils; import java.util.List; +import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value="/message") @CrossOrigin +@RequiredArgsConstructor public class MessageController { private final MessageService messageService; private final AuthenticateUtils authenticateUtils; - // 권한들 넣어주셈요 - public MessageController(MessageService messageService, AuthenticateUtils authenticateUtils) { - this.messageService = messageService; - this.authenticateUtils = authenticateUtils; - } @PostMapping("/create") public MessageDto createMessage( diff --git a/src/main/java/com/example/HappyNewHere/domain/Account.java b/src/main/java/com/example/HappyNewHere/domain/Account.java index 1f32752..a66065e 100644 --- a/src/main/java/com/example/HappyNewHere/domain/Account.java +++ b/src/main/java/com/example/HappyNewHere/domain/Account.java @@ -28,8 +28,8 @@ public Account(Long accountId, String nickname, String userId, String profileImg this.stateMsg = stateMsg; } - public static Account of(Long accountId, AccountRequestDto accountRequestDto) { - return new Account(accountId, accountRequestDto.nickname(), accountRequestDto.userId(), accountRequestDto.profileImg(), accountRequestDto.stateMsg()); + public static Account of(Long accountId, AccountRequestDto accountRequestDto, String profileImg) { + return new Account(accountId, accountRequestDto.nickname(), accountRequestDto.userId(), profileImg, accountRequestDto.stateMsg()); } public Account(Long accountId) { diff --git a/src/main/java/com/example/HappyNewHere/dto/MessageDto.java b/src/main/java/com/example/HappyNewHere/dto/MessageDto.java index 83f3fcc..ad70535 100644 --- a/src/main/java/com/example/HappyNewHere/dto/MessageDto.java +++ b/src/main/java/com/example/HappyNewHere/dto/MessageDto.java @@ -11,12 +11,12 @@ public class MessageDto { private Long messageId; private LocalDateTime createdDate; private String context; - private Long sender; // kakaoI - private Long receiver; // + private String sender; + private String receiver; private int paperNum; private boolean anonymous; - public MessageDto(Long messageId, LocalDateTime createdDate, String context, Long sender, Long receiver, + public MessageDto(Long messageId, LocalDateTime createdDate, String context, String sender, String receiver, int paperNum, boolean anonymous) { this.messageId = messageId; this.createdDate = createdDate; @@ -27,7 +27,15 @@ public MessageDto(Long messageId, LocalDateTime createdDate, String context, Lon this.anonymous = anonymous; } - public static MessageDto from(Messages messages) { - return new MessageDto(messages.getMessageId(), messages.getCreatedDate(), messages.getContext(), messages.getSender(), messages.getReceiver(), messages.getPaperNum(), messages.isAnonymous()); + public static MessageDto from(Messages messages,String sender,String receiver) { + return new MessageDto( + messages.getMessageId(), + messages.getCreatedDate(), + messages.getContext(), + sender, + receiver, + messages.getPaperNum(), + messages.isAnonymous() + ); } } \ No newline at end of file diff --git a/src/main/java/com/example/HappyNewHere/service/AccountService.java b/src/main/java/com/example/HappyNewHere/service/AccountService.java index a68e2ca..d28c687 100644 --- a/src/main/java/com/example/HappyNewHere/service/AccountService.java +++ b/src/main/java/com/example/HappyNewHere/service/AccountService.java @@ -110,7 +110,7 @@ public AccountDto updateAccount(Long accountId, AccountRequestDto accountRequest if(!checkUserId(accountRequestDto.userId(),account.get())) throw new HappyException(ErrorCode.DUPLICATED_USER_NAME); - return AccountDto.from(accountRepository.save(Account.of(accountId, accountRequestDto))); + return AccountDto.from(accountRepository.save(Account.of(accountId, accountRequestDto, account.get().getProfileImg()))); } //수정될userId 중복체크 diff --git a/src/main/java/com/example/HappyNewHere/service/CalenderService.java b/src/main/java/com/example/HappyNewHere/service/CalenderService.java index e9a9921..ba25027 100644 --- a/src/main/java/com/example/HappyNewHere/service/CalenderService.java +++ b/src/main/java/com/example/HappyNewHere/service/CalenderService.java @@ -44,9 +44,8 @@ public CalenderDto showCalender(Long accountId, String userId){ else calenderDto.setOwner(false); //3. 메세지 목록 담기(일치하는 멤버가 아니거나 25일이 아니면 null로 두기) - LocalDateTime now = LocalDateTime.now(); if(calenderDto.isOwner() - // && now.getYear()==2024 + // && LocalDateTime.now().getYear()==2024 ){ List messages = messageRepository.findByReceiver(viewer.get().getAccountId()); for(Messages msg : messages){ diff --git a/src/main/java/com/example/HappyNewHere/service/MessageService.java b/src/main/java/com/example/HappyNewHere/service/MessageService.java index cf96334..5495d8e 100644 --- a/src/main/java/com/example/HappyNewHere/service/MessageService.java +++ b/src/main/java/com/example/HappyNewHere/service/MessageService.java @@ -42,7 +42,7 @@ public MessageDto createMessage(String userId, MessageRequestDto messageRequestD messageRequestDto.isAnonymous() ); messageRepository.save(messages); - return MessageDto.from(messages); + return MessageDto.from(messages,userId,messageRequestDto.getReceiver()); } // 내가 받은 메세지들 가져오기 (Read) diff --git a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java index 2fbb553..a74ae74 100644 --- a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java +++ b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java @@ -5,11 +5,8 @@ import com.example.HappyNewHere.exception.HappyException; import com.example.HappyNewHere.repository.AccountRepository; import java.util.Optional; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; -import org.springframework.security.core.Transient; -import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Service @@ -30,7 +27,7 @@ public String getUserId(Authentication authentication){ public Long getLongId(Authentication authentication){ if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND); Long accountId = Long.parseLong(authentication.getName()); - System.out.println(accountId); + return accountId; }