Skip to content

Commit

Permalink
refactor: 장터 게시글 5분 내에 재생성 불가하도록 수정
Browse files Browse the repository at this point in the history
related to: #235
  • Loading branch information
dmswjdchl committed Oct 11, 2023
1 parent a65388c commit 0ede3ee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface MarketArticleCustomRepository {

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 @@ -18,4 +18,5 @@ public interface MarketArticleRepository extends

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

Optional<Instant> findLatestArticleTimeByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ public Page<MarketArticleEntity> findAllByTitleContainingOrContentContainingByFe
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 @@ -12,6 +12,7 @@
import com.aliens.db.productimage.entity.ProductImageEntity;
import com.aliens.db.productimage.repsitory.ProductImageRepository;
import com.aliens.friendship.domain.article.dto.ArticleDto;
import com.aliens.friendship.domain.article.exception.ArticleCreationNotAllowedException;
import com.aliens.friendship.domain.article.market.dto.CreateMarketArticleRequest;
import com.aliens.friendship.domain.article.market.dto.MarketArticleDto;
import com.aliens.friendship.domain.article.market.dto.UpdateMarketArticleRequest;
Expand All @@ -27,6 +28,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 @@ -139,20 +142,30 @@ public Long saveMarketArticle(
CreateMarketArticleRequest request,
UserDetails userPrincipal
) throws Exception {
MemberEntity member = getMemberEntity(userPrincipal.getUsername());
Optional<Instant> latestArticleTime = marketArticleRepository.findLatestArticleTimeByMemberId(member.getId());

MarketArticleEntity savedMarketArticle = marketArticleRepository.save(request.toEntity(
getMemberEntity(userPrincipal.getUsername())
));
boolean canCreateArticle = latestArticleTime.map(
time -> Duration.between(time, Instant.now()).toMinutes() >= 5
).orElse(true);

for (MultipartFile imageUrl : request.getImageUrls()) {
ProductImageEntity productImage = ProductImageEntity.of(
articleImageService.uploadProfileImage(imageUrl),
savedMarketArticle
);
productImageRepository.save(productImage);
}
if (canCreateArticle) {
MarketArticleEntity savedMarketArticle = marketArticleRepository.save(request.toEntity(
getMemberEntity(userPrincipal.getUsername())
));

return savedMarketArticle.getId();
for (MultipartFile imageUrl : request.getImageUrls()) {
ProductImageEntity productImage = ProductImageEntity.of(
articleImageService.uploadProfileImage(imageUrl),
savedMarketArticle
);
productImageRepository.save(productImage);
}

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

/**
Expand Down Expand Up @@ -212,10 +225,10 @@ public Optional<MarketBookmarkEntity> updateArticleLike(
MarketArticleEntity marketArticle = getMarketArticleEntity(articleId);
MemberEntity member = getMemberEntity(principal.getUsername());
Optional<MarketBookmarkEntity> marketBookmark = marketBookmarkRepository.findByMarketArticleAndMemberEntity(marketArticle, member);
if(marketBookmark.isPresent()){
if (marketBookmark.isPresent()) {
deleteBookmark(articleId, principal);
return Optional.empty();
} else{
} else {
return Optional.of(createBookmark(articleId, principal));
}
}
Expand Down Expand Up @@ -254,8 +267,8 @@ private void deleteBookmark(
}

@Transactional(readOnly = true)
public List<ArticleDto> getAllBookmarks( MemberEntity loginMemberEntity
) {
public List<ArticleDto> getAllBookmarks(MemberEntity loginMemberEntity
) {
List<MarketArticleEntity> marketArticles = marketBookmarkRepository.findAllByMemberEntity(
loginMemberEntity
)
Expand Down

0 comments on commit 0ede3ee

Please sign in to comment.