Skip to content

Commit 5e74d56

Browse files
committed
Feat: add save-text api
1 parent 0a6035c commit 5e74d56

File tree

11 files changed

+66
-1
lines changed

11 files changed

+66
-1
lines changed

server/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ repositories {
2727
dependencies {
2828
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
2929
implementation 'org.springframework.boot:spring-boot-starter-web'
30+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0'
3031
compileOnly 'org.projectlombok:lombok'
3132
developmentOnly 'org.springframework.boot:spring-boot-devtools'
3233
annotationProcessor 'org.projectlombok:lombok'
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
spring.application.name=swdc
1+
spring.application.name=swdc
2+
server.port=8090
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.swdc.server.controller;
2+
3+
import com.swdc.server.service.TextService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.io.IOException;
11+
12+
@RestController
13+
@RequestMapping("/save-text")
14+
public class TextController {
15+
16+
@Autowired
17+
private TextService textService;
18+
19+
@PostMapping("")
20+
public String saveTextToFile(@RequestBody String request) {
21+
try {
22+
textService.saveText(request);
23+
return "File saved successfully!";
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
return "Failed to save file: " + e.getMessage();
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.swdc.server.service;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.time.LocalDate;
10+
import java.time.LocalDateTime;
11+
import java.time.ZoneId;
12+
import java.time.format.DateTimeFormatter;
13+
14+
@Service
15+
public class TextService {
16+
17+
private static final String FILE_PATH = "/mnt/report/report.txt";
18+
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
19+
private final ObjectMapper objectMapper = new ObjectMapper();
20+
21+
public void saveText(String request) throws IOException {
22+
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
23+
String formattedTime = now.format(timeFormatter);
24+
25+
ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(request);
26+
jsonNode.put("time", formattedTime);
27+
28+
String updatedJson = objectMapper.writeValueAsString(jsonNode);
29+
30+
try (FileWriter fileWriter = new FileWriter(FILE_PATH, true)) {
31+
fileWriter.write(updatedJson + "\n"); // JSON 데이터 추가 (한 줄로)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)