package com.nagarro.library.book.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.nagarro.library.book.mapper.BookMapper;
import com.nagarro.library.book.model.Book;
import com.nagarro.library.book.model.dto.BookDTO;
import com.nagarro.library.book.model.dto.PageQueryDTO;
import com.nagarro.library.book.model.vo.BookVO;
import com.nagarro.library.book.service.BookService;
import com.nagarro.library.common.PageListResponse;
import com.nagarro.library.enums.BookEnum;
import com.nagarro.library.enums.ErrorCodeEnum;
import com.nagarro.library.exception.BusinessException;
import com.nagarro.library.user.helper.JwtTokenHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

 @Autowired
 private BookMapper bookMapper;

 @Autowired
 private JwtTokenHelper jwtTokenHelper;

 @Override
 public BookDTO addBook(BookDTO bookDTO, String token) {
 //step1: validate books author, title, publisher, publishedDate, genre
 //validateBook(bookDTO);
 //step2: judge if book exists
 boolean exist = bookMapper.bookExist(bookDTO.getAuthor(), bookDTO.getTitle(), bookDTO.getPublisher());
 if (exist) {
 throw new BusinessException(ErrorCodeEnum.BOOK\_EXIST);
 }
 //step3: convert bookDTO to book
 Book book = new Book();
 BeanUtils.copyProperties(bookDTO, book);
 book.setStatus(BookEnum.AVAILABLE.getBookCode());
 // jwtTokenHelper.getUserIdFromToken(token);
 // Long userId = jwtTokenHelper.getUserIdFromToken(token);
 book.setCreatedBy(jwtTokenHelper.getUserIdFromToken(token));
 //step4: save book
 bookMapper.insert(book);
 //step5: convert book to bookDTO
 BookDTO result = new BookDTO();
 BeanUtils.copyProperties(book, result);
 result.setStatus(BookEnum.getBookStatus(book.getStatus()));
 return result;
 }

 @Override
 public BookDTO updateBook(BookDTO book, String token) {
 //step1: validate books author, title, publisher, publishedDate, genre
 //validateBook(bookDTO);
 //step2: get book by id
 Book bookDB = bookMapper.selectById(book.getId());
 //step3: judge book author, title, publisher is same as bookDB
 if (!StringUtils.equals(bookDB.getAuthor(), book.getAuthor()) || !StringUtils.equals(bookDB.getTitle(), book.getTitle()) || !StringUtils.equals(bookDB.getPublisher(), book.getPublisher())) {
 //step4: judge if book exists
 boolean exist = bookMapper.bookExist(book.getAuthor(), book.getTitle(), book.getPublisher());
 if (exist) {
 throw new BusinessException(ErrorCodeEnum.BOOK\_EXIST);
 }
 }
 //step5: convert bookDTO to book
 Book bookUpdate = new Book();
 BeanUtils.copyProperties(book, bookUpdate);
 bookUpdate.setUpdatedBy(jwtTokenHelper.getUserIdFromToken(token));
 //step6: update book
 bookMapper.updateById(bookUpdate);
 return book;
 }

 @Override
 public PageListResponse bookList(PageQueryDTO pageQuery, String token) {
 //step1: validate orderType only asc or desc, orderField only author, title, publisher, publishedDate, genre
 //step2: book status
 if (!StringUtil.isEmpty(pageQuery.getStatus())) {
 pageQuery.setStatus(String.valueOf(BookEnum.getBookCode(pageQuery.getStatus())));
 }
 //step3: get pageListResponse use Mybatis PageHelper
 Page bookListDTOPage = PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
 bookMapper.bookList(pageQuery);
 List result = bookListDTOPage.getResult();
 //step4: convert book to bookDTO
 List bookDTOList = new ArrayList<>();
 for (Book book : result) {
 BookDTO bookDTO = new BookDTO();
 BeanUtils.copyProperties(book, bookDTO);
 bookDTO.setStatus(BookEnum.getBookStatus(book.getStatus()));
 //step5: judge book availableDate is null or before now
 if (book.getAvailableDate() == null || book.getAvailableDate().before(new Date())) {
 bookDTO.setAvailableDate(new Date());
 }
 bookDTOList.add(bookDTO);
 }
 //step6: convert bookDTOList to bookVO
 BookVO bookVO = new BookVO();
 BeanUtils.copyProperties(bookListDTOPage, bookVO);
 bookVO.setData(bookDTOList);
 return bookVO;

 }

}
패키지 com.nagarro.library.book.service.impl;import com.alibaba.druid.util.StringUtils;import com.github.pagehelper.Page;import com.github.pagehelper.PageHelper;import com.github.pagehelper.util.StringUtil;import com.nagarro.library.book.mapper.BookMapper;import com.nagarro.library.book.model.Book;import com.nagarro.library.book.model.dto.BookDTO;com.nagarro.library.book.model.dto.PageQueryDTO를 가져옵니다;com.nagarro.library.book.model.vo.BookVO를 가져옵니다;com.nagarro.library.book.service.BookService를 가져옵니다;com.nagarro.library.common.PageListResponse를 가져옵니다;com.nagarro.library.enums.BookEnum을 가져옵니다;com.nagarro.library.enums.ErrorCodeEnum을 가져옵니다;com.nagarro.library.exception.BusinessException을 가져옵니다;com.nagarro.library.user.helper.JwtTokenHelper를 가져옵니다;org.springframework.beans.BeanUtils를 가져옵니다;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.ArrayList;java.util.Date;import java.util.List;@Servicepublic class BookServiceImpl implements BookService { @Autowired 비공개 북매퍼 bookMapper; @Autowired private JwtTokenHelper jwtTokenHelper; @Override public BookDTO addBook(BookDTO bookDTO, String token) { //step1: 도서 저자, 제목, 출판사, 출판일, 장르 유효성 검사 //validateBook(bookDTO); //2단계: 책 존재 여부 판단 boolean exist = bookMapper.bookExist(bookDTO.getAuthor(), bookDTO.getTitle(), bookDTO.getPublisher()); if (exist) { 새로운 BusinessException(ErrorCodeEnum.BOOK\_EXIST)을 던집니다; } //3단계: bookDTO를 책으로 변환하기 책 책 = 새로운 책(); BeanUtils.copyProperties(bookDTO, book); book.setStatus(BookEnum.AVAILABLE.getBookCode()); // jwtTokenHelper.getUserIdFromToken(token); // 긴 사용자 아이디 = jwtTokenHelper.getUserIdFromToken(token); book.setCreatedBy(jwtTokenHelper.getUserIdFromToken(token)); //4단계: 책 저장 bookMapper.insert(book); //5단계: 책을 bookDTO로 변환하기 BookDTO 결과 = 새로운 BookDTO(); BeanUtils.copyProperties(book, result); result.setStatus(BookEnum.getBookStatus(book.getStatus())); 결과를 반환합니다; } @Override public BookDTO updateBook(BookDTO book, String token) { //step1: 도서 저자, 제목, 출판사, 출판일, 장르 유효성 검사 //validateBook(bookDTO); //2단계: ID로 책 가져오기 책 책DB = bookMapper.selectById(book.getId()); //3단계: 책 저자, 제목, 출판사가 책DB와 동일한지 판단합니다. if (!StringUtils.equals(bookDB.getAuthor(), book.getAuthor()) || !StringUtils.equals(bookDB.getTitle(), book.getTitle()) || !StringUtils.equals(bookDB.getPublisher(), book.getPublisher()))) { //STEP4: 책이 존재하는지 판단하기 boolean exist = bookMapper.bookExist(book.getAuthor(), book.getTitle(), book.getPublisher()); if (exist) { 새로운 BusinessException(ErrorCodeEnum.BOOK\_EXIST)을 던집니다; } } //5단계: bookDTO를 책으로 변환하기 책 책업데이트 = 새로운 책(); BeanUtils.copyProperties(book, bookUpdate); bookUpdate.setUpdatedBy(jwtTokenHelper.getUserIdFromToken(token)); //6단계: 책 업데이트 bookMapper.updateById(bookUpdate); 반환 책; } @Override public PageListResponse bookList(PageQueryDTO pageQuery, String token) { //step1: 주문 유형은 asc 또는 desc만, 주문 필드는 저자, 제목, 출판사, 출판일, 장르만 유효성 확인 //2단계: 도서 상태 if (!StringUtil.isEmpty(pageQuery.getStatus())) { pageQuery.setStatus(String.valueOf(BookEnum.getBookCode(pageQuery.getStatus()))); } //3단계: 페이지 리스트 응답 가져오기 마이바티스 페이지 헬퍼 사용 페이지 책목록DTOPage = PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize()); 북맵퍼.북리스트(페이지쿼리); 목록 결과 = bookListDTOPage.getResult(); //4단계: 책을 bookDTO로 변환하기 List bookDTOList = new ArrayList<>(); for (Book book : result) { BookDTO bookDTO = new BookDTO(); BeanUtils.copyProperties(book, bookDTO); bookDTO.setStatus(BookEnum.getBookStatus(book.getStatus())); //5단계: 책 사용 가능 날짜가 null이거나 이전인지 판단합니다. if (book.getAvailableDate() == null || book.getAvailableDate().before(new Date())) { bookDTO.setAvailableDate(new Date()); } bookDTOList.add(bookDTO); } //6단계: bookDTOList를 bookVO로 변환하기 BookVO bookVO = 새로운 BookVO(); BeanUtils.copyProperties(bookListDTOPage, bookVO); bookVO.setData(bookDTOList); return bookVO; }}