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

[GOORM]-4 : comment와 user 연동 #29

Merged
merged 2 commits into from
Nov 23, 2024
Merged
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 @@ -5,6 +5,9 @@
import com.goormthon3.team49.domain.comment.presentation.request.CommentRequest;
import com.goormthon3.team49.domain.comment.presentation.response.CommentListResponse;
import com.goormthon3.team49.domain.comment.presentation.response.CommentResponse;
import com.goormthon3.team49.domain.user.application.UserLoginService;
import com.goormthon3.team49.domain.user.domain.User;
import com.goormthon3.team49.domain.user.infrastructure.UserRepository;
import lombok.RequiredArgsConstructor;
import org.hibernate.annotations.Comments;
import org.springframework.stereotype.Service;
Expand All @@ -16,14 +19,14 @@
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
private final UserLoginService userLoginService;
private final UserRepository userRepository;

@Transactional
public CommentResponse createComment(Long product_id, CommentRequest request) {
Long user_id = 1L; //UserDetailService 생성되면 로직 업데이트 예정
String username="홍길동"; //UserDetailService 생성되면 로직 업데이트 예정
Comment comment=Comment.create(request.content(),user_id,username,product_id);
commentRepository.save(comment).getId();
return CommentResponse.of(comment);
public CommentResponse createComment(Long product_id, CommentRequest request, User user) {
Comment comment=Comment.create(request.content(),user,product_id);
commentRepository.save(comment);
return CommentResponse.of(comment,user);
}

@Transactional
Expand All @@ -33,5 +36,4 @@ public CommentListResponse getComment(Long product_id) {
return CommentListResponse.of(product_id,comment_count,comments);
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goormthon3.team49.domain.comment.domain;

import com.goormthon3.team49.common.domain.BaseTimeEntity;
import com.goormthon3.team49.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -20,20 +21,18 @@ public class Comment extends BaseTimeEntity {
@Column(nullable = false, length = 2000)
private String content;

@Column(nullable = false)
private Long user_id;

@Column(nullable = false, length = 2000)
private String username;
@ManyToOne
@JoinColumn
private User user;

@Column(nullable = false)
private Long productId;

public static Comment create(String content, Long user_id, String username, Long product_id) {

public static Comment create(String content, User user, Long product_id) {
return Comment.builder()
.content(content)
.user_id(user_id)
.username(username)
.user(user)
.productId(product_id)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.goormthon3.team49.domain.comment.presentation.request.CommentRequest;
import com.goormthon3.team49.domain.comment.presentation.response.CommentListResponse;
import com.goormthon3.team49.domain.comment.presentation.response.CommentResponse;
import com.goormthon3.team49.domain.user.application.UserLoginService;
import com.goormthon3.team49.domain.user.domain.User;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,14 +19,25 @@
@RequestMapping("/comments")
public class CommentController {
private final CommentService commentService;
private final UserLoginService userLoginService;

@ResponseStatus(CREATED)
@PostMapping("/{productId}/createComment")
public ResponseEntity<CommentResponse> createReview(
@PathVariable Long productId,
@RequestBody CommentRequest request
@RequestBody CommentRequest request,
HttpServletRequest httpServletRequest
) {
CommentResponse response = commentService.createComment(productId,request);
String authHeader = httpServletRequest.getHeader("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new IllegalArgumentException("Authorization header is missing or invalid");
}

String token = authHeader.substring(7);
Long kakaouserID=userLoginService.getKakaoUserIdFromAccessToken(token);
User user=userLoginService.findUserByFromKakaoUserId(kakaouserID);
CommentResponse response = commentService.createComment(productId, request, user);

return ResponseEntity.status(CREATED).body(response);
}

Expand All @@ -36,4 +50,19 @@ public ResponseEntity<CommentListResponse> getReview(
return ResponseEntity.status(OK).body(response);
}


@GetMapping("/who")
public String who(
HttpServletRequest httpServletRequest
) {
String authHeader = httpServletRequest.getHeader("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new IllegalArgumentException("Authorization header is missing or invalid");
}

String token = authHeader.substring(7);
Long kakaouserID=userLoginService.getKakaoUserIdFromAccessToken(token);
User user=userLoginService.findUserByFromKakaoUserId(kakaouserID);
return user.getUserName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public record CommentListResponse(
Long comment_count,
List<CommentResponse> comments
) {
public static CommentListResponse of(Long product_id,Long comment_count,List<Comment> comments) {
public static CommentListResponse of(Long product_id, Long comment_count, List<Comment> comments) {
return CommentListResponse.builder()
.product_id(product_id)
.comment_count(comment_count)
.comments(comments.stream()
.map(CommentResponse::of)
.map(comment -> CommentResponse.of(comment, comment.getUser())) // User 객체를 제공
.toList())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goormthon3.team49.domain.comment.presentation.response;

import com.goormthon3.team49.domain.comment.domain.Comment;
import com.goormthon3.team49.domain.user.domain.User;
import lombok.Builder;
import org.springframework.format.annotation.DateTimeFormat;

Expand All @@ -15,12 +16,12 @@ public record CommentResponse(
@DateTimeFormat(pattern="yyyy-MM-dd")
String createdAt
) {
public static CommentResponse of(Comment comment) {
public static CommentResponse of(Comment comment, User user) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return CommentResponse.builder()
.comment_id(comment.getId())
.user_id(comment.getUser_id())
.username(comment.getUsername())
.user_id(user.getUserId())
.username(user.getUserName())
.content(comment.getContent())
.createdAt(comment.getCreatedAt().format(formatter))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.goormthon3.team49.domain.user.application;


import com.goormthon3.team49.domain.user.domain.User;
import com.goormthon3.team49.domain.user.infrastructure.UserRepository;
import com.goormthon3.team49.domain.user.presentation.UserInfoResponseDto;
Expand All @@ -14,6 +15,8 @@
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Optional;

@Service
public class UserLoginService {

Expand Down Expand Up @@ -114,4 +117,13 @@ public Long getKakaoUserIdFromAccessToken(String accessToken) {

return kakaoUserId;
}

public User findUserByFromKakaoUserId(Long kakaoUserId) {
return userRepository.findByKakaoUserId(kakaoUserId)
.orElseThrow(() -> new RuntimeException("User not found with Kakao User ID: " + kakaoUserId));
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class User {

private Boolean activated;



@CreationTimestamp
@Column(updatable = false)
private Timestamp createdAt;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spring:
host: smtp.gmail.com
port: 587
username: ${EMAIL}
password: ${EMAIL_PASSWORD}
password: ${EMAIL_PASSWORD:}
properties:
mail:
smtp:
Expand Down
Loading