Skip to content

Commit

Permalink
Merge pull request #222 from themoment-team/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
siwony authored Oct 11, 2021
2 parents fb2b13c + 40cf583 commit 74f8819
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 19 deletions.
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true
19 changes: 18 additions & 1 deletion src/main/java/com/moment/the/TheApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package com.moment.the;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.TimeZone;

@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
@Slf4j
public class TheApplication {

/**
* springboot를 한국 표준시로 설정합니다.
* `@PostConstruct`를 사용하여 메소드를 단 한번만 호출 되도록 합니다.
* @author 전지환
*/
@PostConstruct
public void setTimeZone(){
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
log.info("========== server start time: {}", new Date());
}
public static void main(String[] args) {
SpringApplication.run(TheApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.moment.the.uncomfortable.repository;

import com.moment.the.uncomfortable.dto.UncomfortableResponseDto;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
Expand All @@ -29,4 +26,11 @@ public interface UncomfortableCustomRepository {
* @author 정시원
*/
List<UncomfortableResponseDto> uncomfortableViewTopBy(int limit);

/**
* 불편한순간에 좋아요를 모두 0으로 초기화 합니다.
* @return long - 수정 된 게시글 수
* @author 전지환
*/
long formatAllGoods();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.moment.the.uncomfortable.repository;

import com.moment.the.uncomfortable.dto.UncomfortableResponseDto;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.hibernate.criterion.Projection;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

import java.util.List;

Expand Down Expand Up @@ -63,4 +57,18 @@ public List<UncomfortableResponseDto> uncomfortableViewTopBy(int limit) {
.orderBy(uncomfortableDomain.goods.desc())
.fetch();
}

/**
* 불편한순간에 좋아요를 모두 0으로 초기화 합니다.
* @return long - 수정 된 게시글 수
* @author 전지환
*/
@Override
public long formatAllGoods() {
return queryFactory
.update(uncomfortableDomain)
.where(uncomfortableDomain.goods.eq(0).not())
.set(uncomfortableDomain.goods, 0)
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import com.moment.the.uncomfortable.repository.UncomfortableRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

Expand Down Expand Up @@ -117,10 +119,16 @@ private static int calculateAfterDate() {
return period;
}

private void refreshGoodsOnThisDayOfEveryMonth(){
LocalDate today = LocalDate.now();
if (today.getDayOfMonth() == 1 || today.getDayOfMonth() == 14){

}
/**
* 모든 게시글의 좋아요를 0으로 초기화 하는 스케쥴러
* cron -> 요일(x)-매달-1,14일-00:00:00
* @author 전지환
*/
@Transactional
@Scheduled(cron = "0 0 0 1,14 * ?")
public void formatAllGoods(){
log.info("======= Initialization scheduler operation: {}", LocalDateTime.now());
long l = uncomfortableRepository.formatAllGoods();
log.info("======= {} changes have occurred at {}", l, LocalDateTime.now());
}
}
61 changes: 58 additions & 3 deletions src/test/java/com/moment/the/service/UncomfortableServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@
import com.moment.the.uncomfortable.dto.UncomfortableSetDto;
import com.moment.the.uncomfortable.repository.UncomfortableRepository;
import com.moment.the.uncomfortable.service.UncomfortableService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.Period;
import org.springframework.test.annotation.Commit;

import javax.transaction.Transactional;
import java.time.*;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
@Slf4j
class UncomfortableServiceTest {

@Autowired
Expand Down Expand Up @@ -186,4 +197,48 @@ public void cleanUp(){
});

}

@Test @Disabled
@DisplayName("cron식 기간에 알맞게 좋아요가 모두 0으로 초기화 되나요?")
void formatAllGoodsIsWorking() throws InterruptedException {
/**
* uncomfortableEntities: 좋아요가 있는 불편함 2개
* uncomfortableEntities_2: 좋아요가 없는 불편함 2개
*/
List<UncomfortableDomain> uncomfortableEntities = Stream.generate(
() -> UncomfortableDomain.builder()
.content("좋아요가 있는 불편함")
.goods(2)
.build()
).limit(2).collect(Collectors.toList());
List<UncomfortableDomain> uncomfortableEntities_2 = Stream.generate(
() -> UncomfortableDomain.builder()
.content("좋아요가 없는 불편함")
.goods(0)
.build()
).limit(2).collect(Collectors.toList());

List<UncomfortableDomain> uncomfortableDomains = tableRepo.saveAll(uncomfortableEntities);
List<UncomfortableDomain> uncomfortableDomains_2 = tableRepo.saveAll(uncomfortableEntities_2);

Thread.sleep(12*1000);
}

@Test @Disabled
@DisplayName("LocalDateTime과 TimeZone이 연관이 있나요?")
void checkTimeSet(){
// Given real-KST
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
LocalDateTime now = LocalDateTime.now();
log.info("============== this time is: {}", now);

// Given - manipulation localDateTime
ZoneId seoul = ZoneId.of("Asia/Seoul");
ZonedDateTime theTime = ZonedDateTime.of(2021, 10, 13, 23, 59, 59, 0, seoul);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
LocalDateTime afterManipulationTime = LocalDateTime.now();
log.info("============= modified date is: {}", afterManipulationTime);

LocalDateTime of = LocalDateTime.of(2021, 10, 13, 23, 59, 59);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spring:
web-allow-others: true
# Mysql RDB 설정
datasource:
url: jdbc:h2:mem:test
url: jdbc:h2:tcp://localhost/~/the-moment
username: sa
password:
driver-class-name: org.h2.Driver
Expand Down

0 comments on commit 74f8819

Please sign in to comment.