Search in sources :

Example 1 with BookDTO

use of com.github.lybgeek.swagger.dto.BookDTO in project springboot-learning by lyb-geek.

the class BookController method upadteBook.

@PostMapping(value = "/update")
@ApiOperation(value = "更新书籍", notes = "更新书籍")
public Result<BookDTO> upadteBook(BookDTO bookDTO) {
    Result<BookDTO> result = new Result<>();
    if (bookDTO.getId() == null) {
        result.setStatus(Result.fail);
        result.setMessage("id不能为空");
        return result;
    }
    BookDTO book = bookService.editBook(bookDTO);
    result.setData(book);
    return result;
}
Also used : BookDTO(com.github.lybgeek.swagger.dto.BookDTO) Result(com.github.lybgeek.common.model.Result) PageResult(com.github.lybgeek.common.model.PageResult) BindingResult(org.springframework.validation.BindingResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with BookDTO

use of com.github.lybgeek.swagger.dto.BookDTO in project springboot-learning by lyb-geek.

the class BookServiceImpl method addBook.

@Override
@Transactional
public // @DS("follow")
BookDTO addBook(BookDTO bookDTO) {
    Book book = dozerMapper.map(bookDTO, Book.class);
    boolean isExitBookByName = ObjectUtils.isNotEmpty(getBookByName(bookDTO.getBookName()));
    if (isExitBookByName) {
        throw new BizException("书名已经存在");
    }
    book.setCreateDate(new Date());
    book.setUpdateDate(new Date());
    baseMapper.insert(book);
    bookDTO = dozerMapper.map(book, BookDTO.class);
    return bookDTO;
}
Also used : BookDTO(com.github.lybgeek.swagger.dto.BookDTO) Book(com.github.lybgeek.swagger.model.Book) BizException(com.github.lybgeek.common.exception.BizException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BookDTO

use of com.github.lybgeek.swagger.dto.BookDTO in project springboot-learning by lyb-geek.

the class BookServiceImpl method pageBook.

@Override
public PageResult<BookDTO> pageBook(PageQuery<BookDTO> pageQuery) {
    BookDTO bookDTO = pageQuery.getQueryParams();
    Wrapper<Book> wrapper = wrapperQueryCondition(bookDTO);
    IPage<Book> page = new Page<>(pageQuery.getPageNo(), pageQuery.getPageSize());
    IPage<Book> bookIPage = baseMapper.selectPage(page, wrapper);
    if (bookIPage != null) {
        List<BookDTO> bookDTOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(bookIPage.getRecords())) {
            bookDTOS = BeanMapperUtils.mapList(bookIPage.getRecords(), BookDTO.class);
        }
        return PageUtil.INSTANCE.getPage(bookIPage, bookDTOS);
    }
    return null;
}
Also used : BookDTO(com.github.lybgeek.swagger.dto.BookDTO) Book(com.github.lybgeek.swagger.model.Book) ArrayList(java.util.ArrayList) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage)

Example 4 with BookDTO

use of com.github.lybgeek.swagger.dto.BookDTO in project springboot-learning by lyb-geek.

the class BookControllerTest method testAddBook.

@Test
public void testAddBook() throws Exception {
    BookDTO params = BookDTO.builder().bookName("测试驱动").author("张三").price(BigDecimal.valueOf(20.1)).stock(10).build();
    log.info("params", params);
    String result = mockMvc.perform(MockMvcRequestBuilders.post("/api/1.0.0/book/add").header(Constant.ACCESS_TOKEN, Constant.ACCESS_TOKEN_PASS_VALUE).param("bookName", "测试驱动").param("author", "战三").param("price", "20.1").param("stock", "10").contentType(MediaType.APPLICATION_FORM_URLENCODED).characterEncoding("utf-8")).andExpect(MockMvcResultMatchers.status().isOk()).andReturn().getResponse().getContentAsString();
    log.info("输出结构化日志", "result", result);
}
Also used : BookDTO(com.github.lybgeek.swagger.dto.BookDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with BookDTO

use of com.github.lybgeek.swagger.dto.BookDTO in project springboot-learning by lyb-geek.

the class BookController method addBook.

@PostMapping(value = "/add")
@ApiOperation(value = "添加书籍", notes = "添加书籍")
public Result<BookDTO> addBook(@Valid BookDTO bookDTO, BindingResult bindingResult) {
    Result<BookDTO> result = new Result<>();
    if (bindingResult.hasErrors()) {
        return ResultUtil.INSTANCE.getFailResult(bindingResult, result);
    }
    try {
        BookDTO book = bookService.addBook(bookDTO);
        result.setData(book);
    } catch (Exception e) {
        log.error("addBook error:" + e.getMessage(), e);
        result.setStatus(Result.fail);
        result.setMessage(e.getMessage());
    }
    return result;
}
Also used : BookDTO(com.github.lybgeek.swagger.dto.BookDTO) Result(com.github.lybgeek.common.model.Result) PageResult(com.github.lybgeek.common.model.PageResult) BindingResult(org.springframework.validation.BindingResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

BookDTO (com.github.lybgeek.swagger.dto.BookDTO)7 PageResult (com.github.lybgeek.common.model.PageResult)3 Result (com.github.lybgeek.common.model.Result)3 Book (com.github.lybgeek.swagger.model.Book)3 BindingResult (org.springframework.validation.BindingResult)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 BizException (com.github.lybgeek.common.exception.BizException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Transactional (org.springframework.transaction.annotation.Transactional)1