Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.aliens.db.communityarticle.repository;

import java.time.Instant;
import java.util.Optional;

public interface CommunityArticleCustomRepository {
Optional<Instant> findLatestArticleTimeByMemberId(Long memberId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

public interface CommunityArticleRepository
extends JpaRepository<CommunityArticleEntity, Long> {
extends JpaRepository<CommunityArticleEntity, Long>, CommunityArticleCustomRepository {
Page<CommunityArticleEntity> findAllByCategory(ArticleCategory category, Pageable pageable);

List<CommunityArticleEntity> findAllByTitleContainingOrContentContaining(String title, String content);

List<CommunityArticleEntity> findAllByMember(MemberEntity member);

Page<CommunityArticleEntity> findAllByCategoryAndTitleContainingOrContentContaining(ArticleCategory category, String title, String content, Pageable pageable);

Optional<Instant> findLatestArticleTimeByMemberId(Long memberId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.aliens.db.communityarticle.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import javax.persistence.EntityManager;
import java.time.Instant;
import java.util.Optional;

import static com.aliens.db.communityarticle.entity.QCommunityArticleEntity.communityArticleEntity;

@RequiredArgsConstructor

public class CommunityArticleRepositoryImpl implements CommunityArticleCustomRepository {
private final EntityManager entityManager;

@Override
public Optional<Instant> findLatestArticleTimeByMemberId(Long memberId) {
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

return Optional.ofNullable(queryFactory
.select(communityArticleEntity.createdAt.max())
.from(communityArticleEntity)
.where(communityArticleEntity.member.id.eq(memberId))
.fetchOne());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.aliens.db.marketarticle.repository;

import com.aliens.db.marketarticle.entity.MarketArticleEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.Instant;
import java.util.Optional;

public interface MarketArticleCustomRepository {

Page<MarketArticleEntity> findAllWithFetchJoin(Pageable pageable);

Page<MarketArticleEntity> findAllByTitleContainingOrContentContainingByFetchJoin(String title, String content, Pageable pageable);

Optional<Instant> findLatestArticleTimeByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

public interface MarketArticleRepository extends
JpaRepository<MarketArticleEntity, Long> {
JpaRepository<MarketArticleEntity, Long>, MarketArticleCustomRepository {
List<MarketArticleEntity> findAllByTitleContainingOrContentContaining(String title, String content);

List<MarketArticleEntity> findAllByMember(MemberEntity member);

Page<MarketArticleEntity> findAllByTitleContainingOrContentContaining(String title, String content, Pageable pageable);

Optional<Instant> findLatestArticleTimeByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.aliens.db.marketarticle.repository;

import com.aliens.db.marketarticle.entity.MarketArticleEntity;
import com.aliens.db.marketarticlecomment.entity.QMarketArticleCommentEntity;
import com.aliens.db.marketbookmark.entity.QMarketBookmarkEntity;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.JPQLQueryFactory;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.Querydsl;

import javax.persistence.EntityManager;
import java.time.Instant;
import java.util.List;
import java.util.Optional;

import static com.aliens.db.marketarticle.entity.QMarketArticleEntity.marketArticleEntity;
import static com.aliens.db.marketarticlecomment.entity.QMarketArticleCommentEntity.*;
import static com.aliens.db.marketbookmark.entity.QMarketBookmarkEntity.*;

@RequiredArgsConstructor
public class MarketArticleRepositoryImpl implements MarketArticleCustomRepository {
private final EntityManager entityManager;

@Override
public Page<MarketArticleEntity> findAllWithFetchJoin(Pageable pageable) {
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

List<MarketArticleEntity> result = queryFactory
.selectFrom(marketArticleEntity)
.leftJoin(marketArticleEntity.likes, marketBookmarkEntity)
.leftJoin(marketArticleEntity.comments, marketArticleCommentEntity)
.fetchJoin()
.fetch();

return new PageImpl<>(result, pageable, result.size());
}

@Override
public Page<MarketArticleEntity> findAllByTitleContainingOrContentContainingByFetchJoin(String title, String content, Pageable pageable) {
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

List<MarketArticleEntity> result = queryFactory
.selectFrom(marketArticleEntity)
.leftJoin(marketArticleEntity.likes, marketBookmarkEntity)
.leftJoin(marketArticleEntity.comments, marketArticleCommentEntity)
.fetchJoin()
.where(
marketArticleEntity.title.containsIgnoreCase(title)
.or(marketArticleEntity.content.containsIgnoreCase(content))
)
.fetch();

return new PageImpl<>(result, pageable, result.size());
}

@Override
public Optional<Instant> findLatestArticleTimeByMemberId(Long memberId) {
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

return Optional.ofNullable(queryFactory
.select(marketArticleEntity.createdAt.max())
.from(marketArticleEntity)
.where(marketArticleEntity.member.id.eq(memberId))
.fetchOne());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.aliens.friendship.domain.article.community.dto.CreateCommunityArticleRequest;
import com.aliens.friendship.domain.article.community.dto.UpdateCommunityArticleRequest;
import com.aliens.friendship.domain.article.dto.ArticleDto;
import com.aliens.friendship.domain.article.exception.ArticleCreationNotAllowedException;
import com.aliens.friendship.domain.article.service.ArticleImageService;
import com.aliens.friendship.global.error.InvalidResourceOwnerException;
import com.aliens.friendship.global.error.ResourceNotFoundException;
Expand All @@ -25,6 +26,8 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -102,22 +105,33 @@ public Long saveCommunityArticle(
CreateCommunityArticleRequest request,
UserDetails principal
) throws Exception {
CommunityArticleEntity communityArticle = communityArticleRepository.save(
request.toEntity(getMemberEntity(principal.getUsername()))
);
MemberEntity member = getMemberEntity(principal.getUsername());
Optional<Instant> latestArticleTime = communityArticleRepository.findLatestArticleTimeByMemberId(member.getId());

List<MultipartFile> imageUrls = request.getImageUrls();
if (imageUrls != null && !imageUrls.isEmpty()) {
for (MultipartFile imageUrl : imageUrls) {
CommunityArticleImageEntity communityArticleImage = CommunityArticleImageEntity.of(
articleImageService.uploadProfileImage(imageUrl),
communityArticle
);
communityArticleImageRepository.save(communityArticleImage);
boolean canCreateArticle = latestArticleTime.map(
time -> Duration.between(time, Instant.now()).toMinutes() >= 5
).orElse(true);

if (canCreateArticle) {
CommunityArticleEntity communityArticle = communityArticleRepository.save(
request.toEntity(getMemberEntity(principal.getUsername()))
);

List<MultipartFile> imageUrls = request.getImageUrls();
if (imageUrls != null && !imageUrls.isEmpty()) {
for (MultipartFile imageUrl : imageUrls) {
CommunityArticleImageEntity communityArticleImage = CommunityArticleImageEntity.of(
articleImageService.uploadProfileImage(imageUrl),
communityArticle
);
communityArticleImageRepository.save(communityArticleImage);
}
}
}

return communityArticle.getId();
return communityArticle.getId();
} else {
throw new ArticleCreationNotAllowedException();
}
}

public void updateCommunityArticle(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.aliens.friendship.domain.article.exception;

import com.aliens.friendship.global.error.ExceptionCode;
import com.aliens.friendship.global.error.ResourceNotFoundException;

public class ArticleCreationNotAllowedException
extends ResourceNotFoundException {

private ExceptionCode exceptionCode;

public ArticleCreationNotAllowedException() {
super(ArticleExceptionCode.ARTICLE_CREATION_NOT_ALLOWED);
this.exceptionCode = ArticleExceptionCode.ARTICLE_CREATION_NOT_ALLOWED;
}

@Override
public ExceptionCode getExceptionCode() {
return exceptionCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@Getter
Expand All @@ -13,7 +14,8 @@ public enum ArticleExceptionCode
implements ExceptionCode {

ARTICLE_NOT_FOUND(NOT_FOUND, "BA-C-001", "존재하지 않는 게시글입니다."),
ARTICLE_COMMENT_NOT_FOUND(NOT_FOUND, "BA-C-001", "존재하지 않는 게시글 댓글입니다.");
ARTICLE_COMMENT_NOT_FOUND(NOT_FOUND, "BA-C-001", "존재하지 않는 게시글 댓글입니다."),
ARTICLE_CREATION_NOT_ALLOWED(BAD_REQUEST, "BA-C-003", "게시글 생성 후 5분 후에 재생성이 가능합니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.aliens.friendship.domain.article.exception;

import com.aliens.friendship.global.error.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ArticleExceptionHandler {

/**
* ArticleCreationNotAllowedException 핸들링
* Custom Exception
*/
@ExceptionHandler(ArticleCreationNotAllowedException.class)
protected ResponseEntity<ErrorResponse> handlingArticleCreationNotAllowedException(
ArticleCreationNotAllowedException e
) {
log.error("[handling ArticleCreationNotAllowedException] {}", e.getExceptionCode().getMessage());
return new ResponseEntity<>(
ErrorResponse.of(e.getExceptionCode()),
HttpStatus.valueOf(e.getExceptionCode().getHttpStatus().value())
);
}
}
Loading