yl-backend/src/main/java/com/guwan/backend/service/impl/BookServiceImpl.java

80 lines
2.3 KiB
Java

package com.guwan.backend.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guwan.backend.entity.Book;
import com.guwan.backend.mapper.BookMapper;
import com.guwan.backend.service.BookService;
import com.mongoplus.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
private final BookMapper bookMapper;
@Override
@Transactional
public Book addBook(Book book) {
// new LambdaQueryWrapper<>()
// new LambdaQueryChainWrapper<>()
bookMapper.insert(book);
return book;
}
@Override
@Transactional
public Book updateBook(Book book) {
bookMapper.updateById(book);
return book;
}
@Override
@Transactional
public void deleteBook(Long id) {
bookMapper.deleteById(id);
}
@Override
public Book getBookById(Long id) {
return bookMapper.selectById(id);
}
@Override
public Book getBookByIsbn(String isbn) {
return bookMapper.selectOne(
new LambdaQueryWrapper<Book>()
.eq(Book::getIsbn, isbn)
);
}
@Override
public IPage<Book> getBookList(Integer pageNum, Integer pageSize, String keyword) {
LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
if (keyword != null && !keyword.isEmpty()) {
wrapper.like(Book::getTitle, keyword)
.or()
.like(Book::getAuthor, keyword)
.or()
.like(Book::getDescription, keyword);
}
return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
}
@Override
public IPage<Book> getBooksByCategory(String category, Integer pageNum, Integer pageSize) {
LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Book::getCategory, category);
return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
}
}