|
1 | 1 | package platform.service;
|
2 | 2 |
|
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
3 | 4 | import org.springframework.stereotype.Service;
|
4 | 5 | import platform.model.entity.Code;
|
5 | 6 | import platform.model.entity.CodeDTO;
|
6 |
| -import platform.model.entity.NewCode; |
7 | 7 |
|
8 | 8 | import java.time.LocalDateTime;
|
9 | 9 | import java.time.format.DateTimeFormatter;
|
| 10 | +import java.util.List; |
10 | 11 |
|
11 | 12 | @Service
|
12 | 13 | public class CodeServiceImpl implements CodeService {
|
13 |
| - private static final String DATE_FORMATTER= "yyyy/MM/dd HH:mm:ss"; |
| 14 | + private static long currentId = 1; |
14 | 15 |
|
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 | + } |
18 | 26 |
|
19 | 27 | @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; |
25 | 35 | }
|
26 | 36 |
|
27 | 37 | @Override
|
28 |
| - public void updateSnippet(NewCode newCode) { |
29 |
| - code = new Code(newCode.getCode(), LocalDateTime.now()); |
| 38 | + public List<Code> getLatestCode() { |
| 39 | + return codeRepository.getLatestCode(); |
30 | 40 | }
|
31 | 41 | }
|
0 commit comments