Add pagination support and enhance book and publisher management features
This commit is contained in:
parent
0f5defb2d0
commit
276bc8ec62
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,21 @@
|
||||
package com.grtsinry43.bookmanagement.common;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PageResponse<T> {
|
||||
private Integer pageNum;
|
||||
private Integer pageSize;
|
||||
private Long total;
|
||||
private T data;
|
||||
|
||||
public PageResponse(Integer pageNum, Integer pageSize, Long total, T data) {
|
||||
this.pageNum = pageNum;
|
||||
this.pageSize = pageSize;
|
||||
this.total = total;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public PageResponse() {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.grtsinry43.bookmanagement.config;
|
||||
|
||||
import com.grtsinry43.bookmanagement.aop.AuthInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author grtsinry43
|
||||
* @date 2024/9/8 14:48
|
||||
* @description 少年负壮气,奋烈自有时!
|
||||
*/
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private AuthInterceptor authInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(authInterceptor).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
@ -5,8 +5,11 @@ import com.grtsinry43.bookmanagement.common.ApiResponse;
|
||||
import com.grtsinry43.bookmanagement.common.BusinessException;
|
||||
import com.grtsinry43.bookmanagement.common.ErrorCode;
|
||||
import com.grtsinry43.bookmanagement.common.UserRole;
|
||||
import com.grtsinry43.bookmanagement.dto.BookDTO;
|
||||
import com.grtsinry43.bookmanagement.entity.Book;
|
||||
import com.grtsinry43.bookmanagement.service.BookService;
|
||||
import com.grtsinry43.bookmanagement.vo.BookVO;
|
||||
import com.grtsinry43.bookmanagement.common.PageResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -28,14 +31,15 @@ public class BookController {
|
||||
private BookService bookService;
|
||||
|
||||
@GetMapping("/all")
|
||||
public ApiResponse<List<Book>> getAllBooks() {
|
||||
List<Book> books = bookService.getAllBooks();
|
||||
return ApiResponse.success(books);
|
||||
public ApiResponse<PageResponse<List<BookVO>>> getAllBooks(@RequestParam(defaultValue = "1") int pageNum,
|
||||
@RequestParam(defaultValue = "10") int pageSize) {
|
||||
PageResponse<List<BookVO>> page = bookService.getAllBooks(pageNum, pageSize);
|
||||
return ApiResponse.success(page);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ApiResponse<Book> getBookById(@PathVariable Integer id) {
|
||||
Book book = bookService.getBookById(id);
|
||||
public ApiResponse<BookVO> getBookById(@PathVariable Integer id) {
|
||||
BookVO book = bookService.getBookById(id);
|
||||
if (book != null) {
|
||||
return ApiResponse.success(book);
|
||||
} else {
|
||||
@ -45,48 +49,51 @@ public class BookController {
|
||||
|
||||
@GetMapping("/search/title")
|
||||
@AuthCheck(requiredRole = UserRole.USER)
|
||||
public ApiResponse<List<Book>> searchBooksByTitle(@RequestParam String title) {
|
||||
List<Book> books = bookService.getBooksByTitle(title);
|
||||
return ApiResponse.success(books);
|
||||
public ApiResponse<PageResponse<List<BookVO>>> searchBooksByTitle(@RequestParam String title,
|
||||
@RequestParam(defaultValue = "1") int pageNum,
|
||||
@RequestParam(defaultValue = "10") int pageSize) {
|
||||
PageResponse<List<BookVO>> page = bookService.getBooksByTitle(title, pageNum, pageSize);
|
||||
return ApiResponse.success(page);
|
||||
}
|
||||
|
||||
@GetMapping("/search/author")
|
||||
@AuthCheck(requiredRole = UserRole.USER)
|
||||
public ApiResponse<List<Book>> searchBooksByAuthor(@RequestParam String authorName) {
|
||||
List<Book> books = bookService.getBooksByAuthor(authorName);
|
||||
return ApiResponse.success(books);
|
||||
public ApiResponse<PageResponse<List<BookVO>>> searchBooksByAuthor(@RequestParam String authorName,
|
||||
@RequestParam(defaultValue = "1") int pageNum,
|
||||
@RequestParam(defaultValue = "10") int pageSize) {
|
||||
PageResponse<List<BookVO>> page = bookService.getBooksByAuthor(authorName, pageNum, pageSize);
|
||||
return ApiResponse.success(page);
|
||||
}
|
||||
|
||||
@GetMapping("/search/publisher")
|
||||
@AuthCheck(requiredRole = UserRole.USER)
|
||||
public ApiResponse<List<Book>> searchBooksByPublisher(@RequestParam String publisherName) {
|
||||
List<Book> books = bookService.getBooksByPublisher(publisherName);
|
||||
return ApiResponse.success(books);
|
||||
public ApiResponse<PageResponse<List<BookVO>>> searchBooksByPublisher(@RequestParam String publisherName,
|
||||
@RequestParam(defaultValue = "1") int pageNum,
|
||||
@RequestParam(defaultValue = "10") int pageSize) {
|
||||
PageResponse<List<BookVO>> page = bookService.getBooksByPublisher(publisherName, pageNum, pageSize);
|
||||
return ApiResponse.success(page);
|
||||
}
|
||||
|
||||
@PostMapping("/admin/add")
|
||||
@AuthCheck(requiredRole = UserRole.ADMIN)
|
||||
public ApiResponse<Book> addBook(@RequestBody Book book) {
|
||||
bookService.addBook(book);
|
||||
return ApiResponse.success(book);
|
||||
public ApiResponse<BookVO> addBook(@RequestBody BookDTO book) {
|
||||
BookVO res = bookService.addBook(book);
|
||||
return ApiResponse.success(res);
|
||||
|
||||
}
|
||||
|
||||
@PutMapping("/admin/update")
|
||||
@AuthCheck(requiredRole = UserRole.ADMIN)
|
||||
public ApiResponse<Book> updateBook(@RequestBody Book book) {
|
||||
|
||||
bookService.updateBook(book);
|
||||
return ApiResponse.success(book);
|
||||
public ApiResponse<BookVO> updateBook(@RequestBody BookDTO book) {
|
||||
BookVO res = bookService.updateBook(book);
|
||||
return ApiResponse.success(res);
|
||||
|
||||
}
|
||||
|
||||
@DeleteMapping("/admin/delete/{id}")
|
||||
@AuthCheck(requiredRole = UserRole.ADMIN)
|
||||
public ApiResponse<Void> deleteBook(@PathVariable Integer id) {
|
||||
|
||||
bookService.deleteBook(id);
|
||||
return ApiResponse.success(null);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,12 @@
|
||||
package com.grtsinry43.bookmanagement.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import com.grtsinry43.bookmanagement.dto.PublisherDTO;
|
||||
import com.grtsinry43.bookmanagement.entity.Publisher;
|
||||
import com.grtsinry43.bookmanagement.service.PublisherService;
|
||||
import com.grtsinry43.bookmanagement.common.ApiResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -14,5 +19,37 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
@RequestMapping("/publisher")
|
||||
public class PublisherController {
|
||||
@Autowired
|
||||
private PublisherService publisherService;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ApiResponse<Publisher> getById(@PathVariable Integer id) {
|
||||
return ApiResponse.success(publisherService.getById(id));
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public ApiResponse<List<Publisher>> getAll() {
|
||||
return ApiResponse.success(publisherService.getAll());
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ApiResponse<Publisher> add(@RequestBody PublisherDTO dto) {
|
||||
return ApiResponse.success(publisherService.add(dto));
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public ApiResponse<Publisher> update(@PathVariable Integer id, @RequestBody PublisherDTO dto) {
|
||||
return ApiResponse.success(publisherService.update(id, dto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ApiResponse<Void> delete(@PathVariable Integer id) {
|
||||
publisherService.delete(id);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public ApiResponse<List<Publisher>> searchByName(@RequestParam String name) {
|
||||
return ApiResponse.success(publisherService.findByNameLike(name));
|
||||
}
|
||||
}
|
||||
|
||||
42
src/main/java/com/grtsinry43/bookmanagement/dto/BookDTO.java
Normal file
42
src/main/java/com/grtsinry43/bookmanagement/dto/BookDTO.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.grtsinry43.bookmanagement.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Positive;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class BookDTO {
|
||||
@NotBlank(message = "图书标题不能为空")
|
||||
@Size(max = 200, message = "图书标题长度不能超过200个字符")
|
||||
private String title;
|
||||
|
||||
@NotBlank(message = "ISBN不能为空")
|
||||
@Size(max = 20, message = "ISBN长度不能超过20个字符")
|
||||
private String isbn;
|
||||
|
||||
@NotNull(message = "价格不能为空")
|
||||
@Positive(message = "价格必须大于0")
|
||||
private BigDecimal price;
|
||||
|
||||
@NotNull(message = "库存不能为空")
|
||||
@Positive(message = "库存必须大于0")
|
||||
private Integer stock;
|
||||
|
||||
@NotNull(message = "出版日期不能为空")
|
||||
private LocalDate publishDate;
|
||||
|
||||
@NotNull(message = "出版社ID不能为空")
|
||||
private Integer publisherId;
|
||||
|
||||
private String description;
|
||||
|
||||
private String coverImage;
|
||||
|
||||
private List<String> authors; // 作者列表
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.grtsinry43.bookmanagement.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PublisherDTO {
|
||||
@NotBlank(message = "出版社名称不能为空")
|
||||
@Size(max = 100, message = "出版社名称长度不能超过100个字符")
|
||||
private String name;
|
||||
|
||||
@Size(max = 200, message = "地址长度不能超过200个字符")
|
||||
private String address;
|
||||
}
|
||||
@ -42,4 +42,10 @@ public class Book implements Serializable {
|
||||
private LocalDate publishDate;
|
||||
|
||||
private Integer publisherId;
|
||||
|
||||
private String description;
|
||||
|
||||
private String coverImage;
|
||||
|
||||
private String author;
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.grtsinry43.bookmanagement.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.grtsinry43.bookmanagement.entity.BookAuthor;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -14,5 +15,6 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface BookAuthorMapper extends BaseMapper<BookAuthor> {
|
||||
|
||||
int insertBookAuthor(BookAuthor bookAuthor);
|
||||
int deleteByBookId(@Param("bookId") Integer bookId);
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package com.grtsinry43.bookmanagement.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.grtsinry43.bookmanagement.entity.Publisher;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -15,4 +17,11 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
@Mapper
|
||||
public interface PublisherMapper extends BaseMapper<Publisher> {
|
||||
|
||||
Publisher findById(@Param("publisherId") Integer publisherId);
|
||||
List<Publisher> findAll();
|
||||
int insert(Publisher publisher);
|
||||
int update(Publisher publisher);
|
||||
int delete(@Param("publisherId") Integer publisherId);
|
||||
List<Publisher> findByNameLike(@Param("name") String name);
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
package com.grtsinry43.bookmanagement.service;
|
||||
|
||||
import com.grtsinry43.bookmanagement.dto.BookDTO;
|
||||
import com.grtsinry43.bookmanagement.entity.Book;
|
||||
import com.grtsinry43.bookmanagement.vo.BookVO;
|
||||
import com.grtsinry43.bookmanagement.common.PageResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -12,19 +16,17 @@ import java.util.List;
|
||||
* @since 2025-05-19
|
||||
*/
|
||||
public interface BookService {
|
||||
Book getBookById(Integer bookId);
|
||||
BookVO getBookById(Integer bookId);
|
||||
|
||||
List<Book> getAllBooks();
|
||||
PageResponse<List<BookVO>> getBooksByTitle(String title, int pageNum, int pageSize);
|
||||
PageResponse<List<BookVO>> getBooksByAuthor(String authorName, int pageNum, int pageSize);
|
||||
PageResponse<List<BookVO>> getBooksByPublisher(String publisherName, int pageNum, int pageSize);
|
||||
|
||||
List<Book> getBooksByTitle(String title);
|
||||
PageResponse<List<BookVO>> getAllBooks(int pageNum, int pageSize);
|
||||
|
||||
List<Book> getBooksByAuthor(String authorName);
|
||||
BookVO addBook(BookDTO book);
|
||||
|
||||
List<Book> getBooksByPublisher(String publisherName);
|
||||
|
||||
void addBook(Book book);
|
||||
|
||||
void updateBook(Book book);
|
||||
BookVO updateBook(BookDTO book);
|
||||
|
||||
void deleteBook(Integer bookId);
|
||||
}
|
||||
|
||||
@ -2,6 +2,9 @@ package com.grtsinry43.bookmanagement.service;
|
||||
|
||||
import com.grtsinry43.bookmanagement.entity.Publisher;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.grtsinry43.bookmanagement.dto.PublisherDTO;
|
||||
import com.grtsinry43.bookmanagement.common.PageResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -12,5 +15,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
* @since 2025-05-19
|
||||
*/
|
||||
public interface PublisherService extends IService<Publisher> {
|
||||
|
||||
Publisher getById(Integer publisherId);
|
||||
List<Publisher> getAll();
|
||||
PageResponse<List<Publisher>> getAll(int pageNum, int pageSize);
|
||||
Publisher add(PublisherDTO publisherDTO);
|
||||
Publisher update(Integer publisherId, PublisherDTO publisherDTO);
|
||||
void delete(Integer publisherId);
|
||||
List<Publisher> findByNameLike(String name);
|
||||
}
|
||||
|
||||
@ -1,13 +1,25 @@
|
||||
package com.grtsinry43.bookmanagement.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.grtsinry43.bookmanagement.common.BusinessException;
|
||||
import com.grtsinry43.bookmanagement.common.ErrorCode;
|
||||
import com.grtsinry43.bookmanagement.dto.BookDTO;
|
||||
import com.grtsinry43.bookmanagement.entity.Book;
|
||||
import com.grtsinry43.bookmanagement.entity.Author;
|
||||
import com.grtsinry43.bookmanagement.entity.BookAuthor;
|
||||
import com.grtsinry43.bookmanagement.mapper.BookMapper;
|
||||
import com.grtsinry43.bookmanagement.mapper.AuthorMapper;
|
||||
import com.grtsinry43.bookmanagement.mapper.BookAuthorMapper;
|
||||
import com.grtsinry43.bookmanagement.service.BookService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.grtsinry43.bookmanagement.vo.BookVO;
|
||||
import com.grtsinry43.bookmanagement.common.PageResponse;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -22,44 +34,136 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
|
||||
|
||||
@Autowired
|
||||
private BookMapper bookMapper;
|
||||
@Autowired
|
||||
private BookAuthorServiceImpl bookAuthorService;
|
||||
@Autowired
|
||||
private PublisherServiceImpl publisherService;
|
||||
@Autowired
|
||||
private AuthorMapper authorMapper;
|
||||
@Autowired
|
||||
private BookAuthorMapper bookAuthorMapper;
|
||||
|
||||
@Override
|
||||
public Book getBookById(Integer bookId) {
|
||||
return bookMapper.findById(bookId);
|
||||
public BookVO getBookById(Integer bookId) {
|
||||
Book book = bookMapper.findById(bookId);
|
||||
if (book == null) return null;
|
||||
return buildBookVO(book);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getAllBooks() {
|
||||
return bookMapper.findAll();
|
||||
public PageResponse<List<BookVO>> getBooksByTitle(String title, int pageNum, int pageSize) {
|
||||
List<Book> books = bookMapper.findByTitle(title);
|
||||
return buildPageResponse(books, pageNum, pageSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getBooksByTitle(String title) {
|
||||
return bookMapper.findByTitle(title);
|
||||
public PageResponse<List<BookVO>> getBooksByAuthor(String authorName, int pageNum, int pageSize) {
|
||||
List<Book> books = bookMapper.findByAuthor(authorName);
|
||||
return buildPageResponse(books, pageNum, pageSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getBooksByAuthor(String authorName) {
|
||||
return bookMapper.findByAuthor(authorName);
|
||||
public PageResponse<List<BookVO>> getBooksByPublisher(String publisherName, int pageNum, int pageSize) {
|
||||
List<Book> books = bookMapper.findByPublisher(publisherName);
|
||||
return buildPageResponse(books, pageNum, pageSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> getBooksByPublisher(String publisherName) {
|
||||
return bookMapper.findByPublisher(publisherName);
|
||||
public PageResponse<List<BookVO>> getAllBooks(int pageNum, int pageSize) {
|
||||
List<Book> books = bookMapper.findAll();
|
||||
return buildPageResponse(books, pageNum, pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找或新建作者并返回 authorId
|
||||
*/
|
||||
private Integer getOrCreateAuthorId(String authorName) {
|
||||
QueryWrapper<Author> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("name", authorName);
|
||||
Author author = authorMapper.selectOne(queryWrapper);
|
||||
if (author == null) {
|
||||
author = new Author();
|
||||
author.setName(authorName);
|
||||
authorMapper.insert(author);
|
||||
}
|
||||
return author.getAuthorId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置书籍 - 作者关系(先删后建)
|
||||
*/
|
||||
private void resetBookAuthors(Integer bookId, List<String> authorList) {
|
||||
bookAuthorMapper.deleteByBookId(bookId);
|
||||
if (authorList != null) {
|
||||
for (String authorName : authorList) {
|
||||
Integer authorId = getOrCreateAuthorId(authorName);
|
||||
BookAuthor bookAuthor = new BookAuthor();
|
||||
bookAuthor.setBookId(bookId);
|
||||
bookAuthor.setAuthorId(authorId);
|
||||
bookAuthorMapper.insertBookAuthor(bookAuthor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private BookVO buildBookVO(Book book) {
|
||||
BookVO vo = new BookVO();
|
||||
BeanUtils.copyProperties(book, vo);
|
||||
// 查询出版社名
|
||||
if (book.getPublisherId() != null) {
|
||||
var publisher = publisherService.getById(book.getPublisherId());
|
||||
if (publisher != null) {
|
||||
vo.setPublisherName(publisher.getName());
|
||||
}
|
||||
}
|
||||
// 查询作者列表
|
||||
List<String> authorNames = List.of(book.getAuthor().split(","));
|
||||
vo.setAuthor(authorNames);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private PageResponse<List<BookVO>> buildPageResponse(List<Book> books, int pageNum, int pageSize) {
|
||||
int total = books.size();
|
||||
int fromIndex = Math.max(0, (pageNum - 1) * pageSize);
|
||||
int toIndex = Math.min(fromIndex + pageSize, total);
|
||||
List<BookVO> pageList = books.stream().skip(fromIndex).limit(pageSize).map(this::buildBookVO).collect(Collectors.toList());
|
||||
return new PageResponse<>(pageNum, pageSize, (long) total, pageList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBook(Book book) {
|
||||
bookMapper.insert(book);
|
||||
public BookVO addBook(BookDTO book) {
|
||||
Book book1 = new Book();
|
||||
Integer publisherId = book.getPublisherId();
|
||||
if (publisherService.getById(publisherId) == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
} else {
|
||||
book1.setPublisherId(publisherId);
|
||||
}
|
||||
BeanUtils.copyProperties(book, book1);
|
||||
bookMapper.insert(book1);
|
||||
resetBookAuthors(book1.getBookId(), book.getAuthors());
|
||||
return buildBookVO(book1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBook(Book book) {
|
||||
bookMapper.update(book);
|
||||
public BookVO updateBook(BookDTO book) {
|
||||
Book bookEntity = new Book();
|
||||
Integer publisherId = book.getPublisherId();
|
||||
if (publisherService.getById(publisherId) == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
} else {
|
||||
bookEntity.setPublisherId(publisherId);
|
||||
}
|
||||
BeanUtils.copyProperties(book, bookEntity);
|
||||
bookMapper.update(bookEntity);
|
||||
resetBookAuthors(bookEntity.getBookId(), book.getAuthors());
|
||||
// 查询最新的 book
|
||||
Book updated = bookMapper.findById(bookEntity.getBookId());
|
||||
return buildBookVO(updated);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBook(Integer bookId) {
|
||||
bookAuthorMapper.deleteByBookId(bookId);
|
||||
bookMapper.delete(bookId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,11 @@ import com.grtsinry43.bookmanagement.mapper.PublisherMapper;
|
||||
import com.grtsinry43.bookmanagement.service.PublisherService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import java.util.List;
|
||||
import com.grtsinry43.bookmanagement.dto.PublisherDTO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import com.grtsinry43.bookmanagement.common.PageResponse;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -17,4 +22,54 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class PublisherServiceImpl extends ServiceImpl<PublisherMapper, Publisher> implements PublisherService {
|
||||
|
||||
@Autowired
|
||||
private PublisherMapper publisherMapper;
|
||||
|
||||
@Override
|
||||
public Publisher getById(Integer publisherId) {
|
||||
return publisherMapper.findById(publisherId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Publisher> getAll() {
|
||||
return publisherMapper.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResponse<List<Publisher>> getAll(int pageNum, int pageSize) {
|
||||
List<Publisher> all = publisherMapper.findAll();
|
||||
int total = all.size();
|
||||
int fromIndex = Math.max(0, (pageNum - 1) * pageSize);
|
||||
int toIndex = Math.min(fromIndex + pageSize, total);
|
||||
List<Publisher> pageList = all.subList(fromIndex, toIndex);
|
||||
return new PageResponse<>(pageNum, pageSize, (long) total, pageList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher add(PublisherDTO publisherDTO) {
|
||||
Publisher publisher = new Publisher();
|
||||
BeanUtils.copyProperties(publisherDTO, publisher);
|
||||
publisherMapper.insert(publisher);
|
||||
return publisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher update(Integer publisherId, PublisherDTO publisherDTO) {
|
||||
Publisher publisher = publisherMapper.findById(publisherId);
|
||||
if (publisher == null) return null;
|
||||
BeanUtils.copyProperties(publisherDTO, publisher);
|
||||
publisher.setPublisherId(publisherId);
|
||||
publisherMapper.update(publisher);
|
||||
return publisherMapper.findById(publisherId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Integer publisherId) {
|
||||
publisherMapper.delete(publisherId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Publisher> findByNameLike(String name) {
|
||||
return publisherMapper.findByNameLike(name);
|
||||
}
|
||||
}
|
||||
|
||||
35
src/main/java/com/grtsinry43/bookmanagement/vo/BookVO.java
Normal file
35
src/main/java/com/grtsinry43/bookmanagement/vo/BookVO.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.grtsinry43.bookmanagement.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author grtsinry43
|
||||
* @since 2025-05-19
|
||||
*/
|
||||
@Data
|
||||
public class BookVO implements Serializable {
|
||||
private Integer bookId;
|
||||
|
||||
private String title;
|
||||
|
||||
private String isbn;
|
||||
|
||||
private BigDecimal price;
|
||||
|
||||
private Integer stock;
|
||||
|
||||
private LocalDate publishDate;
|
||||
|
||||
private String publisherName;
|
||||
|
||||
private String description;
|
||||
|
||||
private String coverImage;
|
||||
|
||||
private List<String> author;
|
||||
}
|
||||
@ -2,4 +2,13 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.grtsinry43.bookmanagement.mapper.BookAuthorMapper">
|
||||
|
||||
<insert id="insertBookAuthor" parameterType="com.grtsinry43.bookmanagement.entity.BookAuthor">
|
||||
INSERT INTO book_author (book_id, author_id)
|
||||
VALUES (#{bookId}, #{authorId})
|
||||
</insert>
|
||||
|
||||
<delete id="deleteByBookId" parameterType="int">
|
||||
DELETE FROM book_author WHERE book_id = #{bookId}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
||||
@ -2,4 +2,32 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.grtsinry43.bookmanagement.mapper.PublisherMapper">
|
||||
|
||||
<select id="findById" resultType="com.grtsinry43.bookmanagement.entity.Publisher">
|
||||
SELECT * FROM publisher WHERE publisher_id = #{publisherId}
|
||||
</select>
|
||||
|
||||
<select id="findAll" resultType="com.grtsinry43.bookmanagement.entity.Publisher">
|
||||
SELECT * FROM publisher
|
||||
</select>
|
||||
|
||||
<select id="findByNameLike" resultType="com.grtsinry43.bookmanagement.entity.Publisher">
|
||||
SELECT * FROM publisher WHERE name LIKE CONCAT('%', #{name}, '%')
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="com.grtsinry43.bookmanagement.entity.Publisher" useGeneratedKeys="true" keyProperty="publisherId">
|
||||
INSERT INTO publisher (name, address)
|
||||
VALUES (#{name}, #{address})
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="com.grtsinry43.bookmanagement.entity.Publisher">
|
||||
UPDATE publisher
|
||||
SET name = #{name},
|
||||
address = #{address}
|
||||
WHERE publisher_id = #{publisherId}
|
||||
</update>
|
||||
|
||||
<delete id="delete">
|
||||
DELETE FROM publisher WHERE publisher_id = #{publisherId}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
-- 适用于 PostgreSQL 数据库
|
||||
|
||||
-- 创建数据库(如需)
|
||||
-- CREATE DATABASE book_management;
|
||||
|
||||
@ -25,9 +27,12 @@ CREATE TABLE book
|
||||
(
|
||||
book_id SERIAL PRIMARY KEY,
|
||||
title VARCHAR(200) NOT NULL,
|
||||
isbn VARCHAR(20) NOT NULL UNIQUE,
|
||||
isbn VARCHAR(20) NOT NULL,
|
||||
price NUMERIC(10, 2) NOT NULL CHECK (price >= 0),
|
||||
stock INT NOT NULL CHECK (stock >= 0),
|
||||
description TEXT,
|
||||
cover_image VARCHAR(200),
|
||||
author VARCHAR(100) NOT NULL,
|
||||
publish_date DATE,
|
||||
publisher_id INT NOT NULL REFERENCES publisher (publisher_id)
|
||||
);
|
||||
@ -37,7 +42,8 @@ CREATE TABLE book_author
|
||||
(
|
||||
book_author_id SERIAL PRIMARY KEY,
|
||||
book_id INT NOT NULL REFERENCES book (book_id) ON DELETE CASCADE,
|
||||
author_id INT NOT NULL REFERENCES author (author_id) ON DELETE CASCADE
|
||||
author_id INT NOT NULL REFERENCES author (author_id) ON DELETE CASCADE,
|
||||
UNIQUE (book_id, author_id)
|
||||
);
|
||||
|
||||
-- 读者表
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user