Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/TG-WinG/tech-blog into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
singsangssong committed Nov 25, 2024
2 parents bbd4056 + fa44fa3 commit d37bb48
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ tasks.named('test') {

// 배포용 태스크
tasks.register('jarPath') {
println "${project.buildDir}/libs/${project.name}-${version}.jar"
println "${project.buildDir}/libs/${project.name}-${version}.jar"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public static Specification<Post> hasWriterStudentNumber(String studentNumber) {
};
}

public static Specification<Post> hasWriterNameLike(String name) {
return (root, query, cb) -> {
Join<User, Post> userPost = root.join("writer");
return cb.like(userPost.get("name"), "%" + name + "%");
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import org.springframework.data.jpa.repository.JpaRepository;

import kr.tgwing.tech.blog.entity.LikeHistory;
import kr.tgwing.tech.blog.entity.Post;

/**
* LikeHistoryRepository
*/
public interface LikeHistoryRepository extends JpaRepository<LikeHistory, LikeHistory.Key> {

void deleteAllByPost(Post post);
}
12 changes: 10 additions & 2 deletions src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class PostServiceImpl implements PostService {
private final LikeHistoryRepository likeHistoryRepository;

@Override
@Transactional(readOnly = true)
public PostDetail getPost(Long postId, String userStudentNumber) {
User user = getUserEntity(userStudentNumber);
Post post = getPostEntity(postId);
Expand Down Expand Up @@ -119,16 +120,20 @@ public void deletePost(Long postId, String writerStudentNumber) {

// 해당 URL을 요청한 사람이 공지 작성자인 경우에만 삭제 가능
if(post.getWriter().equals(postWriter)) {
likeHistoryRepository.deleteAllByPost(post);
postRepository.deleteById(postId);
} else {
throw new UserIsNotPostWriterException(); }
throw new UserIsNotPostWriterException();
}
}

@Override
@Transactional(readOnly = true)
public Page<PostOverview> getPostOverviews(PostQuery query, String userStudentNumber, Pageable pageable) {
User user = getUserEntity(userStudentNumber);
Specification<Post> spec = PostSpecifications.hasTitleLike(query.getKeyword())
.or(PostSpecifications.hasContentLike(query.getKeyword()));
.or(PostSpecifications.hasContentLike(query.getKeyword()))
.or(PostSpecifications.hasWriterNameLike(query.getKeyword()));

if (query.getHashtag() != null && query.getHashtag().size() > 0) {
spec = spec.and(PostSpecifications.hasHashtagIn(query.getHashtag()));
Expand All @@ -147,6 +152,7 @@ public Page<PostOverview> getPostOverviews(PostQuery query, String userStudentNu
}

@Override
@Transactional(readOnly = true)
public PostOverview getPostOverview(Long postId, String userStudentNumber) {
Post post = getPostEntity(postId);
User user = getUserEntity(userStudentNumber);
Expand Down Expand Up @@ -201,6 +207,7 @@ public void deleteComment(Long postId, Long commentId, String writerStudentNumbe
}

@Override
@Transactional(readOnly = true)
public Page<CommentView> getComments(Long postId, Pageable pageable) {
Post post = postRepository.findById(postId)
.orElseThrow(PostNotFoundException::new);
Expand Down Expand Up @@ -260,6 +267,7 @@ public void deleteReply(Long postId, Long commentId, Long replyId, String writer
}

@Override
@Transactional(readOnly = true)
public Page<ReplyView> getReplies(Long postId, Long commentId, Pageable pageable) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(CommentNotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@RestControllerAdvice
public class ExceptionAdvice { // Exception Handler
Expand All @@ -24,6 +25,11 @@ public ResponseEntity<ExceptionResponse> handleCommonException(CommonException e

@ExceptionHandler(Exception.class)
public ResponseEntity<Void> handleException(Exception e) {
if (e instanceof NoResourceFoundException) {
defaultLog.info(e.getMessage());
return ResponseEntity.notFound().build();
}

defaultLog.error(e.getMessage());
exceptionLog.error(e.getMessage(), e);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package kr.tgwing.tech.common.exception;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
@Getter
Expand Down

0 comments on commit d37bb48

Please sign in to comment.