Skip to content

Commit 3c4ece1

Browse files
Updated CodeService
1 parent 6c982a5 commit 3c4ece1

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/platform/service/CodeService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package platform.service;
22

3+
import platform.model.entity.Code;
34
import platform.model.entity.CodeDTO;
4-
import platform.model.entity.NewCode;
5+
6+
import java.util.List;
57

68
public interface CodeService {
7-
CodeDTO getSnippet();
8-
void updateSnippet(NewCode newCode);
9+
Code getCodeById(long id);
10+
long putCode(CodeDTO newCode);
11+
List<Code> getLatestCode();
912
}
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
package platform.service;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.stereotype.Service;
45
import platform.model.entity.Code;
56
import platform.model.entity.CodeDTO;
6-
import platform.model.entity.NewCode;
77

88
import java.time.LocalDateTime;
99
import java.time.format.DateTimeFormatter;
10+
import java.util.List;
1011

1112
@Service
1213
public class CodeServiceImpl implements CodeService {
13-
private static final String DATE_FORMATTER= "yyyy/MM/dd HH:mm:ss";
14+
private static long currentId = 1;
1415

15-
Code code = new Code("public static void main(String[] args) {\n" +
16-
" SpringApplication.run(CodeSharingPlatform.class, args);\n" +
17-
"}", LocalDateTime.now());
16+
@Autowired
17+
private CodeRepository codeRepository;
18+
19+
private final String DATE_FORMATTER= "yyyy/MM/dd HH:mm:ss";
20+
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
21+
22+
@Override
23+
public Code getCodeById(long id) {
24+
return codeRepository.getCodeById(id);
25+
}
1826

1927
@Override
20-
public CodeDTO getSnippet() {
21-
CodeDTO codeDTO = new CodeDTO();
22-
codeDTO.setCode(code.getCode());
23-
codeDTO.setDate(code.getDate().format(DateTimeFormatter.ofPattern(DATE_FORMATTER)));
24-
return codeDTO;
28+
public long putCode(CodeDTO newCode) {
29+
long id = currentId++;
30+
LocalDateTime localDateTime = LocalDateTime.now();
31+
codeRepository.putCode(
32+
new Code(id, newCode.getCode(), formatter.format(localDateTime), localDateTime)
33+
);
34+
return id;
2535
}
2636

2737
@Override
28-
public void updateSnippet(NewCode newCode) {
29-
code = new Code(newCode.getCode(), LocalDateTime.now());
38+
public List<Code> getLatestCode() {
39+
return codeRepository.getLatestCode();
3040
}
3141
}

0 commit comments

Comments
 (0)