Skip to content

Commit

Permalink
feat: 설문조사할 query DB에 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
inpink committed Mar 29, 2024
1 parent 94d0efe commit 4ae38b5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/main/java/knusearch/clear/ClearApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package knusearch.clear;

import knusearch.clear.survey.SurveyQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/knusearch/clear/survey/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
http
.authorizeHttpRequests(requests -> requests
.requestMatchers(
new AntPathRequestMatcher("/"),
new AntPathRequestMatcher("/home"),
new AntPathRequestMatcher("/search"),
new AntPathRequestMatcher("/searchResult"),
new AntPathRequestMatcher("/survey/login"))
new AntPathRequestMatcher("/**"))
.permitAll()
.anyRequest().authenticated()
)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/knusearch/clear/survey/SurveyController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package knusearch.clear.survey;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.stereotype.Controller;
Expand All @@ -12,7 +13,11 @@

@Controller
@Slf4j
@RequiredArgsConstructor
public class SurveyController {

private final SurveyQueryService surveyQueryService;

@GetMapping("/survey/login")
public String loginForm(Model model, HttpServletRequest request) {
CsrfToken csrfToken = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
Expand All @@ -39,4 +44,12 @@ public String handleSurveyQuery(@PathVariable("number") int number) {
return "hello";
}


@GetMapping("/survey/updateQueries")
public String updateQueries() {
surveyQueryService.loadQueriesFromFile("static/surveyQueries.txt");

return "hello";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

@Repository
public interface SurveyQueryRepository extends JpaRepository<SurveyQuery, Long> {


}
33 changes: 20 additions & 13 deletions src/main/java/knusearch/clear/survey/SurveyQueryService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
package knusearch.clear.survey;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SurveyQueryService {

@Autowired
private SurveyQueryRepository repository;
private final SurveyQueryRepository surveyQueryRepository;

@Transactional
public void loadQueriesFromFile(String filePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
List<SurveyQuery> queries = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
SurveyQuery query = new SurveyQuery();
query.setQuery(line);
queries.add(query);
public void loadQueriesFromFile(String resourcePath) {
try {
System.out.println("ㅎㅇ");
InputStream resourceStream = new ClassPathResource(resourcePath).getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream))) {
List<SurveyQuery> queries = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
SurveyQuery query = new SurveyQuery();
query.setQuery(line);
queries.add(query);
}
surveyQueryRepository.saveAll(queries);
}
repository.saveAll(queries);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
File renamed without changes.

0 comments on commit 4ae38b5

Please sign in to comment.