Skip to content

Commit

Permalink
fix: 댓글 생성할때 advertiseId로 연결 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyo-j committed Mar 4, 2024
1 parent e8f5652 commit ef4686e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion kobaco/kobaco/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/comments")
@RequestMapping("/api/comments")
public class CommentController {
private final CommentService commentService;

@PostMapping("/create")
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO) {
CommentDetail createdComment = commentService.createComment(commentDTO);
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, @RequestParam Long advertiseId) {
CommentDetail createdComment = commentService.createComment(commentDTO, advertiseId);
return ResponseEntity.status(HttpStatus.CREATED).body(createdComment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CommentService {
private final CommentLikeManager commentLikeManager;

@Transactional
public CommentDetail createComment(CommentDetail commentDetail) {
public CommentDetail createComment(CommentDetail commentDetail, Long advertiseId) {
final Long userId = userUtils.getRequestUserId();
if (userId == null) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다.");
Expand All @@ -33,10 +33,11 @@ public CommentDetail createComment(CommentDetail commentDetail) {
Comment comment = new Comment(
null,
commentDetail.getContent(),
userId
userId,
advertiseId
);

Comment savedCommentEntity = commentRepository.save(comment);
Comment savedCommentEntity = commentRepository.save(comment, advertiseId);
return new CommentDetail(
savedCommentEntity.getCommentId(),
savedCommentEntity.getContent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ public class Comment {
private Long commentId;
private String content;
private Long commenterId;
private Long advertiseId;

public static Comment of (Long commentId, String content, Long commenterId) {
return new Comment(commentId, content, commenterId);
public Comment(Long commentId, String content, Long commenterId) {
this.commentId = commentId;
this.content = content;
this.commenterId = commenterId;
this.advertiseId = null;
}

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

public interface CommentRepository {
Optional<Comment> findById(Long commentId);
Comment save(Comment comment);
Comment save(Comment comment, Long advertiseId);
List<Comment> findAll();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ public class CommentEntity {
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

private CommentEntity(String content, UserEntity user) {
@Column(name = "advertise_id")
private Long advertiseId;

private CommentEntity(String content, UserEntity user, Long advertiseId) {
this.content = content;
this.user = user;
this.advertiseId = advertiseId;
}
public static CommentEntity of(String content, UserEntity user){
return new CommentEntity(content, user);

public static CommentEntity of(String content, UserEntity user, Long advertiseId) {
return new CommentEntity(content, user, advertiseId);
}
public static CommentEntity from(Long id){
return new CommentEntity(null, null);

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

return CommentEntity.of(comment.getContent(), userEntity, advertiseId);
}

public Comment toDomain(CommentEntity commentEntity) {
Expand All @@ -25,6 +24,7 @@ public Comment toDomain(CommentEntity commentEntity) {
commentEntity.getContent(),
commentEntity.getUser().getId()
);

}

public List<Comment> toDomainList(List<CommentEntity> commentEntities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public Optional<Comment> findById(Long commentId) {
}

@Override
public Comment save(Comment comment) {
// 사용자의 ID를 함께 전달
CommentEntity commentEntity = commentMapper.toEntity(comment, comment.getCommenterId());
public Comment save(Comment comment, Long advertiseId) {
CommentEntity commentEntity = commentMapper.toEntity(comment, comment.getCommenterId(), advertiseId);
commentEntity = commentJpaRepository.save(commentEntity);
return commentMapper.toDomain(commentEntity);
}
Expand Down

0 comments on commit ef4686e

Please sign in to comment.