트러블 슈팅

최길중·2026년 2월 3일

BF

content, createAt 댓글 중에 이런게 뒤죽박죽 나온다...

GetCommentResponse

public class GetCommentResponse {
    private final Long id;
    private final String content;
    private final String nickName;
    private final LocalDateTime createdAt;
    private final LocalDateTime updatedAt;

    public GetCommentResponse(Comment comment) {
        this.id = comment.getId();
        this.content = comment.getContent();
        this.nickName = comment.getNickName();
        this.createdAt = comment.getCreatedAt();
        this.updatedAt = comment.getModifiedAt();
    }


}
  • 여기서 GetCommentResponse생성자를 이렇게 쓰면 안됐고
public class GetCommentResponse {
    private final Long id;
    private final String content;
    private final String nickName;
    private final LocalDateTime createdAt;
    private final LocalDateTime updatedAt;

    public GetCommentResponse(Long id, String content, String nickName, LocalDateTime createdAt, LocalDateTime updatedAt) {
        this.id = id;
        this.content = content;
        this.nickName = nickName;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }
  • 그냥 정상적으로 바꿔준다. (매개변수 부분)

ScheduleService

@Transactional(readOnly = true)
    public GetScheduleCommentResponse findScheduleComment(Long scheduleId) {
        Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(
                () -> new IllegalArgumentException("없는 스케줄입니다.")
        );

        List<Comment> comments = commentRepository.findAllByScheduleIdOrderByIdAsc(scheduleId);

        List<GetCommentResponse> commentResponses = new ArrayList<>();
        for (Comment comment : comments) {
            commentResponses.add(new GetCommentResponse(comment));
        }

        return new GetScheduleCommentResponse(
                schedule.getId(),
                schedule.getTitle(),
                schedule.getContent(),
                schedule.getNickName(),
                schedule.getCreatedAt(),
                schedule.getModifiedAt(),
                commentResponses
        );
    }

여기에서 이제

for (Comment comment : comments) {
            commentResponses.add(new GetCommentResponse(comment));
        }
  • 이 부분을
for (Comment comment : comments) {
            commentResponses.add(new GetCommentResponse(
                    comment.getId(),
                    comment.getContent(),
                    comment.getNickName(),
                    comment.getCreatedAt(),
                    comment.getModifiedAt()));
        }

이렇게 바꿔 주니까

AF


짜라란 잘 나오는 모습이다


비밀번호 문제

if (!schedule.getPassword().equals(request.getPassword())){
            throw new IllegalArgumentException("비밀번호가 일치하지 않습니다.");
        }
null 뭐시기 에러

public class UpdateScheduleRequest {
    private String title;
    private String nickName;
    private String password="456";
}
이렇게 받아오고

@Transactional
    public UpdateScheduleResponse updateSchedule(Long scheduleId, UpdateScheduleRequest request) {

        Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(
                () -> new IllegalArgumentException("없는 스케쥴입니다.")
        );

        if (request.getPassword() == null || !request.getPassword().equals(schedule.getPassword())) {
            throw new IllegalArgumentException("비밀번호가 일치하지 않습니다.");
        }

        schedule.updateTitleContent(request.getTitle(), request.getNickName());
        return new UpdateScheduleResponse(schedule.getId(),schedule.getModifiedAt());
    }
여기서 주는데 왜 문제가 생길까

정답은

@Getter
public class CreateCommentRequest {
    private Long scheduleId;
    private String content;
    private String nickName;
    private String password="456";
}
  • 여기에 그냥 생성할때 비번을 넣어버리니까 해결

  • 이런 식으로 안하고 제출 할때는 json으로 password 입력 받아서 푸는 형식으로 변경


댓글 만든거 설명

이유가 좀 이상하게 나와서...

    @GetMapping("/schedules/{scheduleId}")
    public ResponseEntity<GetScheduleCommentResponse> getOneSchedule(@PathVariable Long scheduleId){
        return ResponseEntity.status(HttpStatus.OK).body(scheduleService.findScheduleComment(scheduleId));
    }

위에 해결 내용 적었습니다.


dto 새로 만든거

package com.example.schedule.dto.schedule;

import com.example.schedule.dto.Comment.GetCommentResponse;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
public class GetScheduleCommentResponse {
    private final Long id;
    private final String title;
    private final String content;
    private final String nickName;
    private final LocalDateTime createdAt;
    private final LocalDateTime updatedAt;
    private final List<GetCommentResponse> commentResponses;

    public GetScheduleCommentResponse(Long id, String title, String content, String nickName, LocalDateTime createdAt, LocalDateTime updatedAt, List<GetCommentResponse> commentResponses) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.nickName = nickName;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
        this.commentResponses = commentResponses;
    }
}

누가 알려 준건데 까먹었네요....저 List

public interface CommentRepository extends JpaRepository<Comment, Long> {
    long countByScheduleId(Long scheduleId); // 서비스에서 쓸라고 작성
    List<Comment> findAllByScheduleIdOrderByIdAsc(Long scheduleId);
}

service 작성

@Transactional
    public CreateCommentResponse save(Long scheduleId,CreateCommentRequest request) {

        Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(
                () -> new IllegalArgumentException("없는 스케쥴입니다.")
        );

        long count = commentRepository.countByScheduleId(scheduleId);
        if (count>=10){
            throw new IllegalArgumentException("10개 작성 끝났어유...");
        }

        Comment comment = new Comment(
                scheduleId,
                request.getContent(),
                request.getNickName(),
                request.getPassword());

        Comment savedComment = commentRepository.save(comment);
        return new CreateCommentResponse(
                savedComment.getId(),
                savedComment.getScheduleId(),
                savedComment.getContent(),
                savedComment.getNickName(),
                savedComment.getCreatedAt(),
                savedComment.getModifiedAt()
        );
    }

profile
취준생

1개의 댓글

comment-user-thumbnail
2026년 2월 4일

날씨 ㄹㅇ 흐리다... 그와중에 한줄기 빛 캡틴길중

답글 달기