Skip to content

Commit

Permalink
Merge pull request #32 from 9oormthon-univ/feature/GOORM-4-comment
Browse files Browse the repository at this point in the history
Feature/goorm 4 comment
  • Loading branch information
sunny2you authored Nov 23, 2024
2 parents b48b205 + 901709c commit 036b61a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 45 deletions.
32 changes: 14 additions & 18 deletions src/main/java/com/goormthon3/team49/common/auth/CorsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 인증 정보 포함 허용
config.addAllowedOrigin("http://localhost:3000"); // Origin 허용
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("OPTIONS"); // 명시적으로 OPTIONS 메서드 포함
config.addAllowedHeader("Content-Type"); // Content-Type 명시
config.addAllowedHeader("Authorization"); // Authorization 허용

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("Authorization", "Content-Type")
.exposedHeaders("Custom-Header")
.allowCredentials(true)
.maxAge(3600);
}

}



Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class CommentService {
private final UserRepository userRepository;

@Transactional
public CommentResponse createComment(Long product_id, CommentRequest request, User user) {
Comment comment=Comment.create(request.content(),user,product_id);
public CommentResponse createComment(Long product_id, CommentRequest request, String username) {
Comment comment=Comment.create(request.content(),username,product_id);
commentRepository.save(comment);
return CommentResponse.of(comment,user);
return CommentResponse.of(comment);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ public class Comment extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private Long commentId;

@Column(nullable = false, length = 2000)
private String content;

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

@Column(nullable = false)
private Long productId;


public static Comment create(String content, User user, Long product_id) {
public static Comment create(String content, String username, Long product_id) {
return Comment.builder()
.content(content)
.user(user)
.username(username)
.productId(product_id)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public ResponseEntity<CommentResponse> createReview(

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

return ResponseEntity.status(CREATED).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public static CommentListResponse of(Long product_id, Long comment_count, List<C
.product_id(product_id)
.comment_count(comment_count)
.comments(comments.stream()
.map(comment -> CommentResponse.of(comment, comment.getUser())) // User 객체를 제공
.toList())
.map(CommentResponse::of) // CommentResponse.of(comment) -> 이 부분 수정
.toList()) // 끝에 toList()를 제대로 닫기
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ public record CommentResponse(
@DateTimeFormat(pattern="yyyy-MM-dd")
String createdAt
) {
public static CommentResponse of(Comment comment, User user) {
public static CommentResponse of(Comment comment) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return CommentResponse.builder()
.comment_id(comment.getId())
.user_id(user.getUserId())
.username(user.getUserName())
.comment_id(comment.getCommentId())
.username(comment.getUsername())
.content(comment.getContent())
.createdAt(comment.getCreatedAt().format(formatter))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ public static class KakaoAccount {
@JsonProperty("profile")
private Profile profile;

@JsonSetter("profile")
public void setProfile(Profile profile) {
if (profile == null) {
this.profile = new Profile(); // 기본값을 설정 (빈 Profile 객체)
} else {
this.profile = profile;
}
}

@Getter
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down

0 comments on commit 036b61a

Please sign in to comment.