feat: 章节增加修改功能
This commit is contained in:
@@ -1,23 +1,10 @@
|
||||
package com.lanyuanxiaoyao.bookstore;
|
||||
|
||||
import cn.hutool.core.lang.Tuple;
|
||||
import cn.hutool.core.text.csv.CsvUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.blinkfox.fenix.EnableFenix;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Line;
|
||||
import com.lanyuanxiaoyao.bookstore.service.BookService;
|
||||
import com.lanyuanxiaoyao.bookstore.service.ChapterService;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
@@ -33,44 +20,4 @@ public class BookStoreApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BookStoreApplication.class, args);
|
||||
}
|
||||
|
||||
@Resource
|
||||
private BookService bookService;
|
||||
@Resource
|
||||
private ChapterService chapterService;
|
||||
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
// @EventListener(ApplicationReadyEvent.class)
|
||||
public void loadOldData() throws FileNotFoundException {
|
||||
var reader = CsvUtil.getReader(new FileReader("C:\\Users\\lanyuanxiaoyao\\Result_6.csv"));
|
||||
var rows = reader.stream()
|
||||
.map(row -> new Row(Long.parseLong(row.get(0)), row.get(1), Long.parseLong(row.get(2)), row.get(3)))
|
||||
.toList();
|
||||
var book = bookService.detailOrThrow(3602572744994816L);
|
||||
rows.stream()
|
||||
.map(row -> new Tuple(row.chapterSequence(), row.chapterTitle()))
|
||||
.distinct()
|
||||
.forEach(tuple -> {
|
||||
var chapter = new Chapter();
|
||||
chapter.setSequence(NumberUtil.toDouble(tuple.get(0)));
|
||||
chapter.setName(tuple.get(1));
|
||||
chapter.setBook(book);
|
||||
|
||||
var lines = rows.stream()
|
||||
.filter(row -> ObjectUtil.equals(row.chapterSequence(), tuple.get(0)) && ObjectUtil.equals(row.chapterTitle(), tuple.get(1)))
|
||||
.map(row -> {
|
||||
var line = new Line();
|
||||
line.setSequence(NumberUtil.toDouble(row.lineSequence()));
|
||||
line.setText(row.lineText());
|
||||
line.setChapter(chapter);
|
||||
return line;
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
chapter.setContent(lines);
|
||||
chapterService.save(chapter);
|
||||
});
|
||||
}
|
||||
|
||||
public record Row(long chapterSequence, String chapterTitle, long lineSequence, String lineText) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.lanyuanxiaoyao.bookstore.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Line;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.vo.Option;
|
||||
import com.lanyuanxiaoyao.bookstore.service.BookService;
|
||||
import com.lanyuanxiaoyao.bookstore.service.ChapterService;
|
||||
@@ -8,11 +10,15 @@ import com.lanyuanxiaoyao.bookstore.service.LineService;
|
||||
import com.lanyuanxiaoyao.service.template.controller.GlobalResponse;
|
||||
import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -43,7 +49,7 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
|
||||
public GlobalResponse<Object> saveWithContent(@RequestBody SaveWithContentItem item) {
|
||||
if (SaveWithContentItem.Mode.CREATE.equals(item.mode())) {
|
||||
var chapter = new Chapter();
|
||||
chapter.setSequence(chapterService.latestSequence(item.bookId()));
|
||||
chapter.setSequence(chapterService.latestSequence(item.bookId()) + 1);
|
||||
chapter.setName(item.name());
|
||||
chapter.setDescription(item.description());
|
||||
chapter.setBook(bookService.detailOrThrow(item.bookId()));
|
||||
@@ -62,6 +68,18 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
|
||||
return GlobalResponse.responseSuccess();
|
||||
}
|
||||
|
||||
@GetMapping("content/{chapter_id}")
|
||||
public GlobalResponse<Map<String, Object>> content(@PathVariable("chapter_id") Long chapterId) {
|
||||
var chapter = chapterService.detailOrThrow(chapterId);
|
||||
return GlobalResponse.responseDetailData(
|
||||
chapter.getContent()
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(Line::getSequence))
|
||||
.map(Line::getText)
|
||||
.collect(Collectors.joining("\n"))
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("generate_sequence")
|
||||
public GlobalResponse<Object> generateSequence() {
|
||||
chapterService.generateSequence();
|
||||
@@ -73,7 +91,7 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
|
||||
return item -> {
|
||||
var chapter = new Chapter();
|
||||
chapter.setId(item.id());
|
||||
chapter.setSequence(chapterService.latestSequence(item.bookId()));
|
||||
chapter.setSequence(ObjectUtil.defaultIfNull(item.sequence(), chapterService.latestSequence(item.bookId()) + 1));
|
||||
chapter.setName(item.name());
|
||||
chapter.setDescription(item.description());
|
||||
chapter.setBook(bookService.detailOrThrow(item.bookId()));
|
||||
@@ -105,6 +123,7 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
|
||||
public record SaveItem(
|
||||
Long id,
|
||||
Long bookId,
|
||||
Double sequence,
|
||||
String name,
|
||||
String description
|
||||
) {
|
||||
@@ -118,10 +137,6 @@ public class ChapterController extends SimpleControllerSupport<Chapter, ChapterC
|
||||
Mode mode,
|
||||
String content
|
||||
) {
|
||||
public SaveItem toSaveItem() {
|
||||
return new SaveItem(null, bookId, name, description);
|
||||
}
|
||||
|
||||
public enum Mode {
|
||||
CREATE, OVERRIDE
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class Line extends SimpleEntity {
|
||||
@Lob
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
@ToString.Exclude
|
||||
@Column(nullable = false)
|
||||
@Column(nullable = false, columnDefinition = "longtext")
|
||||
private String text;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
|
||||
|
||||
@@ -2,12 +2,12 @@ package com.lanyuanxiaoyao.bookstore.service;
|
||||
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.Chapter;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.QChapter;
|
||||
import com.lanyuanxiaoyao.bookstore.entity.vo.Option;
|
||||
import com.lanyuanxiaoyao.bookstore.repository.ChapterRepository;
|
||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@@ -32,7 +32,7 @@ public class ChapterService extends SimpleServiceSupport<Chapter> {
|
||||
}
|
||||
|
||||
public void generateSequence() {
|
||||
var chapters = chapterRepository.findAll(Sort.by(Sort.Direction.ASC, Chapter.Fields.sequence));
|
||||
var chapters = chapterRepository.findAll(QChapter.chapter.sequence.asc());
|
||||
for (int index = 0; index < chapters.size(); index++) {
|
||||
chapters.get(index).setSequence(NumberUtil.toDouble(index));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user