Skip to content

Commit

Permalink
Merge pull request #187 from Kernel360/develop
Browse files Browse the repository at this point in the history
Develop to Main merge
  • Loading branch information
mooncw authored Nov 24, 2023
2 parents 00b5148 + 4150c00 commit b0e17a4
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostEntity> postList = List.of();
// @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
// @Builder.Default
// @OrderBy(clause = "id desc")
// private List<PostEntity> postList = List.of();
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<PostDto> 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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public BoardDto createBoard(

var saveEntity = boardRepository.save(entity);

return boardConverter.toDto(saveEntity);
return boardConverter.toDtoOnlyBoard(saveEntity);
}

// 게시판 조회
public List<BoardDto> getBoardList() {
List<BoardEntity> boardEntityList = boardRepository.findAll();
return boardEntityList.stream()
.map(boardConverter::toDto)
.map(boardConverter::toDtoOnlyBoard)
.toList();
}

Expand All @@ -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());
Expand All @@ -78,7 +78,7 @@ public BoardDto getBoard(Long id) {
}

public List<PostDto> getNoticeBoard(Long id) {
var noticeList = postRepository.findAllByBoardId(id);
var noticeList = postRepository.findAllByBoardIdOrderByIdDesc(id);
if(noticeList.isEmpty()) {
throw new BusinessException(BoardErrorCode.THERE_IS_NO_BOARD);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -35,6 +37,7 @@ public class CommentEntity extends BaseEntity {

private Long userId;
@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
@ToString.Exclude
private PostEntity post;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

public interface CommentRepository extends JpaRepository<CommentEntity, Long>{
// select * from comment where post_id = ?
List<CommentEntity> findAllByPostIdOrderByIdDesc(Long postId);
List<CommentEntity> findAllByPostId(Long postId);

Long countByPostId(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public void deleteComment(Long commentId){
commentRepository.deleteById(commentId);
}

public List<CommentDto> findAllByPostId(Long postId) {
List<CommentEntity> commentEntityList = commentRepository.findAllByPostIdOrderByIdDesc(postId);
return commentEntityList.stream()
.map(commentConverter::toDto)
.toList();
}
// public List<CommentDto> findAllByPostId(Long postId) {
// List<CommentEntity> commentEntityList = commentRepository.findAllByPostId(postId);
// return commentEntityList.stream()
// .map(commentConverter::toDto)
// .toList();
// }

public boolean isWriter(String userEmail, Long id) {
var comment = commentRepository.findById(id).orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -26,6 +28,7 @@ public class PostEntity extends BaseEntity {
private Long id;

@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
@ToString.Exclude
private BoardEntity board;
Expand All @@ -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<CommentEntity> commentList = List.of();
}
Original file line number Diff line number Diff line change
@@ -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<PostEntity, Long> {
// Optional<List<PostEntity>> findAllByBoardId(Long boardId);
List<PostEntity> findAllByBoardId(Long boardId);
List<PostEntity> findAllByBoardIdOrderByIdDesc(Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PostDto {
private String thumbnailUrl;
private List<String> imageList = List.of();
private Long commentCnt;
private List<CommentDto> commentList = List.of();
// private List<CommentDto> commentList = List.of();
private Map<String, List<CommentDto>> commentMap = Map.of();
@JsonProperty("is_like")
private boolean isLike;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CommentDto> commentList = commentRepository.findAllByPostId(postEntity.getId())
.stream()
.map(commentConverter::toDto)
.toList();

long commentCnt = commentList.size();

// map으로 변환
Map<String, List<CommentDto>> commentMap = new HashMap<>();
for (CommentDto comment : commentList) {
Expand All @@ -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));
Expand All @@ -78,7 +81,6 @@ public PostDto toDto(PostEntity postEntity) {
.collect(Collectors.toList())
)
.commentCnt(commentCnt)
.commentList(commentList)
.commentMap(commentMap)
.isLike(isLike)
.likeCnt(likeCnt.intValue())
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down Expand Up @@ -103,7 +103,7 @@ public Api<List<PostDto>> getPostList(Pageable pageable) {
.build();

var dtoList = entityList.stream()
.map(postConverter::toDto)
.map(postConverter::toDtoOnlyPost)
.toList();

return Api.<List<PostDto>>builder()
Expand Down
8 changes: 4 additions & 4 deletions backend/orury/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/presentation/Board/post_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class _PostCreateState extends State<PostCreate> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('게시글 작성'),
),
body: Padding(
Expand Down
27 changes: 26 additions & 1 deletion frontend/lib/presentation/Board/post_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class _PostDetailState extends State<PostDetail> {

return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('게시물 상세보기'),
),
body: Padding(
Expand Down Expand Up @@ -322,7 +323,31 @@ class _PostDetailState extends State<PostDetail> {
);
} else if (result == 'delete') {
// 게시글 삭제 기능 구현
deletePost();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('삭제 확인'),
content: Text('내용을 삭제하시겠습니까?'),
actions: <Widget>[
TextButton(
child: Text('취소'),
onPressed: () {
router.pop(); // Dialog를 닫습니다.
},
),
TextButton(
child: Text('삭제'),
onPressed: () {
// 여기에 삭제 관련 코드를 작성합니다.
deletePost();
router.pop(); // Dialog를 닫습니다.
},
),
],
);
},
);
}
},
itemBuilder: (BuildContext context) =>
Expand Down
Loading

0 comments on commit b0e17a4

Please sign in to comment.