feat: 优化界面,增加数据导入
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
package com.lanyuanxiaoyao.bookstore;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.blinkfox.fenix.EnableFenix;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
|
||||
import com.lanyuanxiaoyao.bookstore.service.BookService;
|
||||
import com.lanyuanxiaoyao.bookstore.service.ChapterService;
|
||||
import com.lanyuanxiaoyao.bookstore.service.LineService;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
/**
|
||||
@@ -17,7 +30,43 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
@EnableFenix
|
||||
@EnableConfigurationProperties
|
||||
public class BookStoreApplication {
|
||||
@Resource
|
||||
private BookService bookService;
|
||||
@Resource
|
||||
private ChapterService chapterService;
|
||||
@Resource
|
||||
private LineService lineService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BookStoreApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void importIntoDatabase() throws IOException {
|
||||
var items = new ArrayList<Item>();
|
||||
Files.list(Path.of("out"))
|
||||
.sorted(Comparator.comparing(path -> FileUtil.lastModifiedTime(path.toFile())))
|
||||
.forEach(path -> {
|
||||
try {
|
||||
items.add(new Item(
|
||||
path.getFileName().toString(),
|
||||
Files.readString(path)
|
||||
));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
for (var item : items) {
|
||||
var chapter = new Chapter();
|
||||
chapter.setSequence(chapterService.latestSequence(3608359126886400L) + 1);
|
||||
chapter.setName(null);
|
||||
chapter.setDescription(null);
|
||||
chapter.setBook(bookService.detailOrThrow(3608359126886400L));
|
||||
var chapterId = chapterService.save(chapter);
|
||||
lineService.load(chapterId, item.content());
|
||||
}
|
||||
}
|
||||
|
||||
private record Item(String title, String content) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user