Skip to content

Commit

Permalink
#44 [Update] 스케쥴러를 통해 Store의 북마크, 별점 평균 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 29, 2022
1 parent acfe257 commit af97963
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/mpnp/baechelin/BaechelinApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableJpaAuditing
@EnableConfigurationProperties({
CorsProperties.class,
AppProperties.class
})
@EnableScheduling
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@PropertySources({@PropertySource("classpath:application-key.properties")})
public class BaechelinApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.domain.StoreImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

public interface StoreRepository extends JpaRepository<Store, Long> {
@Modifying
@Query("UPDATE Store s set s.pointAvg = :avg where s.id = :storeId")
void updateAvg(@Param("avg") double avg, @Param("storeId") Long storeId);

@Query("SELECT AVG(p.point) FROM Store s join s.reviewList p where s.id = :id")
double getAvg(@Param("id") Long id);

@Modifying
@Query("UPDATE Store s set s.bookMarkCount = :count where s.id = :storeId")
void updateBookmarkCnt(@Param("count") int count, @Param("storeId") Long storeId);

@Query("SELECT COUNT(b) FROM Store s join s.bookmarkList b where s.id = :id")
int getBookmarkCnt(@Param("id") Long id);
}
21 changes: 21 additions & 0 deletions src/main/java/com/mpnp/baechelin/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mpnp.baechelin.bookmark.domain.Bookmark;
import com.mpnp.baechelin.bookmark.repository.BookmarkRepository;
import com.mpnp.baechelin.common.QuerydslLocation;
import com.mpnp.baechelin.exception.CustomException;
import com.mpnp.baechelin.exception.ErrorCode;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.review.domain.ReviewImage;
import com.mpnp.baechelin.store.domain.Store;
Expand All @@ -18,6 +20,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
Expand All @@ -31,6 +35,8 @@
@Service
@Transactional
@RequiredArgsConstructor
@EnableScheduling
@Slf4j
public class StoreService {

private final StoreRepository storeRepository;
Expand Down Expand Up @@ -246,4 +252,19 @@ public List<StoreCardResponseDto> searchStores(String sido, String sigungu, Stri
}
return result;
}

@Scheduled(cron = "0 0 0-23 * * *")
public void updateSchedule() {
List<Store> storeList = storeRepository.findAll();
for (Store store : storeList) {
if (!store.getReviewList().isEmpty()) {
double storeAvg = Double.parseDouble(String.format("%.1f", storeRepository.getAvg(store.getId())));
storeRepository.updateAvg(storeAvg, store.getId());
}
if (!store.getBookmarkList().isEmpty()) {
int bookmarkCnt = storeRepository.getBookmarkCnt(store.getId());
storeRepository.updateBookmarkCnt(bookmarkCnt,store.getId());
}
}
}
}

0 comments on commit af97963

Please sign in to comment.