diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/board/db/BoardEntity.java b/backend/orury/src/main/java/com/kernel360/orury/domain/board/db/BoardEntity.java index e48d162..3726984 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/board/db/BoardEntity.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/board/db/BoardEntity.java @@ -28,8 +28,8 @@ public class BoardEntity extends BaseEntity { private Long id; private String boardTitle; - @OneToMany(mappedBy = "board", cascade = CascadeType.ALL) - @Builder.Default - @OrderBy(clause = "id desc") - private List postList = List.of(); +// @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, fetch = FetchType.LAZY) +// @Builder.Default +// @OrderBy(clause = "id desc") +// private List postList = List.of(); } diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardConverter.java b/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardConverter.java index fbe84bc..64fbf0e 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardConverter.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardConverter.java @@ -1,7 +1,10 @@ package com.kernel360.orury.domain.board.service; import com.kernel360.orury.domain.board.db.BoardEntity; +import com.kernel360.orury.domain.board.db.BoardRepository; import com.kernel360.orury.domain.board.model.BoardDto; +import com.kernel360.orury.domain.post.db.PostEntity; +import com.kernel360.orury.domain.post.db.PostRepository; import com.kernel360.orury.domain.post.model.PostDto; import com.kernel360.orury.domain.post.service.PostConverter; @@ -10,23 +13,22 @@ import org.springframework.stereotype.Service; import java.util.Collections; +import java.util.List; @Service @RequiredArgsConstructor public class BoardConverter { + private final PostRepository postRepository; private final PostConverter postConverter; public BoardDto toDto(BoardEntity boardEntity) { - var postList = boardEntity.getPostList() + List postList = postRepository.findAllByBoardIdOrderByIdDesc(boardEntity.getId()) .stream() - .map(postEntity -> { - PostDto postDto = postConverter.toDto(postEntity); -// postDto.setImageList(Collections.emptyList()); // 빈 리스트로 설정 - return postDto; - }) + .map(postConverter::toDtoOnlyPost) .toList(); + return BoardDto.builder() .id(boardEntity.getId()) .boardTitle(boardEntity.getBoardTitle()) @@ -38,6 +40,19 @@ public BoardDto toDto(BoardEntity boardEntity) { .build(); } + public BoardDto toDtoOnlyBoard(BoardEntity boardEntity) { + return BoardDto.builder() + .id(boardEntity.getId()) + .boardTitle(boardEntity.getBoardTitle()) + .createdBy(boardEntity.getCreatedBy()) + .createdAt(boardEntity.getCreatedAt()) + .updatedBy(boardEntity.getUpdatedBy()) + .updatedAt(boardEntity.getUpdatedAt()) + .postList(List.of()) + .build(); + } + + public BoardEntity toEntity(BoardDto boardDto) { return BoardEntity.builder() .id(boardDto.getId()) diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardService.java b/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardService.java index 5920c44..1b1740d 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardService.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/board/service/BoardService.java @@ -38,14 +38,14 @@ public BoardDto createBoard( var saveEntity = boardRepository.save(entity); - return boardConverter.toDto(saveEntity); + return boardConverter.toDtoOnlyBoard(saveEntity); } // 게시판 조회 public List getBoardList() { List boardEntityList = boardRepository.findAll(); return boardEntityList.stream() - .map(boardConverter::toDto) + .map(boardConverter::toDtoOnlyBoard) .toList(); } @@ -56,7 +56,7 @@ public BoardDto updateBoard( BoardEntity entity = boardRepository.findById(boardRequest.getId()) .orElseThrow(() -> new BusinessException(BoardErrorCode.THERE_IS_NO_BOARD)); - BoardDto updatedDto = boardConverter.toDto(entity); + BoardDto updatedDto = boardConverter.toDtoOnlyBoard(entity); updatedDto.setBoardTitle(boardRequest.getBoardTitle()); updatedDto.setUpdatedBy(Constant.ADMIN.getMessage()); updatedDto.setUpdatedAt(LocalDateTime.now()); @@ -78,7 +78,7 @@ public BoardDto getBoard(Long id) { } public List getNoticeBoard(Long id) { - var noticeList = postRepository.findAllByBoardId(id); + var noticeList = postRepository.findAllByBoardIdOrderByIdDesc(id); if(noticeList.isEmpty()) { throw new BusinessException(BoardErrorCode.THERE_IS_NO_BOARD); } diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentEntity.java b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentEntity.java index e8ca761..ac99b6b 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentEntity.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentEntity.java @@ -12,6 +12,8 @@ import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.SuperBuilder; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import javax.persistence.*; @@ -35,6 +37,7 @@ public class CommentEntity extends BaseEntity { private Long userId; @ManyToOne + @OnDelete(action = OnDeleteAction.CASCADE) @JsonIgnore @ToString.Exclude private PostEntity post; diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentRepository.java b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentRepository.java index 2454bc7..174f23c 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentRepository.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/db/CommentRepository.java @@ -6,5 +6,7 @@ public interface CommentRepository extends JpaRepository{ // select * from comment where post_id = ? - List findAllByPostIdOrderByIdDesc(Long postId); + List findAllByPostId(Long postId); + + Long countByPostId(Long postId); } diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/service/CommentService.java b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/service/CommentService.java index f5ffc55..38b2e49 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/comment/service/CommentService.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/comment/service/CommentService.java @@ -104,12 +104,12 @@ public void deleteComment(Long commentId){ commentRepository.deleteById(commentId); } - public List findAllByPostId(Long postId) { - List commentEntityList = commentRepository.findAllByPostIdOrderByIdDesc(postId); - return commentEntityList.stream() - .map(commentConverter::toDto) - .toList(); - } +// public List findAllByPostId(Long postId) { +// List commentEntityList = commentRepository.findAllByPostId(postId); +// return commentEntityList.stream() +// .map(commentConverter::toDto) +// .toList(); +// } public boolean isWriter(String userEmail, Long id) { var comment = commentRepository.findById(id).orElseThrow( diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostEntity.java b/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostEntity.java index a9b55ff..87100b8 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostEntity.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostEntity.java @@ -7,6 +7,8 @@ import com.kernel360.orury.global.common.Listener; import lombok.*; import lombok.experimental.SuperBuilder; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.OrderBy; import javax.persistence.*; @@ -26,6 +28,7 @@ public class PostEntity extends BaseEntity { private Long id; @ManyToOne + @OnDelete(action = OnDeleteAction.CASCADE) @JsonIgnore @ToString.Exclude private BoardEntity board; @@ -41,8 +44,4 @@ public class PostEntity extends BaseEntity { private int likeCnt; private Long userId; - @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) - @Builder.Default - @OrderBy(clause = "id asc") - private List commentList = List.of(); } \ No newline at end of file diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostRepository.java b/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostRepository.java index 45ee8b5..c3c192c 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostRepository.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/post/db/PostRepository.java @@ -1,11 +1,11 @@ package com.kernel360.orury.domain.post.db; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.List; import java.util.Optional; public interface PostRepository extends JpaRepository { -// Optional> findAllByBoardId(Long boardId); - List findAllByBoardId(Long boardId); + List findAllByBoardIdOrderByIdDesc(Long boardId); } diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/post/model/PostDto.java b/backend/orury/src/main/java/com/kernel360/orury/domain/post/model/PostDto.java index a2d78d1..4339769 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/post/model/PostDto.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/post/model/PostDto.java @@ -34,7 +34,7 @@ public class PostDto { private String thumbnailUrl; private List imageList = List.of(); private Long commentCnt; - private List commentList = List.of(); +// private List commentList = List.of(); private Map> commentMap = Map.of(); @JsonProperty("is_like") private boolean isLike; diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostConverter.java b/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostConverter.java index af8313b..0b41cb5 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostConverter.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostConverter.java @@ -2,6 +2,7 @@ import com.kernel360.orury.domain.board.db.BoardEntity; import com.kernel360.orury.domain.board.db.BoardRepository; +import com.kernel360.orury.domain.comment.db.CommentRepository; import com.kernel360.orury.domain.comment.model.CommentDto; import com.kernel360.orury.domain.comment.service.CommentConverter; import com.kernel360.orury.domain.post.db.PostEntity; @@ -31,17 +32,16 @@ public class PostConverter { private final BoardRepository boardRepository; private final UserRepository userRepository; private final CommentConverter commentConverter; + private final CommentRepository commentRepository; private final PostLikeRepository postLikeRepository; public PostDto toDto(PostEntity postEntity) { - var commentList = postEntity.getCommentList() + List commentList = commentRepository.findAllByPostId(postEntity.getId()) .stream() .map(commentConverter::toDto) .toList(); - long commentCnt = commentList.size(); - // map으로 변환 Map> commentMap = new HashMap<>(); for (CommentDto comment : commentList) { @@ -54,6 +54,9 @@ public PostDto toDto(PostEntity postEntity) { } } + + Long commentCnt = commentRepository.countByPostId(postEntity.getId()); + // 게시글 작성자 userNickname 설정을 위한 entity UserEntity userEntity = userRepository.findById(postEntity.getUserId()) .orElseThrow(() -> new BusinessException(UserErrorCode.THERE_IS_NO_USER)); @@ -78,7 +81,6 @@ public PostDto toDto(PostEntity postEntity) { .collect(Collectors.toList()) ) .commentCnt(commentCnt) - .commentList(commentList) .commentMap(commentMap) .isLike(isLike) .likeCnt(likeCnt.intValue()) @@ -89,6 +91,43 @@ public PostDto toDto(PostEntity postEntity) { .build(); } + public PostDto toDtoOnlyPost(PostEntity postEntity) { + Long commentCnt = commentRepository.countByPostId(postEntity.getId()); + + // 게시글 작성자 userNickname 설정을 위한 entity + UserEntity userEntity = userRepository.findById(postEntity.getUserId()) + .orElseThrow(() -> new BusinessException(UserErrorCode.THERE_IS_NO_USER)); + + // 좋아요 수 및 유저 좋아요 세팅 + long loginId = getLoginId(); + boolean isLike = getPostLike(loginId, postEntity.getId()); + Long likeCnt = postLikeRepository.countByPostLikePKPostId(postEntity.getId()); + + return PostDto.builder() + .id(postEntity.getId()) + .boardId(postEntity.getBoard().getId()) + .postTitle(postEntity.getPostTitle()) + .postContent(postEntity.getPostContent()) + .userNickname(userEntity.getNickname()) + .viewCnt(postEntity.getViewCnt()) + .likeCnt(postEntity.getLikeCnt()) + .userId(postEntity.getUserId()) + .thumbnailUrl(postEntity.getThumbnailUrl()) + .imageList(postEntity.getImages() == null ? List.of() : Arrays.stream(postEntity.getImages().split(",")) + .map(image -> getenv().get("IMGUR_URL") + image) + .collect(Collectors.toList()) + ) + .commentCnt(commentCnt) + .commentMap(Map.of()) + .isLike(isLike) + .likeCnt(likeCnt.intValue()) + .createdBy(postEntity.getCreatedBy()) + .createdAt(postEntity.getCreatedAt()) + .updatedBy(postEntity.getUpdatedBy()) + .updatedAt(postEntity.getUpdatedAt()) + .build(); + } + public PostEntity toEntity(PostDto postDto) { BoardEntity boardEntity = boardRepository.findById(postDto.getBoardId()) .orElseThrow( diff --git a/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostService.java b/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostService.java index 7a0a991..87fbc3e 100644 --- a/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostService.java +++ b/backend/orury/src/main/java/com/kernel360/orury/domain/post/service/PostService.java @@ -57,7 +57,7 @@ public PostDto createPost( .build(); var saveEntity = postRepository.save(entity); - return postConverter.toDto(saveEntity); + return postConverter.toDtoOnlyPost(saveEntity); } public PostDto getPost(Long id) { @@ -74,7 +74,7 @@ public PostDto updatePost( var postEntityOptional = postRepository.findById(postId); var entity = postEntityOptional.orElseThrow( () -> new BusinessException(PostErrorCode.THERE_IS_NO_POST)); - var dto = postConverter.toDto(entity); + var dto = postConverter.toDtoOnlyPost(entity); dto.setPostTitle(postRequest.getPostTitle()); dto.setPostContent(postRequest.getPostContent()); dto.setUpdatedBy(Constant.ADMIN.getMessage()); @@ -103,7 +103,7 @@ public Api> getPostList(Pageable pageable) { .build(); var dtoList = entityList.stream() - .map(postConverter::toDto) + .map(postConverter::toDtoOnlyPost) .toList(); return Api.>builder() diff --git a/backend/orury/src/main/resources/application.yml b/backend/orury/src/main/resources/application.yml index 380aa42..3085b1e 100644 --- a/backend/orury/src/main/resources/application.yml +++ b/backend/orury/src/main/resources/application.yml @@ -25,11 +25,11 @@ spring: mode:NONE datasource: driver-class-name: com.mysql.cj.jdbc.Driver - # url: ${LOCAL_DB_URL} # configuration에서 설정 필요 +# url: ${LOCAL_DB_URL} # configuration에서 설정 필요 url: ENC(4t9k8jNC5SIDd1hpsqRJiw5NgwtRAZ9l2cY42PcN3v6GNDC61KXk4X3nYwwUdW4+aC6g41zh14HyfWg+2fPmfuTXHg7aw+vTvL9Oomq1wEKM2r2ZHJbhmakdpmbeMVjVALC/MSk28Tp4a5sBhUFRWdpaFO2qQcpdUHUd9KsuJh0=) - # username: ${LOCAL_DB_USERNAME} +# username: ${LOCAL_DB_USERNAME} username: ENC(2STroDy2TfWobkrTroFwbRcxLke05156ZFMrqwKmVssNcN4foSX4I6ax8YAgdW3R) - # password: ${LOCAL_DB_USER_PASSWORD} +# password: ${LOCAL_DB_USER_PASSWORD} password: ENC(a8l7l/AYNz2x1HPYyu+urOBwVcBhQkeWgTi6Fvt+lQFq1QyHsL8yyi5+wr7lh26P) flyway: enabled: true @@ -38,7 +38,7 @@ jwt: header: Authorization #HS512 알고리즘을 사용할 것이기 때문에 512bit, 즉 64byte 이상의 secret key를 사용해야 한다. #echo 'silvernine-tech-spring-boot-jwt-tutorial-secret-silvernine-tech-spring-boot-jwt-tutorial-secret'|base64 - # secret: ${LOCAL_JWT_SECRET_KEY} +# secret: ${LOCAL_JWT_SECRET_KEY} secret: ENC(XNkGVBeK1sJZhLhri+zz7pJOhHCvfif265mvT8OUIbOGeQcOCtHNnG2s3qjsKNe2u+dLoNVQBzbF1bKUfDxi8Po5tL7jQbZMPA33Dg1QMQFQWV46IyrYnLykYXQQvpin/SNPXW04ECDoRLF3TNwcS22D8uWEwe8L2wtcauyHeO1z+J6lUQArPHy76O2pzC7FHlBjOTw3STd23e3dd1WBQtHAYVmOIvNuPreulzSaHXc=) access-validity: 1800 refresh-validity: 7200 diff --git a/frontend/lib/presentation/Board/post_create.dart b/frontend/lib/presentation/Board/post_create.dart index 0a8278c..9e4d95e 100644 --- a/frontend/lib/presentation/Board/post_create.dart +++ b/frontend/lib/presentation/Board/post_create.dart @@ -141,6 +141,7 @@ class _PostCreateState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( + centerTitle: true, title: Text('게시글 작성'), ), body: Padding( diff --git a/frontend/lib/presentation/Board/post_detail.dart b/frontend/lib/presentation/Board/post_detail.dart index 48b27f2..471db83 100644 --- a/frontend/lib/presentation/Board/post_detail.dart +++ b/frontend/lib/presentation/Board/post_detail.dart @@ -255,6 +255,7 @@ class _PostDetailState extends State { return Scaffold( appBar: AppBar( + centerTitle: true, title: Text('게시물 상세보기'), ), body: Padding( @@ -322,7 +323,31 @@ class _PostDetailState extends State { ); } else if (result == 'delete') { // 게시글 삭제 기능 구현 - deletePost(); + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text('삭제 확인'), + content: Text('내용을 삭제하시겠습니까?'), + actions: [ + TextButton( + child: Text('취소'), + onPressed: () { + router.pop(); // Dialog를 닫습니다. + }, + ), + TextButton( + child: Text('삭제'), + onPressed: () { + // 여기에 삭제 관련 코드를 작성합니다. + deletePost(); + router.pop(); // Dialog를 닫습니다. + }, + ), + ], + ); + }, + ); } }, itemBuilder: (BuildContext context) => diff --git a/frontend/lib/presentation/main/main_screen.dart b/frontend/lib/presentation/main/main_screen.dart index 49e95df..54b99a1 100644 --- a/frontend/lib/presentation/main/main_screen.dart +++ b/frontend/lib/presentation/main/main_screen.dart @@ -37,7 +37,7 @@ class _MainScreenState extends State { super.initState(); initSharedPreferences(); connectToServer(); - loadBoards(); + // loadBoards(); } @override @@ -81,10 +81,11 @@ class _MainScreenState extends State { // Uri.http(dotenv.env['API_URL']!, '/api/board'), Uri.http(dotenv.env['AWS_API_URL']!, '/api/board'), ); - + print('함수명 : getBoards'); final jsonData = jsonDecode(utf8.decode(response.bodyBytes)) as List; final boards = jsonData.map((boardJson) => Boards.fromJson(boardJson)).toList(); + print(boards[0].boardTitle); return boards; } @@ -100,7 +101,7 @@ class _MainScreenState extends State { // Uri.http(dotenv.env['API_URL']!, '/api/board/1'), Uri.http(dotenv.env['AWS_API_URL']!, '/api/board/$boardId'), ); - + print('함수명 : fetchPosts'); final jsonData = jsonDecode(utf8.decode(response.bodyBytes)); final board = Board.fromJson(jsonData); return board.postList;