feat(图书初步): 图书初步

图书初步
This commit is contained in:
ovo 2024-12-17 14:46:59 +08:00
parent 8154807f90
commit 1ffa7c4a75
11 changed files with 92 additions and 22 deletions

View File

@ -1,9 +1,11 @@
package com.guwan.backend;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
@MapperScan("com.guwan.backend.mapper")
public class BackendApplication {
@ -12,4 +14,10 @@ public class BackendApplication {
SpringApplication.run(BackendApplication.class, args);
}
@PostConstruct
public void init() {
log.info("大爱仙尊: http://localhost:8084/daxz.html?id=1");
}
}

View File

@ -24,7 +24,7 @@ public class DatabaseInitConfig implements ApplicationRunner {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("db/schema.sql"));
populator.addScript(new ClassPathResource("db/data.sql"));
// populator.addScript(new ClassPathResource("db/daaixianzun.sql"));
//populator.addScript(new ClassPathResource("db/daaixianzun.sql"));
populator.setContinueOnError(true);
populator.execute(dataSource);
log.info("数据库初始化完成");

View File

@ -0,0 +1,34 @@
package com.guwan.backend.config;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author roc
*/
@Configuration
public class LocalDateTimeConfiguration {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
//返回时间数据序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
//接收时间数据反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
};
}
}

View File

@ -12,7 +12,7 @@ public class SecurityConstants {
* 这些路径可以直接访问不需要认证
*/
public static final List<String> WHITE_LIST = List.of(
"/common/**", //公共接口
"/api/common/**", //公共接口
"/demo/**", // 测试接口
"/api/products",
"/api/user/register", // 用户注册

View File

@ -20,7 +20,7 @@ public class BookController {
private final BookService bookService;
@Operation(summary = "添加图书")
@PostMapping
@PostMapping("/add")
public Result<Book> addBook(@RequestBody Book book) {
try {
return Result.success(bookService.addBook(book));
@ -30,6 +30,22 @@ public class BookController {
}
}
@Operation(summary = "分页查询图书列表")
@GetMapping
public Result<IPage<Book>> getBookList(
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String keyword) {
try {
return Result.success(bookService.getBookList(pageNum, pageSize, keyword));
} catch (Exception e) {
log.error("查询图书列表失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "更新图书信息")
@PutMapping("/{id}")
public Result<Book> updateBook(@PathVariable Long id, @RequestBody Book book) {
@ -76,19 +92,7 @@ public class BookController {
}
}
@Operation(summary = "分页查询图书列表")
@GetMapping
public Result<IPage<Book>> getBookList(
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String keyword) {
try {
return Result.success(bookService.getBookList(pageNum, pageSize, keyword));
} catch (Exception e) {
log.error("查询图书列表失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "根据分类获取图书")
@GetMapping("/category/{category}")

View File

@ -23,7 +23,7 @@ import java.util.regex.Pattern;
@Slf4j
@RestController
@RequestMapping("/common")
@RequestMapping("/api/common")
@RequiredArgsConstructor
public class CommonController {
@ -122,6 +122,8 @@ public class CommonController {
ArrayList<BookContent> bookContents = new ArrayList<>();
int sectionId = 1;
// 输出卷和节信息
for (int i = 0; i < volumes.size(); i++) {
// 输出卷的标题
@ -153,6 +155,7 @@ public class CommonController {
bookContent.setVolume(volumes.get(i));
bookContent.setSection(section);
bookContent.setSectionContent(sectionContent);
bookContent.setSectionId(sectionId++);
System.out.println("bookContent = " + bookContent);
@ -174,6 +177,13 @@ public class CommonController {
}
@GetMapping("/getBookContent")
public Result<String> getBookContent(String bookName, Long id) {
BookContent bookContent = bookContentService.getBookContent(bookName, id);
return Result.success(bookContent.getSectionContent());
}
@GetMapping("/getBookCommentByPath")

View File

@ -14,16 +14,14 @@ public class Book {
private String author; // 作者
private String publisher; // 出版社
private String description; // 描述
private String bookUrl; // 图书内容url
private String coverUrl; // 封面图片URL
private String category; // 分类
private String tags; // 标签(逗号分隔)
private Integer totalPages; // 总页数
private String language; // 语言
private LocalDateTime publishDate; // 出版日期
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createdTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updatedTime;
}
}

View File

@ -15,4 +15,6 @@ public class BookContent {
private String volume;
private String section;
private String sectionContent;
private Integer sectionId;
}

View File

@ -5,6 +5,7 @@ import com.guwan.backend.entity.BookContent;
public interface BookContentService extends IService<BookContent> {
BookContent getBookContent(String bookName, Long id);
// 添加书籍
}

View File

@ -1,12 +1,24 @@
package com.guwan.backend.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guwan.backend.entity.BookContent;
import com.guwan.backend.mapper.BookContentMapper;
import com.guwan.backend.service.BookContentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class BookContentServiceImpl extends ServiceImpl<BookContentMapper, BookContent> implements BookContentService {
private final BookContentMapper bookContentMapper;
@Override
public BookContent getBookContent(String bookName, Long id) {
return bookContentMapper.selectOne(new LambdaQueryWrapper<BookContent>()
.eq(BookContent::getBookName, bookName)
.eq(BookContent::getSectionId, id));
}
}

View File

@ -70,6 +70,7 @@ CREATE TABLE IF NOT EXISTS `book_content` (
`volume` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`section` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`section_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
`section_id` int DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2342 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;