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

fix: 충돌 수정 #60

Merged
merged 13 commits into from
Mar 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import core.kobaco.application.comment.service.CommentLikeDetailResponse;
import core.kobaco.application.comment.service.CommentService;
import core.kobaco.application.comment.service.dto.CommentDetail;
import core.kobaco.application.comment.service.dto.CommentCreateRequest;
import core.kobaco.application.comment.service.dto.CommentDetailResponse;
import core.kobaco.global.ApiResponse;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -18,19 +19,18 @@
@RequestMapping("/api/comments")
public class CommentController {
private final CommentService commentService;


@Operation(summary = "댓글 생성")
@PostMapping("/{advertiseId}")
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, @PathVariable Long advertiseId) {
CommentDetail createdComment = commentService.createComment(commentDTO, advertiseId);
public ResponseEntity<CommentCreateRequest> createComment(@RequestBody CommentCreateRequest commentDTO, @PathVariable Long advertiseId) {
CommentCreateRequest createdComment = commentService.createComment(commentDTO.getContent(), advertiseId);
return ResponseEntity.status(HttpStatus.CREATED).body(createdComment);
}


@Operation(summary = "댓글 조회")
@GetMapping("/{advertiseId}")
public ResponseEntity<List<CommentDetail>> getAllComments(@PathVariable Long advertiseId) {
List<CommentDetail> comments = commentService.getAllComments(advertiseId);
public ResponseEntity<List<CommentDetailResponse>> getAllComments(@PathVariable Long advertiseId) {
List<CommentDetailResponse> comments = commentService.getAllComments(advertiseId);
return ApiResponse.success(comments);
}

Expand All @@ -48,3 +48,4 @@ public CommentLikeDetailResponse getCommentLikeCount(@PathVariable Long commentI

}


Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ public static CommentLikeDetailResponse of(Boolean isLike, Long likeCount){
}
}


Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package core.kobaco.application.comment.service;

import core.kobaco.application.comment.service.dto.CommentDetail;
import core.kobaco.application.comment.service.dto.CommentCreateRequest;
import core.kobaco.application.comment.service.dto.CommentDetailResponse;
import core.kobaco.domain.comment.*;

import core.kobaco.domain.commentlike.service.CommentLikeManager;
import core.kobaco.domain.user.UserUtils;

import core.kobaco.domain.user.service.UserReader;
Expand All @@ -14,6 +16,7 @@
import java.util.*;
import java.util.stream.Collectors;


@Service
@Transactional
public class CommentService {
Expand All @@ -30,52 +33,43 @@ public CommentService(CommentRepository commentRepository, UserReader userReader
}

@Transactional
public CommentDetail createComment(CommentDetail commentDetail, Long advertiseId) {
final Long userId = userUtils.getRequestUserId();

if (userId == null) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다.");
public CommentCreateRequest createComment(String content, Long advertiseId) {
final Long userId = userUtils.getRequestUserId();
if (userId == null) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다.");
}
Comment comment = new Comment(null, content, userId);
Comment savedCommentEntity = commentRepository.save(comment, advertiseId);
return CommentCreateRequest.of(savedCommentEntity.getContent());
}

Comment comment = new Comment(
null,
commentDetail.getContent(),
userId,
advertiseId
);

Comment savedCommentEntity = commentRepository.save(comment, advertiseId);

String userEmail = getUserEmail(userId);

return new CommentDetail(
savedCommentEntity.getCommentId(),
savedCommentEntity.getContent(),
userEmail
);
}

public List<CommentDetail> getAllComments(Long advertiseId) {
List<Comment> comments = commentRepository.findAllByAdvertiseId(advertiseId);
return comments.stream()
.map(comment -> new CommentDetail(comment.getCommentId(), comment.getContent(), getUserEmail(comment.getCommenterId())))
.collect(Collectors.toList());
}

private String getUserEmail(Long userId) {
return userReader.read(userId).getEmail();
public List<CommentDetailResponse> getAllComments(Long advertiseId) {
List<Comment> comments = commentRepository.findAllByAdvertiseId(advertiseId);
return comments.stream()
.map(comment -> {
String userEmail = getUserEmail(comment.getCommenterId());
return CommentDetailResponse.of(comment.getCommentId(), comment.getContent(), userEmail);
})
.collect(Collectors.toList());
}

}
private String getUserEmail(Long userId) {
return userReader.read(userId).getEmail();
}

@Transactional
public void likeComment(Long commentId) {
final Long userId = userUtils.getRequestUserId();
commentLikeManager.like(commentId, userId);
}
public CommentLikeDetailResponse getCommentLikeCount(final Long commentId) {
return CommentLikeDetailResponse.of(
commentLikeManager.isLike(commentId),
commentLikeManager.getLikeCount(commentId)
@Transactional
public void likeComment(Long commentId) {
final Long userId = userUtils.getRequestUserId();
if (userId == null) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다.");
}
commentLikeManager.like(commentId, userId);
}
public CommentLikeDetailResponse getCommentLikeCount(final Long commentId) {
return CommentLikeDetailResponse.of(
commentLikeManager.isLike(commentId),
commentLikeManager.getLikeCount(commentId)
);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.kobaco.application.comment.service.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CommentCreateRequest {
private String content;

public CommentCreateRequest(String content) {
this.content = content;
}

public static CommentCreateRequest of(String content) {
return new CommentCreateRequest(content);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.kobaco.application.comment.service.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;


@Getter
@NoArgsConstructor
public class CommentDetailResponse {
private Long commentId;
private String content;
private String userEmail;

public CommentDetailResponse(Long commentId, String content, String userEmail) {
this.commentId = commentId;
this.content = content;
this.userEmail = userEmail;
}

public static CommentDetailResponse of(Long commentId, String content, String userEmail) {
return new CommentDetailResponse(commentId, content, userEmail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ public Comment(Long commentId, String content, Long commenterId) {

}


Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ public interface CommentRepository {
List<Comment> findAllByAdvertiseId(Long advertiseId);

}

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package core.kobaco.domain.commentLike.adapter;
package core.kobaco.domain.commentlike.adapter;

import core.kobaco.application.comment.service.CommentLikeManager;
import core.kobaco.domain.commentLike.service.CommentLike;
import core.kobaco.domain.commentLike.service.CommentLikeRepository;
import core.kobaco.domain.commentlike.service.CommentLikeManager;
import core.kobaco.domain.commentlike.service.CommentLike;
import core.kobaco.domain.commentlike.service.CommentLikeRepository;
import core.kobaco.domain.user.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -32,3 +32,4 @@ public void like(Long commentId, Long userId) {
);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.kobaco.domain.commentLike.service;
package core.kobaco.domain.commentlike.service;

import lombok.Getter;

Expand All @@ -19,3 +19,4 @@ public static CommentLike of(Long likeId, Long commentId, Long userId) {
return new CommentLike(likeId, commentId, userId);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.kobaco.application.comment.service;
package core.kobaco.domain.commentlike.service;

public interface CommentLikeManager {
Long getLikeCount(Long commentId);
Expand All @@ -8,3 +8,4 @@ public interface CommentLikeManager {
}



Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.kobaco.domain.commentLike.service;
package core.kobaco.domain.commentlike.service;

import java.util.Optional;

Expand All @@ -14,3 +14,4 @@ public interface CommentLikeRepository {
Optional<CommentLike> findByCommentIdAndUserId(Long commentId, Long userId);
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

public interface CommentJpaRepository extends JpaRepository<CommentEntity,Long> {
List<CommentEntity> findAllByAdvertiseId(Long advertiseId);
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core.kobaco.infra.jpa.comment.entity;

import core.kobaco.domain.advertise.Advertisement;

import core.kobaco.infra.jpa.advertisement.entity.AdvertisementEntity;
import core.kobaco.infra.jpa.user.UserEntity;
import jakarta.persistence.*;
Expand All @@ -26,17 +26,18 @@ public class CommentEntity {
@JoinColumn(name = "advertise_id", nullable = false)
private AdvertisementEntity advertise;

private CommentEntity(String content, UserEntity user, AdvertisementEntity advertise) {
private CommentEntity(Long id,String content, UserEntity user, AdvertisementEntity advertise) {
this.id = id;
this.content = content;
this.user = user;
this.advertise = advertise;
}

public static CommentEntity of(String content, UserEntity user, AdvertisementEntity advertise) {
return new CommentEntity(content, user, advertise);
public static CommentEntity of(Long id, String content, UserEntity user, AdvertisementEntity advertise) {
return new CommentEntity(id, content, user, advertise);
}

public static CommentEntity from(Long id) {
return new CommentEntity(null, null, null);
return new CommentEntity(id,null, null, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public class CommentMapper {
public CommentEntity toEntity(Comment comment, Long userId, Long advertiseId) {
UserEntity userEntity = UserEntity.from(userId);
AdvertisementEntity advertisementEntity = AdvertisementEntity.from(advertiseId);
return CommentEntity.of(comment.getContent(), userEntity, advertisementEntity);
return CommentEntity.of(comment.getCommentId(), comment.getContent(), userEntity, advertisementEntity);
}


public Comment toDomain(CommentEntity commentEntity) {
return new Comment(
commentEntity.getId(),
Expand All @@ -33,3 +35,4 @@ public List<Comment> toDomainList(List<CommentEntity> commentEntities) {
.collect(Collectors.toList());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ public List<Comment> findAllByAdvertiseId(Long advertiseId) {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ public static CommentLikeEntity of(Long id, UserEntity user, CommentEntity comme
return new CommentLikeEntity(id, user, comment);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ public interface CommentLikeJpaRepository extends JpaRepository<CommentLikeEntit

Optional<CommentLikeEntity> findByCommentIdAndUserId(Long commentId, Long userId);
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package core.kobaco.infra.jpa.commentLike;


import core.kobaco.domain.commentLike.service.CommentLike;
import core.kobaco.domain.commentlike.service.CommentLike;
import core.kobaco.infra.jpa.comment.entity.CommentEntity;
import core.kobaco.infra.jpa.commentLike.CommentLikeEntity;
import core.kobaco.infra.jpa.user.UserEntity;
import org.springframework.stereotype.Component;

Expand All @@ -20,8 +19,9 @@ public CommentLikeEntity toEntity(CommentLike commentLike) {
public CommentLike toDomain(CommentLikeEntity commentLikeEntity) {
return CommentLike.of(
commentLikeEntity.getId(),
commentLikeEntity.getUser().getId(),
commentLikeEntity.getComment().getId()
commentLikeEntity.getComment().getId(),
commentLikeEntity.getUser().getId()
);
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package core.kobaco.infra.jpa.commentLike;

import core.kobaco.domain.commentLike.service.CommentLike;
import core.kobaco.domain.commentLike.service.CommentLikeRepository;
import core.kobaco.infra.jpa.commentLike.CommentLikeJpaRepository;
import core.kobaco.infra.jpa.commentLike.CommentLikeMapper;
import core.kobaco.domain.commentlike.service.CommentLike;
import core.kobaco.domain.commentlike.service.CommentLikeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -40,3 +38,4 @@ public Optional<CommentLike> findByCommentIdAndUserId(Long commentId, Long userI
return commentLikeJpaRepository.findByCommentIdAndUserId(commentId, userId).map(likeMapper::toDomain);
}
}

Loading