Skip to content

Commit

Permalink
최종이길
Browse files Browse the repository at this point in the history
  • Loading branch information
bikooju committed Jan 11, 2025
2 parents 5bb98ca + d62ccca commit ca880df
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 35 deletions.
16 changes: 16 additions & 0 deletions src/main/java/banban/springboot/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package banban.springboot.config;

import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("https://on-air.netlify.app")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@EnableWebSecurity
@Configuration
Expand All @@ -15,6 +20,7 @@ public class SecurityConfig {
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.cors(cors -> cors.configure(http)) // CORS 설정 추가
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/**").permitAll()
.anyRequest().authenticated()
Expand All @@ -23,4 +29,18 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://on-air.netlify.app"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
configuration.setMaxAge(3600L);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

}
1 change: 0 additions & 1 deletion src/main/java/banban/springboot/domain/entity/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class News {
@Column(nullable = false)
private boolean isBreakingNews;

@Column(nullable = false)
private LocalDateTime createdAt;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public interface NewsRepository extends JpaRepository<News,Long> {
List<News> findByTeamGroupAndIsBreakingNewsFalse(TeamGroup teamGroup);

List<News> findByTeamGroupAndCreatedAtBetween(TeamGroup teamGroup, LocalDateTime start, LocalDateTime end);
void deleteByCreatedAtBefore(LocalDateTime dateTime);
void deleteByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
}

21 changes: 17 additions & 4 deletions src/main/java/banban/springboot/service/NewsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ public List<NewsResponseDTO.NewsReadResponseDTO> getRegularNewsByGroupKey(String
.toList();
}

// private LocalDateTime getCurrentTime() {
// if (isTestMode()) {
// return NewsTestController.getMockCurrentTime();
// }
// return LocalDateTime.now();
// }

// private boolean isTestMode() {
// return Arrays.asList(environment.getActiveProfiles()).contains("test");
// }

private LocalDateTime getCurrentTime() {
return NewsTestController.getMockCurrentTime(); // 항상 테스트 시간 반환
}
Expand Down Expand Up @@ -180,7 +191,7 @@ public List<NewsResponseDTO.NewsYesterdayResponseDTO> getYesterdayNews(String gr

//LocalDateTime now = LocalDateTime.now();
LocalDateTime now = getCurrentTime();
LocalDateTime twoYesterdayAt6PM = now.minusDays(2).withHour(18).withMinute(0).withSecond(0);
LocalDateTime twoYesterdayAt6PM = now.minusDays(1).withHour(00).withMinute(0).withSecond(0);
LocalDateTime yesterdayAt6PM = now.minusDays(1).withHour(17).withMinute(59).withSecond(59);

List<News> yesterdayNews = newsRepository.findByTeamGroupAndCreatedAtBetween(
Expand All @@ -195,9 +206,11 @@ public List<NewsResponseDTO.NewsYesterdayResponseDTO> getYesterdayNews(String gr
@Transactional
public void deleteOldNews() {
LocalDateTime now = getCurrentTime(); // 수정된 부분
LocalDateTime yesterdayAt6PM = now.minusDays(1)
.withHour(18).withMinute(0).withSecond(0);
newsRepository.deleteByCreatedAtBefore(yesterdayAt6PM);
// LocalDateTime yesterdayAt6PM = now.minusDays(1)
// .withHour(18).withMinute(0).withSecond(0);
LocalDateTime twoYesterdayAt6PM = now.minusDays(1).withHour(00).withMinute(0).withSecond(0);
LocalDateTime yesterdayAt6PM = now.minusDays(1).withHour(17).withMinute(59).withSecond(59);
newsRepository.deleteByCreatedAtBetween(twoYesterdayAt6PM, yesterdayAt6PM);
}

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public ApiResponse<String> getCurrentTestTime() {
public static LocalDateTime getMockCurrentTime() {
return mockCurrentTime;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public class NewsRequestDTO {
@NotNull(message = "긍정인지 부정인지 작성해주세요")
private NewsCategories newsCategories;

// private LocalDateTime createdAt = LocalDateTime.now();
//private LocalDateTime createdAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import banban.springboot.domain.entity.News;
import banban.springboot.domain.enums.NewsCategories;
import banban.springboot.domain.enums.NewsCategories;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -22,18 +21,17 @@ public static class NewsCreateResponseDTO {
private String headline;
private String content;
private String images;
private boolean isBreakingNews;
private LocalDateTime createdAt;
private NewsCategories newsCategories;
private boolean isBreakingNews;

public static NewsCreateResponseDTO from(News news) {
return NewsCreateResponseDTO.builder()
.newsId(news.getId())
.headline(news.getHeadline())
.content(news.getContent())
.isBreakingNews(news.isBreakingNews())
.images(news.getThumbnail_URL())
.createdAt(news.getCreatedAt())
.newsCategories(news.getNewsCategories())
.build();
}
}
Expand All @@ -43,53 +41,50 @@ public static NewsCreateResponseDTO from(News news) {
@NoArgsConstructor
@Builder
@AllArgsConstructor
public static class NewsTodayResponseDTO {
public static class NewsReadResponseDTO {
private Long newsId;
private String headline;
private String content;
private String username;
private boolean isBreakingNews;
private int likes;
private LocalDateTime createdAt;
private NewsCategories newsCategories;
private String images;

public static NewsTodayResponseDTO from(News news) {
return NewsTodayResponseDTO.builder()
public static NewsReadResponseDTO from(News news) {
return NewsReadResponseDTO.builder()
.newsId(news.getId())
.headline(news.getHeadline())
.content(news.getContent())
.username(news.getMember().getUsername())
.isBreakingNews(news.isBreakingNews())
.likes(news.getLikes())
.createdAt(news.getCreatedAt())
.images(news.getThumbnail_URL())
.newsCategories(news.getNewsCategories())
.build();
}
}


@Getter
@Setter
@NoArgsConstructor
@Builder
@AllArgsConstructor
public static class NewsYesterdayResponseDTO {
public static class NewsTodayResponseDTO {
private Long newsId;
private String headline;
private String content;
private boolean isBreakingNews;
private int likes;
private String images;
private LocalDateTime createdAt;
private NewsCategories newsCategories;

public static NewsYesterdayResponseDTO from(News news) {
return NewsYesterdayResponseDTO.builder()
public static NewsTodayResponseDTO from(News news) {
return NewsTodayResponseDTO.builder()
.newsId(news.getId())
.headline(news.getHeadline())
.content(news.getContent())
.isBreakingNews(news.isBreakingNews())
.likes(news.getLikes())
.newsCategories(news.getNewsCategories())
.createdAt(news.getCreatedAt())
.images(news.getThumbnail_URL())
.build();
}
}
Expand All @@ -99,24 +94,22 @@ public static NewsYesterdayResponseDTO from(News news) {
@NoArgsConstructor
@Builder
@AllArgsConstructor
public static class NewsReadResponseDTO {
public static class NewsYesterdayResponseDTO {
private Long newsId;
private String headline;
private String content;
private String username;
private String images;
private NewsCategories newsCategories;
private boolean isBreakingNews;
private int likes;
private LocalDateTime createdAt;

public static NewsReadResponseDTO from(News news) {
return NewsReadResponseDTO.builder()
public static NewsYesterdayResponseDTO from(News news) {
return NewsYesterdayResponseDTO.builder()
.newsId(news.getId())
.headline(news.getHeadline())
.content(news.getContent())
.username(news.getMember().getUsername())
.isBreakingNews(news.isBreakingNews())
.images(news.getThumbnail_URL())
.newsCategories(news.getNewsCategories())
.likes(news.getLikes())
.createdAt(news.getCreatedAt())
.build();
}
}
Expand Down

0 comments on commit ca880df

Please sign in to comment.