Skip to content

Commit 0c98b69

Browse files
feat: implement JSON file-based project persistence
Introduce `app.projects.file` configuration property to specify the JSON file path for project data. Add `JsonConfig` for `ObjectMapper` bean definition. Update `ProjectJsonReaderWriterImpl` to handle reading and writing `Project` objects as JSON.
1 parent 6f79493 commit 0c98b69

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.preponderous.parpt.config;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class JsonConfig {
9+
10+
@Bean
11+
public ObjectMapper objectMapper() {
12+
return new ObjectMapper();
13+
}
14+
}
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
1+
12
package com.preponderous.parpt.repo;
23

4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.preponderous.parpt.domain.Project;
6+
import org.springframework.beans.factory.annotation.Value;
47
import org.springframework.stereotype.Component;
58

9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.util.ArrayList;
612
import java.util.List;
713

814
@Component
915
public class ProjectJsonReaderWriterImpl implements ProjectJsonReaderWriter {
16+
17+
private final String projectsFilePath;
18+
private final ObjectMapper objectMapper;
19+
20+
public ProjectJsonReaderWriterImpl(
21+
@Value("${app.projects.file}") String projectsFilePath,
22+
ObjectMapper objectMapper) {
23+
this.projectsFilePath = projectsFilePath;
24+
this.objectMapper = objectMapper;
25+
}
26+
1027
@Override
1128
public void writeJson(List<Project> projects) {
12-
throw new UnsupportedOperationException("Not supported yet.");
29+
if (projects == null) {
30+
throw new IllegalArgumentException("Projects list cannot be null");
31+
}
32+
33+
try {
34+
objectMapper.writeValue(new File(projectsFilePath), projects);
35+
} catch (IOException e) {
36+
throw new RuntimeException("Failed to write projects to JSON file", e);
37+
}
1338
}
1439

1540
@Override
1641
public List<Project> readJson() {
17-
throw new UnsupportedOperationException("Not supported yet.");
42+
File file = new File(projectsFilePath);
43+
44+
if (!file.exists() || file.length() == 0) {
45+
return new ArrayList<>();
46+
}
47+
48+
try {
49+
return objectMapper.readValue(file,
50+
objectMapper.getTypeFactory().constructCollectionType(List.class, Project.class));
51+
} catch (IOException e) {
52+
throw new RuntimeException("Failed to read projects from JSON file", e);
53+
}
1854
}
19-
}
55+
}

src/main/resources/application.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ parpt:
3434
- "How long will this take to build? (1=very quick, 5=very long) "
3535
- "How many people need to be involved? (1=just a few people, 5=many teams) "
3636
- "Will this be hard to maintain? (1=very easy, 5=very difficult) "
37-
- "Does this need ongoing work? (1=set and forget, 5=lots of upkeep) "
37+
- "Does this need ongoing work? (1=set and forget, 5=lots of upkeep) "
38+
app:
39+
projects:
40+
file: projects.json

0 commit comments

Comments
 (0)