Skip to content

Commit

Permalink
✅ 팔로우 요청 DTO 변환 및 반환, 토큰 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdals4716 committed Aug 5, 2024
1 parent dcd4832 commit 2a50039
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.annotation.AuthenticationPrincipal;


import java.util.List;

Expand All @@ -27,10 +30,14 @@ public class FollowController {
private FollowRequestRepository followRequestRepository;

@PostMapping("/request")
public ResponseEntity<?> sendFollowRequest(@RequestBody FollowRequestDTO followRequestDTO) {
public ResponseEntity<?> sendFollowRequest(@RequestBody FollowRequestDTO followRequestDTO, @AuthenticationPrincipal UserDetails userDetails) {
try {
FollowRequest request = followService.sendFollowRequest(followRequestDTO.getSenderId(), followRequestDTO.getReceiverId());
return ResponseEntity.ok(request);
//DTO에서 수신인 정보를 추출하고 인증된 사용자 정보를 추출
FollowRequest request = followService.sendFollowRequest(followRequestDTO.getReceiverId(), userDetails);
//생성된 엔티티를 DTO 클래스 entityToDto 메서드로 DTO 변환
FollowRequestDTO responseDTO = FollowRequestDTO.entityToDto(request);
//변환된 DTO 값 반환
return ResponseEntity.ok(responseDTO);
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
package com.example.MangoWafflee.Follow.DTO;

import com.example.MangoWafflee.DTO.UserDTO;
import com.example.MangoWafflee.Entity.UserEntity;
import com.example.MangoWafflee.Follow.Entity.FollowRequest;
import com.example.MangoWafflee.Follow.Entity.FollowRequestStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FollowRequestDTO {
private Long id;
private Long senderId;
private Long receiverId;
private FollowRequestStatus status;
private LocalDateTime requestDate;

//엔티티를 직접 반환하게 하지않고 필요한 데이터만 포함한 객체를 반환하도록 DTO를 사용
public static FollowRequestDTO entityToDto(FollowRequest followRequest) {
return new FollowRequestDTO(
followRequest.getId(),
followRequest.getSender().getId(),
followRequest.getReceiver().getId(),
followRequest.getStatus(),
followRequest.getRequestDate()
);
}

public FollowRequest dtoToEntity(UserEntity sender, UserEntity receiver) {
return new FollowRequest(id, sender, receiver, status, requestDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
Expand All @@ -28,7 +29,12 @@ public class FollowService {
@Autowired
private UserRepository userRepository;

public FollowRequest sendFollowRequest(Long senderId, Long receiverId) { // 팔로우 요청
public FollowRequest sendFollowRequest(Long receiverId, UserDetails userDetails) {
//UserDetails에서 UserEntity로 끌고옴
UserEntity sender = (UserEntity) userDetails;
//인증된 사용자 정보 추출
Long senderId = sender.getId();

if (senderId.equals(receiverId)) {
throw new IllegalArgumentException("자기 자신에게 팔로우 요청을 보낼 수 없습니다.");
}
Expand All @@ -39,8 +45,8 @@ public FollowRequest sendFollowRequest(Long senderId, Long receiverId) { // 팔
throw new RuntimeException("이 분은 당신의 친구입니다...");
}

UserEntity sender = userRepository.findById(senderId).orElseThrow(() -> new RuntimeException("Sender not found"));
UserEntity receiver = userRepository.findById(receiverId).orElseThrow(() -> new RuntimeException("Receiver not found"));
UserEntity receiver = userRepository.findById(receiverId)
.orElseThrow(() -> new RuntimeException("Receiver not found"));

FollowRequest request = new FollowRequest();
request.setSender(sender);
Expand Down

0 comments on commit 2a50039

Please sign in to comment.