From f476ff28031052f11da19e6d7614322cac13a8db Mon Sep 17 00:00:00 2001 From: kwonssshyeon Date: Fri, 22 Dec 2023 18:22:06 +0900 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=EC=BA=98=EB=A6=B0=EB=8D=94=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EB=82=A0=EC=A7=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/HappyNewHere/service/CalenderService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/example/HappyNewHere/service/CalenderService.java b/src/main/java/com/example/HappyNewHere/service/CalenderService.java index a0c1900..d39bdb2 100644 --- a/src/main/java/com/example/HappyNewHere/service/CalenderService.java +++ b/src/main/java/com/example/HappyNewHere/service/CalenderService.java @@ -42,8 +42,7 @@ public CalenderDto showCalender(Long accountId, String userId){ //3. 메세지 목록 담기(일치하는 멤버가 아니거나 25일이 아니면 null로 두기) LocalDateTime now = LocalDateTime.now(); if(calenderDto.isOwner() - && now.getMonthValue()==12 - && now.getDayOfMonth()==25){ + && now.getYear()==2024){ List messages = messageRepository.findByReceiver(viewer.get().getUserId()); calenderDto.setMessagesList(messages); } From f25ff50ca5f61203e90f9b082ea14de562a5dae4 Mon Sep 17 00:00:00 2001 From: rnjs5540 Date: Fri, 22 Dec 2023 20:21:57 +0900 Subject: [PATCH 2/6] Refactoring: message?? --- .../controller/MessageController.java | 15 +++++++-- .../example/HappyNewHere/domain/Messages.java | 15 +++++---- .../example/HappyNewHere/dto/MessageDto.java | 14 ++++---- .../dto/request/MessageRequestDto.java | 32 +++++++++++++++++++ .../HappyNewHere/service/MessageService.java | 13 ++++++-- .../HappyNewHere/utils/AuthenticateUtils.java | 29 +++++++++++++++++ 6 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java create mode 100644 src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java diff --git a/src/main/java/com/example/HappyNewHere/controller/MessageController.java b/src/main/java/com/example/HappyNewHere/controller/MessageController.java index a818cad..f03a548 100644 --- a/src/main/java/com/example/HappyNewHere/controller/MessageController.java +++ b/src/main/java/com/example/HappyNewHere/controller/MessageController.java @@ -1,9 +1,12 @@ package com.example.HappyNewHere.controller; import com.example.HappyNewHere.dto.MessageDto; +import com.example.HappyNewHere.dto.request.MessageRequestDto; import com.example.HappyNewHere.service.MessageService; +import com.example.HappyNewHere.utils.AuthenticateUtils; import java.util.List; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @RestController @@ -11,15 +14,21 @@ @CrossOrigin public class MessageController { private final MessageService messageService; + private final AuthenticateUtils authenticateUtils; // 권한들 넣어주셈요 - public MessageController(MessageService messageService) { + public MessageController(MessageService messageService, AuthenticateUtils authenticateUtils) { this.messageService = messageService; + this.authenticateUtils = authenticateUtils; } @PostMapping("/create") - public MessageDto createMessage(@RequestBody MessageDto messageDto) { - return messageService.createMessage(messageDto); + public MessageDto createMessage( + Authentication authentication, + @RequestBody MessageRequestDto messageRequestDto) + { + String userId = authenticateUtils.getAccount(authentication); + return messageService.createMessage(userId,messageRequestDto); } @GetMapping("/read/{userId}") diff --git a/src/main/java/com/example/HappyNewHere/domain/Messages.java b/src/main/java/com/example/HappyNewHere/domain/Messages.java index b90a7fa..080eea6 100644 --- a/src/main/java/com/example/HappyNewHere/domain/Messages.java +++ b/src/main/java/com/example/HappyNewHere/domain/Messages.java @@ -1,6 +1,8 @@ package com.example.HappyNewHere.domain; import com.example.HappyNewHere.dto.MessageDto; +import com.example.HappyNewHere.dto.request.AccountRequestDto; +import com.example.HappyNewHere.dto.request.MessageRequestDto; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -21,8 +23,8 @@ public class Messages { private String context; private String sender; private String receiver; - private int decorate; - private boolean isHidden; + private int paperNum; + private boolean anonymous; // dto -> entity public Messages(MessageDto messageDto) { @@ -31,10 +33,11 @@ public Messages(MessageDto messageDto) { this.context = messageDto.getContext(); this.sender = messageDto.getSender(); this.receiver = messageDto.getReceiver(); - this.decorate = messageDto.getDecorate(); - this.isHidden = messageDto.isHidden(); // 왜 바로접근가능?? + this.paperNum = messageDto.getPaperNum(); + this.anonymous = messageDto.isAnonymous(); // 왜 바로접근가능?? } + // patch로 변경 ..? public void update(MessageDto messageDto) { this.messageId = messageDto.getMessageId(); @@ -42,7 +45,7 @@ public void update(MessageDto messageDto) { this.context = messageDto.getContext(); this.sender = messageDto.getSender(); this.receiver = messageDto.getReceiver(); - this.decorate = messageDto.getDecorate(); - this.isHidden = messageDto.isHidden(); + this.paperNum = messageDto.getPaperNum(); + this.anonymous = messageDto.isAnonymous(); } } diff --git a/src/main/java/com/example/HappyNewHere/dto/MessageDto.java b/src/main/java/com/example/HappyNewHere/dto/MessageDto.java index 6b2820e..aa9291f 100644 --- a/src/main/java/com/example/HappyNewHere/dto/MessageDto.java +++ b/src/main/java/com/example/HappyNewHere/dto/MessageDto.java @@ -10,8 +10,8 @@ public class MessageDto { private String context; private String sender; private String receiver; - private int decorate; - private boolean isHidden; + private int paperNum; + private boolean anonymous; // entity -> dto public MessageDto(Messages messages) { @@ -20,18 +20,18 @@ public MessageDto(Messages messages) { this.context = messages.getContext(); this.sender = messages.getSender(); this.receiver = messages.getReceiver(); - this.decorate = messages.getDecorate(); - this.isHidden = messages.isHidden(); + this.paperNum = messages.getPaperNum(); + this.anonymous = messages.isAnonymous(); } public MessageDto(Long messageId, LocalDateTime createdDate, String context, String sender, String receiver, - int decorate, boolean isHidden) { + int paperNum, boolean anonymous) { this.messageId = messageId; this.createdDate = createdDate; this.context = context; this.sender = sender; this.receiver = receiver; - this.decorate = decorate; - this.isHidden = isHidden; + this.paperNum = paperNum; + this.anonymous = anonymous; } } \ No newline at end of file diff --git a/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java b/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java new file mode 100644 index 0000000..e900d7e --- /dev/null +++ b/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java @@ -0,0 +1,32 @@ +package com.example.HappyNewHere.dto.request; + +import com.example.HappyNewHere.domain.Messages; +import java.time.LocalDateTime; +import lombok.Data; + +@Data +public class MessageRequestDto { + private Long messageId; + private String context; + private String receiver; + private int paperNum; + private boolean anonymous; + + // entity -> dto + public MessageRequestDto(Messages messages) { + this.messageId = messages.getMessageId(); + this.context = messages.getContext(); + this.receiver = messages.getReceiver(); + this.paperNum = messages.getPaperNum(); + this.anonymous = messages.isAnonymous(); + } + + public MessageRequestDto(Long messageId, String context, String receiver, + int paperNum, boolean anonymous) { + this.messageId = messageId; + this.context = context; + this.receiver = receiver; + this.paperNum = paperNum; + this.anonymous = anonymous; + } +} diff --git a/src/main/java/com/example/HappyNewHere/service/MessageService.java b/src/main/java/com/example/HappyNewHere/service/MessageService.java index 9251cd6..5bad254 100644 --- a/src/main/java/com/example/HappyNewHere/service/MessageService.java +++ b/src/main/java/com/example/HappyNewHere/service/MessageService.java @@ -2,8 +2,10 @@ import com.example.HappyNewHere.domain.Messages; import com.example.HappyNewHere.dto.MessageDto; +import com.example.HappyNewHere.dto.request.MessageRequestDto; import com.example.HappyNewHere.repository.MessageRepository; import jakarta.transaction.Transactional; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; @@ -15,8 +17,14 @@ public class MessageService { private final MessageRepository messageRepository; // 메세지 생성 (Create) - public MessageDto createMessage(MessageDto messageDto) { - Messages messages = new Messages(messageDto); + public MessageDto createMessage(String userId, MessageRequestDto messageRequestDto) { + Messages messages = new Messages(); + messages.setAnonymous(messageRequestDto.isAnonymous()); + messages.setCreatedDate(LocalDateTime.now()); + messages.setContext(messageRequestDto.getContext()); + messages.setReceiver(messages.getReceiver()); + messages.setPaperNum(messageRequestDto.getPaperNum()); + messages.setSender(userId); messageRepository.save(messages); return new MessageDto(messages); } @@ -40,7 +48,6 @@ public List findAllMessages(String receiver) { public MessageDto findOneMessage(Long messageId) { Messages messages = messageRepository.findById(messageId).orElseThrow( () -> new IllegalArgumentException("조회 실패") - // ....? 람다함수 공부좀 ); return new MessageDto(messages); } diff --git a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java new file mode 100644 index 0000000..58aca44 --- /dev/null +++ b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java @@ -0,0 +1,29 @@ +package com.example.HappyNewHere.utils; + +import com.example.HappyNewHere.domain.Account; +import com.example.HappyNewHere.exception.ErrorCode; +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 +@RequiredArgsConstructor +public class AuthenticateUtils { + private final AccountRepository accountRepository; + public String getAccount(Authentication authentication){ + if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND); + Long accountId = Long.parseLong(authentication.getName()); + Optional account = accountRepository.findById(accountId); + if (account.isEmpty()) + throw new HappyException(ErrorCode.USER_NOT_FOUND); + + return account.get().getUserId(); + } + +} From 8bf998beedf6bb3af6d799b8ede27b2b3ce8b32e Mon Sep 17 00:00:00 2001 From: kwonssshyeon Date: Fri, 22 Dec 2023 20:47:23 +0900 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=ED=86=A1?= =?UTF-8?q?=20=EA=B8=B0=EB=B3=B8=20=ED=94=84=EB=A1=9C=ED=95=84=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HappyNewHere/controller/CalenderController.java | 8 +++++--- .../example/HappyNewHere/service/AccountService.java | 11 ++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java index 68b3275..4d3e38e 100644 --- a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java +++ b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java @@ -7,6 +7,7 @@ import lombok.RequiredArgsConstructor; import org.apache.coyote.Response; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @RestController @@ -18,11 +19,12 @@ public class CalenderController { @GetMapping("{userId}") - public ResponseEntity showCalender(@PathVariable("userId") String userId){ + public ResponseEntity showCalender( + Authentication authentication){ + //TODO: 헤더에서 accountId 가져오기 - System.out.println(userId); Long accountId = 12345L; - return ResponseEntity.ok().body(calenderService.showCalender(accountId,userId)); + return ResponseEntity.ok().body(calenderService.showCalender(accountId)); } @PostMapping() diff --git a/src/main/java/com/example/HappyNewHere/service/AccountService.java b/src/main/java/com/example/HappyNewHere/service/AccountService.java index 81223e3..e22cc51 100644 --- a/src/main/java/com/example/HappyNewHere/service/AccountService.java +++ b/src/main/java/com/example/HappyNewHere/service/AccountService.java @@ -68,7 +68,16 @@ public String saveAccount(UserInfo userInfo){ Account account = new Account(); account.setAccountId(userInfo.getId()); account.setNickname(userInfo.getProperties().getNickname()); - account.setProfileImg(userInfo.getProperties().getProfile_image()); + + if (userInfo.getProperties().getProfile_image().equals("http://k.kakaocdn.net/dn/1G9kp/btsAot8liOn/8CWudi3uy07rvFNUkk3ER0/img_640x640.jpg")){ + + account.setProfileImg("https://dang-na-dong.s3.ap-northeast-2.amazonaws.com/Snowman.png"); + } + else{ + account.setProfileImg(userInfo.getProperties().getProfile_image()); + } + + accountRepository.save(account); responseToken = JwtUtils.createJwt(account.getAccountId(), secretKey, expiredMs); From b21d774c085976ff49fa464115198bdf403f9997 Mon Sep 17 00:00:00 2001 From: kwonssshyeon Date: Fri, 22 Dec 2023 21:25:21 +0900 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20authentication=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/HappyNewHere/controller/CalenderController.java | 2 +- .../com/example/HappyNewHere/controller/MessageController.java | 2 +- .../java/com/example/HappyNewHere/utils/AuthenticateUtils.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java index 4d3e38e..f72e30e 100644 --- a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java +++ b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java @@ -24,7 +24,7 @@ public ResponseEntity showCalender( //TODO: 헤더에서 accountId 가져오기 Long accountId = 12345L; - return ResponseEntity.ok().body(calenderService.showCalender(accountId)); + return ResponseEntity.ok().body(calenderService.showCalender(accountId,"111")); } @PostMapping() diff --git a/src/main/java/com/example/HappyNewHere/controller/MessageController.java b/src/main/java/com/example/HappyNewHere/controller/MessageController.java index f03a548..0d7f3ee 100644 --- a/src/main/java/com/example/HappyNewHere/controller/MessageController.java +++ b/src/main/java/com/example/HappyNewHere/controller/MessageController.java @@ -27,7 +27,7 @@ public MessageDto createMessage( Authentication authentication, @RequestBody MessageRequestDto messageRequestDto) { - String userId = authenticateUtils.getAccount(authentication); + String userId = authenticateUtils.getUserId(authentication); return messageService.createMessage(userId,messageRequestDto); } diff --git a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java index 58aca44..1f5edfc 100644 --- a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java +++ b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java @@ -16,7 +16,7 @@ @RequiredArgsConstructor public class AuthenticateUtils { private final AccountRepository accountRepository; - public String getAccount(Authentication authentication){ + public String getUserId(Authentication authentication){ if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND); Long accountId = Long.parseLong(authentication.getName()); Optional account = accountRepository.findById(accountId); From ed8080e0ccf58c234cf01535d4411efe94e52396 Mon Sep 17 00:00:00 2001 From: kwonssshyeon Date: Fri, 22 Dec 2023 21:26:59 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20authentication.getName=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/HappyNewHere/utils/AuthenticateUtils.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java index 1f5edfc..b125004 100644 --- a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java +++ b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java @@ -26,4 +26,11 @@ public String getUserId(Authentication authentication){ return account.get().getUserId(); } + public Long getLongId(Authentication authentication){ + if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND); + Long accountId = Long.parseLong(authentication.getName()); + + return accountId; + } + } From ac28be4cb91cf10d77ab3778cda82cc938e2dfff Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Fri, 22 Dec 2023 23:21:48 +0900 Subject: [PATCH 6/6] =?UTF-8?q?messagesList=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CalenderController.java | 9 +++--- .../example/HappyNewHere/domain/Messages.java | 4 +-- .../example/HappyNewHere/dto/CalenderDto.java | 3 +- .../dto/request/MessageRequestDto.java | 5 +-- .../dto/response/MsgResponseDto.java | 26 ++++++++++++++++ .../HappyNewHere/service/CalenderService.java | 31 +++++++++++-------- .../HappyNewHere/service/MessageService.java | 4 ++- .../HappyNewHere/utils/AuthenticateUtils.java | 3 +- 8 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/example/HappyNewHere/dto/response/MsgResponseDto.java diff --git a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java index f72e30e..9f8194c 100644 --- a/src/main/java/com/example/HappyNewHere/controller/CalenderController.java +++ b/src/main/java/com/example/HappyNewHere/controller/CalenderController.java @@ -2,6 +2,7 @@ import com.example.HappyNewHere.service.CalenderService; +import com.example.HappyNewHere.utils.AuthenticateUtils; import jakarta.persistence.Entity; import lombok.Data; import lombok.RequiredArgsConstructor; @@ -16,20 +17,20 @@ @CrossOrigin public class CalenderController { private final CalenderService calenderService; - + private final AuthenticateUtils authenticateUtils; @GetMapping("{userId}") public ResponseEntity showCalender( + @PathVariable String userId, Authentication authentication){ //TODO: 헤더에서 accountId 가져오기 - Long accountId = 12345L; - return ResponseEntity.ok().body(calenderService.showCalender(accountId,"111")); + Long accountId = authenticateUtils.getLongId(authentication); + return ResponseEntity.ok().body(calenderService.showCalender(accountId,userId)); } @PostMapping() public ResponseEntity updateCalender(){ - return ResponseEntity.ok().body(null); } diff --git a/src/main/java/com/example/HappyNewHere/domain/Messages.java b/src/main/java/com/example/HappyNewHere/domain/Messages.java index 080eea6..e75e47d 100644 --- a/src/main/java/com/example/HappyNewHere/domain/Messages.java +++ b/src/main/java/com/example/HappyNewHere/domain/Messages.java @@ -21,8 +21,8 @@ public class Messages { private Long messageId; private LocalDateTime createdDate; private String context; - private String sender; - private String receiver; + private String sender; // SenderUserId + private String receiver; // ReciverUserId private int paperNum; private boolean anonymous; diff --git a/src/main/java/com/example/HappyNewHere/dto/CalenderDto.java b/src/main/java/com/example/HappyNewHere/dto/CalenderDto.java index ea9632b..98618e8 100644 --- a/src/main/java/com/example/HappyNewHere/dto/CalenderDto.java +++ b/src/main/java/com/example/HappyNewHere/dto/CalenderDto.java @@ -2,6 +2,7 @@ import com.example.HappyNewHere.domain.Calender; import com.example.HappyNewHere.domain.Messages; +import com.example.HappyNewHere.dto.response.MsgResponseDto; import lombok.Data; import java.util.List; @@ -10,5 +11,5 @@ public class CalenderDto { private boolean isOwner; private Calender calender; - private List messagesList; + private List messagesList; } diff --git a/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java b/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java index e900d7e..b3dab3b 100644 --- a/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java +++ b/src/main/java/com/example/HappyNewHere/dto/request/MessageRequestDto.java @@ -6,7 +6,6 @@ @Data public class MessageRequestDto { - private Long messageId; private String context; private String receiver; private int paperNum; @@ -14,16 +13,14 @@ public class MessageRequestDto { // entity -> dto public MessageRequestDto(Messages messages) { - this.messageId = messages.getMessageId(); this.context = messages.getContext(); this.receiver = messages.getReceiver(); this.paperNum = messages.getPaperNum(); this.anonymous = messages.isAnonymous(); } - public MessageRequestDto(Long messageId, String context, String receiver, + public MessageRequestDto(String context, String receiver, int paperNum, boolean anonymous) { - this.messageId = messageId; this.context = context; this.receiver = receiver; this.paperNum = paperNum; diff --git a/src/main/java/com/example/HappyNewHere/dto/response/MsgResponseDto.java b/src/main/java/com/example/HappyNewHere/dto/response/MsgResponseDto.java new file mode 100644 index 0000000..ba15e7b --- /dev/null +++ b/src/main/java/com/example/HappyNewHere/dto/response/MsgResponseDto.java @@ -0,0 +1,26 @@ +package com.example.HappyNewHere.dto.response; + +import com.example.HappyNewHere.domain.Messages; +import com.example.HappyNewHere.dto.MessageDto; + +import java.time.LocalDateTime; + +public record MsgResponseDto( + String context, + String senderId, + String senderNickname, + int paperNum, + boolean anonymous, + LocalDateTime day +) { + public static MsgResponseDto of(Messages messages, String senderNickname){ + return new MsgResponseDto( + messages.getContext(), + messages.getSender(), + senderNickname, + messages.getPaperNum(), + messages.isAnonymous(), + messages.getCreatedDate() + ); + } +} diff --git a/src/main/java/com/example/HappyNewHere/service/CalenderService.java b/src/main/java/com/example/HappyNewHere/service/CalenderService.java index d39bdb2..4ad7012 100644 --- a/src/main/java/com/example/HappyNewHere/service/CalenderService.java +++ b/src/main/java/com/example/HappyNewHere/service/CalenderService.java @@ -4,6 +4,7 @@ import com.example.HappyNewHere.domain.Calender; import com.example.HappyNewHere.domain.Messages; import com.example.HappyNewHere.dto.CalenderDto; +import com.example.HappyNewHere.dto.response.MsgResponseDto; import com.example.HappyNewHere.repository.AccountRepository; import com.example.HappyNewHere.repository.CalenderRepository; import com.example.HappyNewHere.repository.MessageRepository; @@ -11,6 +12,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -23,13 +25,13 @@ public class CalenderService { public CalenderDto showCalender(Long accountId, String userId){ CalenderDto calenderDto = new CalenderDto(); - - //1. 달력스타일 담기 - Optional account = accountRepository.findById(accountId); - if(account.isPresent()){ - Optional calender = calenderRepository.findById(account.get()); - calender.ifPresent(calenderDto::setCalender); - } + calenderDto.setMessagesList(new ArrayList<>()); +// //1. 달력스타일 담기 +// Optional account = accountRepository.findById(accountId); +// if(account.isPresent()){ +// Optional calender = calenderRepository.findById(account.get().getAccountId()); +// calender.ifPresent(calenderDto::setCalender); +// } //2. 일치하는 멤버인지 확인 @@ -42,15 +44,18 @@ public CalenderDto showCalender(Long accountId, String userId){ //3. 메세지 목록 담기(일치하는 멤버가 아니거나 25일이 아니면 null로 두기) LocalDateTime now = LocalDateTime.now(); if(calenderDto.isOwner() - && now.getYear()==2024){ - List messages = messageRepository.findByReceiver(viewer.get().getUserId()); - calenderDto.setMessagesList(messages); + //&& now.getYear()==2024 + ){ + List messages = messageRepository.findByReceiver(userId); + for(Messages msg : messages){ + calenderDto.getMessagesList().add(toMsgDto(msg)); + } } - - return calenderDto; } - + public MsgResponseDto toMsgDto(Messages messages){ + return MsgResponseDto.of(messages,accountRepository.findByUserId(messages.getSender()).get().getNickname()); + } //TODO: 달력스타일 수정 diff --git a/src/main/java/com/example/HappyNewHere/service/MessageService.java b/src/main/java/com/example/HappyNewHere/service/MessageService.java index 5bad254..551c5ed 100644 --- a/src/main/java/com/example/HappyNewHere/service/MessageService.java +++ b/src/main/java/com/example/HappyNewHere/service/MessageService.java @@ -4,6 +4,7 @@ import com.example.HappyNewHere.dto.MessageDto; import com.example.HappyNewHere.dto.request.MessageRequestDto; import com.example.HappyNewHere.repository.MessageRepository; +import com.example.HappyNewHere.utils.AuthenticateUtils; import jakarta.transaction.Transactional; import java.time.LocalDateTime; import java.util.ArrayList; @@ -15,6 +16,7 @@ @RequiredArgsConstructor public class MessageService { private final MessageRepository messageRepository; + private final AuthenticateUtils authenticateUtils; // 메세지 생성 (Create) public MessageDto createMessage(String userId, MessageRequestDto messageRequestDto) { @@ -22,7 +24,7 @@ public MessageDto createMessage(String userId, MessageRequestDto messageRequestD messages.setAnonymous(messageRequestDto.isAnonymous()); messages.setCreatedDate(LocalDateTime.now()); messages.setContext(messageRequestDto.getContext()); - messages.setReceiver(messages.getReceiver()); + messages.setReceiver(messageRequestDto.getReceiver()); messages.setPaperNum(messageRequestDto.getPaperNum()); messages.setSender(userId); messageRepository.save(messages); diff --git a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java index b125004..2fbb553 100644 --- a/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java +++ b/src/main/java/com/example/HappyNewHere/utils/AuthenticateUtils.java @@ -16,6 +16,7 @@ @RequiredArgsConstructor public class AuthenticateUtils { private final AccountRepository accountRepository; + public String getUserId(Authentication authentication){ if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND); Long accountId = Long.parseLong(authentication.getName()); @@ -29,7 +30,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; }