Skip to content

Commit 7d454a1

Browse files
authored
Merge pull request #8 from hyohyo-zz/sprint3-advenced
Sprint3 advenced
2 parents e20a6a2 + 78af390 commit 7d454a1

16 files changed

+233
-74
lines changed

src/main/java/com/sprint/mission/discodeit/DiscodeitApplication.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sprint.mission.discodeit;
22

3+
import com.sprint.mission.discodeit.config.DiscodeitProperties;
34
import com.sprint.mission.discodeit.dto.Response.ChannelResponse;
45
import com.sprint.mission.discodeit.dto.Response.MessageResponse;
56
import com.sprint.mission.discodeit.dto.Response.UserResponse;
@@ -17,19 +18,24 @@
1718
import com.sprint.mission.discodeit.util.DataInitializer;
1819
import org.springframework.boot.SpringApplication;
1920
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2022
import org.springframework.context.ConfigurableApplicationContext;
2123

2224
import java.util.*;
2325
import java.util.stream.Collectors;
2426

2527
@SpringBootApplication
28+
@EnableConfigurationProperties(DiscodeitProperties.class)
2629
public class DiscodeitApplication {
2730

2831
public static void main(String[] args) {
32+
ConfigurableApplicationContext context = SpringApplication.run(DiscodeitApplication.class, args);
2933
//초기화
30-
DataInitializer.clearSerializedData();
34+
DataInitializer initializer = context.getBean(DataInitializer.class);
35+
initializer.clearSerializedData();
3136

32-
ConfigurableApplicationContext context = SpringApplication.run(DiscodeitApplication.class, args);
37+
context.close();
38+
context = SpringApplication.run(DiscodeitApplication.class, args);
3339

3440
//콩받기
3541
UserService userService = context.getBean(UserService.class);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sprint.mission.discodeit.config;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
7+
@Getter @Setter
8+
@ConfigurationProperties(prefix = "discodeit.repository")
9+
public class DiscodeitProperties {
10+
private String type;
11+
private String filePath;
12+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.sprint.mission.discodeit.config;
2+
3+
import com.sprint.mission.discodeit.repository.*;
4+
import com.sprint.mission.discodeit.repository.file.*;
5+
import com.sprint.mission.discodeit.repository.jcf.*;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
@RequiredArgsConstructor
12+
public class RepositoryConfig {
13+
14+
private final DiscodeitProperties properties;
15+
16+
@Bean
17+
public UserRepository userRepository(ChannelRepository channelRepository) {
18+
String type = properties.getType();
19+
20+
if ("file".equalsIgnoreCase(type)) {
21+
return new FileUserRepository(properties, channelRepository);
22+
} else { // "jcf" 또는 null인 경우 기본값
23+
return new JCFUserRepository();
24+
}
25+
}
26+
@Bean
27+
public ChannelRepository channelRepository() {
28+
String type = properties.getType();
29+
if ("file".equalsIgnoreCase(type)) {
30+
return new FileChannelRepository(properties);
31+
} else {
32+
return new JCFChannelRepository();
33+
}
34+
}
35+
36+
@Bean
37+
public MessageRepository messageRepository(UserRepository userRepository, ChannelRepository channelRepository) {
38+
String type = properties.getType();
39+
if ("file".equalsIgnoreCase(type)) {
40+
return new FileMessageRepository(properties, userRepository, channelRepository);
41+
} else {
42+
return new JCFMessageRepository();
43+
}
44+
}
45+
46+
@Bean
47+
public ReadStatusRepository readStatusRepository() {
48+
String type = properties.getType();
49+
if ("file".equalsIgnoreCase(type)) {
50+
return new FileReadStatusRepository(properties);
51+
} else {
52+
return new JCFReadStatusRepository();
53+
}
54+
}
55+
56+
@Bean
57+
public UserStatusRepository userStatusRepository() {
58+
String type = properties.getType();
59+
if ("file".equalsIgnoreCase(type)) {
60+
return new FileUserStatusRepository(properties);
61+
} else {
62+
return new JCFUserStatusRepository();
63+
}
64+
}
65+
66+
@Bean
67+
public BinaryContentRepository binaryContentRepository() {
68+
String type = properties.getType();
69+
if ("file".equalsIgnoreCase(type)) {
70+
return new FileBinaryContentRepository(properties);
71+
} else {
72+
return new JCFBinaryContentRepository();
73+
}
74+
}
75+
}

src/main/java/com/sprint/mission/discodeit/repository/file/FileBinaryContentRepository.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package com.sprint.mission.discodeit.repository.file;
22

3+
import com.sprint.mission.discodeit.config.DiscodeitProperties;
34
import com.sprint.mission.discodeit.entity.BinaryContent;
45
import com.sprint.mission.discodeit.repository.BinaryContentRepository;
5-
import org.springframework.stereotype.Repository;
6+
import jakarta.annotation.PostConstruct;
67

78
import java.io.*;
89
import java.util.*;
910
import java.util.stream.Collectors;
1011

11-
import static com.sprint.mission.discodeit.util.DataInitializer.BINARYCONTENT_FILE_PATH;
1212

13-
@Repository
1413
public class FileBinaryContentRepository implements BinaryContentRepository {
15-
private final String FILE_PATH = BINARYCONTENT_FILE_PATH;
14+
private static final long serialVersionUID = 1L;
1615

17-
private Map<UUID, BinaryContent> data = loadData();
16+
private final String filePath;
17+
private Map<UUID, BinaryContent> data;
18+
19+
public FileBinaryContentRepository(DiscodeitProperties properties) {
20+
this.filePath = properties.getFilePath() + "/binarycontent.ser";
21+
this.data = loadData();
22+
}
1823

1924
@Override
2025
public BinaryContent save(BinaryContent binaryContent) {
@@ -84,7 +89,7 @@ public boolean deleteByMessageId(UUID messageId) {
8489
}
8590

8691
private void saveData() {
87-
try (FileOutputStream fos = new FileOutputStream(FILE_PATH);
92+
try (FileOutputStream fos = new FileOutputStream(filePath);
8893
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
8994
oos.writeObject(data);
9095
} catch (IOException e) {
@@ -96,7 +101,7 @@ private void saveData() {
96101
// 불러오기 메서드
97102
@SuppressWarnings("unchecked")
98103
private Map<UUID, BinaryContent> loadData() {
99-
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
104+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
100105
return (Map<UUID, BinaryContent>) ois.readObject();
101106
} catch (FileNotFoundException e) {
102107
System.out.println("[binaryContent] 저장된 파일이 없습니다. 새 데이터를 시작합니다.");

src/main/java/com/sprint/mission/discodeit/repository/file/FileChannelRepository.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package com.sprint.mission.discodeit.repository.file;
22

3+
import com.sprint.mission.discodeit.config.DiscodeitProperties;
34
import com.sprint.mission.discodeit.entity.Channel;
45
import com.sprint.mission.discodeit.entity.User;
56
import com.sprint.mission.discodeit.repository.ChannelRepository;
6-
import org.springframework.stereotype.Repository;
7+
import jakarta.annotation.PostConstruct;
78

89
import java.io.*;
910
import java.util.*;
1011
import java.util.stream.Collectors;
1112

12-
import static com.sprint.mission.discodeit.util.DataInitializer.CHANNEL_FILE_PATH;
1313

14-
@Repository
1514
public class FileChannelRepository implements ChannelRepository {
16-
private final String FILE_PATH = CHANNEL_FILE_PATH;
15+
private static final long serialVersionUID = 1L;
1716

18-
private Map<UUID, Channel> data = loadData();
17+
private final String filePath;
18+
private Map<UUID, Channel> data;
1919

20+
public FileChannelRepository(DiscodeitProperties properties) {
21+
if (properties.getFilePath() == null) {
22+
throw new IllegalStateException("filePath 설정이 null입니다. application.yaml 설정 확인 필요");
23+
}
24+
this.filePath = properties.getFilePath() + "/channel.ser";
25+
this.data = new HashMap<>();
26+
}
27+
28+
// 파일 있으면 불러오기
29+
@PostConstruct
30+
public void init() {
31+
this.data = loadData();
32+
}
2033
//채널 생성
2134
@Override
2235
public Channel create(Channel channel) {
@@ -68,24 +81,24 @@ public Set<User> members(UUID id) {
6881
}
6982

7083
private void saveData() {
71-
try (FileOutputStream fos = new FileOutputStream(FILE_PATH);
84+
try (FileOutputStream fos = new FileOutputStream(filePath);
7285
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
7386
oos.writeObject(data);
7487
} catch (IOException e) {
75-
System.err.println("[메시지] 데이터 저장 중 오류 발생: " + e.getMessage());
88+
System.err.println("[채널] 데이터 저장 중 오류 발생: " + e.getMessage());
7689
e.printStackTrace();
7790
}
7891
}
7992

8093
// 불러오기 메서드
8194
@SuppressWarnings("unchecked")
8295
private Map<UUID, Channel> loadData() {
83-
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
96+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
8497
return (Map<UUID, Channel>) ois.readObject();
8598
} catch (FileNotFoundException e) {
86-
System.out.println("[메시지] 저장된 파일이 없습니다. 새 데이터를 시작합니다.");
99+
System.out.println("[채널] 저장된 파일이 없습니다. 새 데이터를 시작합니다.");
87100
} catch (IOException | ClassNotFoundException e) {
88-
System.out.println("[메시지] 데이터 불러오기 중 오류 발생: " + e.getMessage());
101+
System.out.println("[채널] 데이터 불러오기 중 오류 발생: " + e.getMessage());
89102
e.printStackTrace();
90103
}
91104
// 실패 시 빈 Map 반환

src/main/java/com/sprint/mission/discodeit/repository/file/FileMessageRepository.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
package com.sprint.mission.discodeit.repository.file;
22

3+
import com.sprint.mission.discodeit.config.DiscodeitProperties;
34
import com.sprint.mission.discodeit.entity.Message;
45
import com.sprint.mission.discodeit.repository.ChannelRepository;
56
import com.sprint.mission.discodeit.repository.MessageRepository;
67
import com.sprint.mission.discodeit.repository.UserRepository;
7-
import org.springframework.stereotype.Repository;
8+
import jakarta.annotation.PostConstruct;
89

910
import java.io.*;
1011
import java.time.Instant;
1112
import java.util.*;
1213

13-
import static com.sprint.mission.discodeit.util.DataInitializer.MESSAGE_FILE_PATH;
1414

15-
@Repository
1615
public class FileMessageRepository implements MessageRepository {
1716
private static final long serialVersionUID = 1L;
1817

19-
private final String FILE_PATH = MESSAGE_FILE_PATH;
20-
18+
private final String filePath;
2119
private final UserRepository userRepository;
2220
private final ChannelRepository channelRepository;
21+
private Map<UUID, Message> data;
2322

24-
private final Map<UUID, Message> data = loadData();
25-
26-
public FileMessageRepository(UserRepository userRepository, ChannelRepository channelRepository) {
23+
public FileMessageRepository(DiscodeitProperties properties, UserRepository userRepository, ChannelRepository channelRepository) {
24+
if (properties.getFilePath() == null) {
25+
throw new IllegalStateException("filePath 설정이 null입니다. application.yaml 설정 확인 필요");
26+
}
27+
this.filePath = properties.getFilePath() + "/message.ser";
2728
this.userRepository = userRepository;
2829
this.channelRepository = channelRepository;
30+
this.data = new HashMap<>();
2931
}
3032

33+
// 파일 있으면 불러오기
34+
@PostConstruct
35+
public void init() {
36+
this.data = loadData();
37+
}
3138
//메시지 생성
3239
@Override
3340
public Message create(Message message) {
@@ -88,7 +95,7 @@ public boolean deleteByChannelId(UUID channelId) {
8895
}
8996

9097
private void saveData() {
91-
try (FileOutputStream fos = new FileOutputStream(FILE_PATH);
98+
try (FileOutputStream fos = new FileOutputStream(filePath);
9299
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
93100
oos.writeObject(data);
94101
} catch (IOException e) {
@@ -100,7 +107,7 @@ private void saveData() {
100107
// 불러오기 메서드
101108
@SuppressWarnings("unchecked")
102109
private Map<UUID, Message> loadData() {
103-
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
110+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
104111
return (Map<UUID, Message>) ois.readObject();
105112
} catch (FileNotFoundException e) {
106113
System.out.println("[메시지] 저장된 파일이 없습니다. 새 데이터를 시작합니다.");

src/main/java/com/sprint/mission/discodeit/repository/file/FileReadStatusRepository.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
package com.sprint.mission.discodeit.repository.file;
22

3+
import com.sprint.mission.discodeit.config.DiscodeitProperties;
34
import com.sprint.mission.discodeit.entity.ReadStatus;
45
import com.sprint.mission.discodeit.repository.ReadStatusRepository;
5-
import org.springframework.stereotype.Repository;
6+
import jakarta.annotation.PostConstruct;
67

78
import java.io.*;
89
import java.util.*;
910
import java.util.stream.Collectors;
1011

11-
import static com.sprint.mission.discodeit.util.DataInitializer.READSTATUS_FILE_PATH;
1212

13-
@Repository
1413
public class FileReadStatusRepository implements ReadStatusRepository {
15-
private final String FILE_PATH = READSTATUS_FILE_PATH;
14+
private static final long serialVersionUID = 1L;
1615

17-
private Map<UUID, ReadStatus> data = loadData();
16+
private final String filePath;
17+
private Map<UUID, ReadStatus> data;
18+
19+
public FileReadStatusRepository(DiscodeitProperties properties) {
20+
if (properties.getFilePath() == null) {
21+
throw new IllegalStateException("filePath 설정이 null입니다. application.yaml 설정 확인 필요");
22+
}
23+
this.filePath = properties.getFilePath() + "/readstatus.ser";
24+
this.data = new HashMap<>();
25+
}
26+
27+
// 파일 있으면 불러오기
28+
@PostConstruct
29+
public void init() {
30+
this.data = loadData();
31+
}
1832

1933
@Override
2034
public ReadStatus create(ReadStatus readStatus) {
@@ -91,7 +105,7 @@ public boolean deleteByChannelId(UUID channelId) {
91105

92106

93107
private void saveData() {
94-
try (FileOutputStream fos = new FileOutputStream(FILE_PATH);
108+
try (FileOutputStream fos = new FileOutputStream(filePath);
95109
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
96110
oos.writeObject(data);
97111
} catch (IOException e) {
@@ -103,7 +117,7 @@ private void saveData() {
103117
// 불러오기 메서드
104118
@SuppressWarnings("unchecked")
105119
private Map<UUID, ReadStatus> loadData() {
106-
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
120+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
107121
return (Map<UUID, ReadStatus>) ois.readObject();
108122
} catch (FileNotFoundException e) {
109123
System.out.println("[readStatus] 저장된 파일이 없습니다. 새 데이터를 시작합니다.");

0 commit comments

Comments
 (0)