Skip to content

Commit

Permalink
Merge pull request #27 from Happy-New-Here/feature-#27
Browse files Browse the repository at this point in the history
Feature #27
  • Loading branch information
yooonwodyd authored Dec 22, 2023
2 parents 71aa85b + ac28be4 commit df50e38
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@


import com.example.HappyNewHere.service.CalenderService;
import com.example.HappyNewHere.utils.AuthenticateUtils;
import jakarta.persistence.Entity;
import lombok.Data;
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
Expand All @@ -15,19 +17,20 @@
@CrossOrigin
public class CalenderController {
private final CalenderService calenderService;

private final AuthenticateUtils authenticateUtils;

@GetMapping("{userId}")
public ResponseEntity showCalender(@PathVariable("userId") String userId){
public ResponseEntity showCalender(
@PathVariable String userId,
Authentication authentication){

//TODO: 헤더에서 accountId 가져오기
System.out.println(userId);
Long accountId = 12345L;
Long accountId = authenticateUtils.getLongId(authentication);
return ResponseEntity.ok().body(calenderService.showCalender(accountId,userId));
}

@PostMapping()
public ResponseEntity updateCalender(){

return ResponseEntity.ok().body(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
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
@RequestMapping(value="/message")
@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.getUserId(authentication);
return messageService.createMessage(userId,messageRequestDto);
}

@GetMapping("/read/{userId}")
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/example/HappyNewHere/domain/Messages.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,10 +21,10 @@ public class Messages {
private Long messageId;
private LocalDateTime createdDate;
private String context;
private String sender;
private String receiver;
private int decorate;
private boolean isHidden;
private String sender; // SenderUserId
private String receiver; // ReciverUserId
private int paperNum;
private boolean anonymous;

// dto -> entity
public Messages(MessageDto messageDto) {
Expand All @@ -31,18 +33,19 @@ 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();
this.createdDate = messageDto.getCreatedDate();
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();
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/HappyNewHere/dto/CalenderDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,5 +11,5 @@
public class CalenderDto {
private boolean isOwner;
private Calender calender;
private List<Messages> messagesList;
private List<MsgResponseDto> messagesList;
}
14 changes: 7 additions & 7 deletions src/main/java/com/example/HappyNewHere/dto/MessageDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.HappyNewHere.dto.request;

import com.example.HappyNewHere.domain.Messages;
import java.time.LocalDateTime;
import lombok.Data;

@Data
public class MessageRequestDto {
private String context;
private String receiver;
private int paperNum;
private boolean anonymous;

// entity -> dto
public MessageRequestDto(Messages messages) {
this.context = messages.getContext();
this.receiver = messages.getReceiver();
this.paperNum = messages.getPaperNum();
this.anonymous = messages.isAnonymous();
}

public MessageRequestDto(String context, String receiver,
int paperNum, boolean anonymous) {
this.context = context;
this.receiver = receiver;
this.paperNum = paperNum;
this.anonymous = anonymous;
}
}
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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);
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/com/example/HappyNewHere/service/CalenderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -23,13 +25,13 @@ public class CalenderService {

public CalenderDto showCalender(Long accountId, String userId){
CalenderDto calenderDto = new CalenderDto();

//1. 달력스타일 담기
Optional<Account> account = accountRepository.findById(accountId);
if(account.isPresent()){
Optional<Calender> calender = calenderRepository.findById(account.get());
calender.ifPresent(calenderDto::setCalender);
}
calenderDto.setMessagesList(new ArrayList<>());
// //1. 달력스타일 담기
// Optional<Account> account = accountRepository.findById(accountId);
// if(account.isPresent()){
// Optional<Calender> calender = calenderRepository.findById(account.get().getAccountId());
// calender.ifPresent(calenderDto::setCalender);
// }


//2. 일치하는 멤버인지 확인
Expand All @@ -42,16 +44,18 @@ public CalenderDto showCalender(Long accountId, String userId){
//3. 메세지 목록 담기(일치하는 멤버가 아니거나 25일이 아니면 null로 두기)
LocalDateTime now = LocalDateTime.now();
if(calenderDto.isOwner()
&& now.getMonthValue()==12
&& now.getDayOfMonth()==25){
List<Messages> messages = messageRepository.findByReceiver(viewer.get().getUserId());
calenderDto.setMessagesList(messages);
//&& now.getYear()==2024
){
List<Messages> 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: 달력스타일 수정
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/example/HappyNewHere/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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 com.example.HappyNewHere.utils.AuthenticateUtils;
import jakarta.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,10 +16,17 @@
@RequiredArgsConstructor
public class MessageService {
private final MessageRepository messageRepository;
private final AuthenticateUtils authenticateUtils;

// 메세지 생성 (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(messageRequestDto.getReceiver());
messages.setPaperNum(messageRequestDto.getPaperNum());
messages.setSender(userId);
messageRepository.save(messages);
return new MessageDto(messages);
}
Expand All @@ -40,7 +50,6 @@ public List<MessageDto> findAllMessages(String receiver) {
public MessageDto findOneMessage(Long messageId) {
Messages messages = messageRepository.findById(messageId).orElseThrow(
() -> new IllegalArgumentException("조회 실패")
// ....? 람다함수 공부좀
);
return new MessageDto(messages);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 getUserId(Authentication authentication){
if (authentication==null) throw new HappyException(ErrorCode.USER_NOT_FOUND);
Long accountId = Long.parseLong(authentication.getName());
Optional<Account> account = accountRepository.findById(accountId);
if (account.isEmpty())
throw new HappyException(ErrorCode.USER_NOT_FOUND);

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());
System.out.println(accountId);
return accountId;
}

}

0 comments on commit df50e38

Please sign in to comment.