Skip to content

Commit 2b00363

Browse files
committed
feat(backend): add api for create spreadsheet
1 parent 286bbc4 commit 2b00363

File tree

7 files changed

+79
-13
lines changed

7 files changed

+79
-13
lines changed

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@
9393
<scope>provided</scope>
9494
<version>${lombok.version}</version>
9595
</dependency>
96+
<dependency>
97+
<groupId>com.fasterxml.jackson.core</groupId>
98+
<artifactId>jackson-core</artifactId>
99+
<version>${jackson.version}</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>com.fasterxml.jackson.core</groupId>
103+
<artifactId>jackson-databind</artifactId>
104+
<version>${jackson.version}</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>com.fasterxml.jackson.core</groupId>
108+
<artifactId>jackson-annotations</artifactId>
109+
<version>${jackson.version}</version>
110+
</dependency>
96111
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
97112
<dependency>
98113
<artifactId>spring-boot-starter</artifactId>

scalable-ot-api/src/main/java/com/brotherjing/controller/DocController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public class DocController {
3030
@RequestMapping(value = "/create", method = RequestMethod.POST)
3131
@ResponseBody
3232
BaseProto.Snapshot create() {
33-
return docService.create();
33+
return docService.create(BaseProto.DocType.PLAIN_TEXT);
34+
}
35+
36+
@RequestMapping(value = "/create/sheet", method = RequestMethod.POST)
37+
@ResponseBody
38+
BaseProto.Snapshot createSheet() {
39+
return docService.create(BaseProto.DocType.JSON);
3440
}
3541

3642
@GetMapping(value = "/{docId}/fetch")

scalable-ot-core/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@
5252
<groupId>junit</groupId>
5353
<scope>test</scope>
5454
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-core</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.fasterxml.jackson.core</groupId>
61+
<artifactId>jackson-databind</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.fasterxml.jackson.core</groupId>
65+
<artifactId>jackson-annotations</artifactId>
66+
</dependency>
5567
</dependencies>
5668

5769
<build>

scalable-ot-core/src/main/java/com/brotherjing/core/dto/SnapshotDto.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@
1313
@Data
1414
@Document
1515
@CompoundIndex(def = "{'docId': 1, 'version': 1}")
16-
public class SnapshotDto implements Serializable {
16+
public class SnapshotDto implements Serializable, Cloneable {
1717
@Id
1818
String id;
1919

2020
String docId;
2121
String data;
2222
int version;
23+
24+
@Override
25+
public SnapshotDto clone() {
26+
return new SnapshotDtoBuilder()
27+
.id(id)
28+
.docId(docId)
29+
.data(data)
30+
.version(version)
31+
.build();
32+
}
2333
}

scalable-ot-core/src/main/java/com/brotherjing/core/service/DocService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.brotherjing.proto.BaseProto;
66

77
public interface DocService {
8-
BaseProto.Snapshot create();
8+
BaseProto.Snapshot create(BaseProto.DocType docType);
99

1010
BaseProto.Snapshot get(String docId);
1111

scalable-ot-core/src/main/java/com/brotherjing/core/service/impl/DocServiceImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.brotherjing.core.service.impl;
22

3+
import java.util.Arrays;
34
import java.util.Collections;
45
import java.util.List;
56
import java.util.stream.Collectors;
@@ -22,6 +23,8 @@
2223
import com.brotherjing.core.util.IDUtils;
2324
import com.brotherjing.core.util.ShortUUID;
2425
import com.brotherjing.proto.BaseProto;
26+
import com.fasterxml.jackson.core.JsonProcessingException;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
2528
import com.google.common.annotations.VisibleForTesting;
2629
import com.google.common.collect.Lists;
2730

@@ -39,25 +42,37 @@ public class DocServiceImpl implements DocService {
3942
private CommandDao commandDao;
4043

4144
@Override
42-
public BaseProto.Snapshot create() {
45+
public BaseProto.Snapshot create(BaseProto.DocType docType) {
4346
String docId = ShortUUID.generate();
4447
SnapshotDto dto = SnapshotDto.builder()
4548
.id(IDUtils.generateSnapshotPK(docId))
4649
.docId(docId)
47-
.data("")
50+
.data(getInitialData(docType))
4851
.version(0)
4952
.build();
5053
// also create initial snapshot
51-
SnapshotDto initSnapshot = SnapshotDto.builder()
52-
.id(IDUtils.generateSnapshotPK(docId, 0))
53-
.docId(docId)
54-
.data("")
55-
.version(0)
56-
.build();
54+
SnapshotDto initSnapshot = dto.clone();
55+
initSnapshot.setId(IDUtils.generateSnapshotPK(docId, 0));
5756
docDao.saveAll(Lists.newArrayList(dto, initSnapshot));
5857
return Converter.toSnapshotProto(dto);
5958
}
6059

60+
@VisibleForTesting
61+
String getInitialData(BaseProto.DocType docType) {
62+
if (BaseProto.DocType.JSON.equals(docType)) {
63+
String[][] data = new String[10][10];
64+
for (String[] row : data) {
65+
Arrays.fill(row, "");
66+
}
67+
try {
68+
return new ObjectMapper().writeValueAsString(data);
69+
} catch (JsonProcessingException e) {
70+
return "{}";
71+
}
72+
}
73+
return "";
74+
}
75+
6176
@Override
6277
public BaseProto.Snapshot get(String docId) {
6378
return docDao.findById(IDUtils.generateSnapshotPK(docId))

scalable-ot-core/src/test/java/com/brotherjing/core/service/impl/DocServiceImplTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import com.brotherjing.Const;
1919
import com.brotherjing.config.MongoConfig;
2020
import com.brotherjing.core.dto.SnapshotDto;
21+
import com.brotherjing.core.executor.CommandExecutorRegistry;
22+
import com.brotherjing.core.executor.impl.TextCommandExecutor;
2123
import com.brotherjing.proto.BaseProto;
2224

2325
@RunWith(SpringJUnit4ClassRunner.class)
2426
@ContextConfiguration(classes = MongoConfig.class)
25-
@Import(DocServiceImpl.class)
27+
@Import({ DocServiceImpl.class, CommandExecutorRegistry.class, TextCommandExecutor.class })
2628
public class DocServiceImplTest {
2729

2830
@Autowired
@@ -43,9 +45,15 @@ public void tearDown() {
4345
mongoOps.dropCollection(SnapshotDto.class);
4446
}
4547

48+
@Test
49+
public void testGetInitialData() {
50+
String data = docService.getInitialData(BaseProto.DocType.JSON);
51+
Assert.assertNotNull(data);
52+
}
53+
4654
@Test
4755
public void testTakeSnapshot() {
48-
BaseProto.Snapshot doc = docService.create();
56+
BaseProto.Snapshot doc = docService.create(BaseProto.DocType.PLAIN_TEXT);
4957
String docId = doc.getDocId();
5058
int toVersion = Const.TAKE_SNAPSHOT_INTERVAL + 1;
5159
List<BaseProto.Command> commands = IntStream.range(0, toVersion)

0 commit comments

Comments
 (0)