@@ -18,76 +18,84 @@ import com.example.demo.repository.BookCollectionRepository;
@Service
public class BookAdminService {
private final BookCollectionRepository bookRepository ;
private final BookCollectionRepository bookRepository ;
public BookAdminService ( BookCollectionRepository bookRepository ) {
this . bookRepository = bookRepository ;
}
public BookAdminService ( BookCollectionRepository bookRepository ) {
this . bookRepository = bookRepository ;
}
/** 入力チェック( ISBN必須13文字以内、書籍名必須50、よみ100、出版社50) 。※ISBNを数字13桁に限定するかは要確認 */
public List < String > validate ( BookForm f ) {
f . trim ( ) ;
List < String > e = new ArrayList < > ( ) ;
if ( f . getIsbn ( ) . isEmpty ( ) ) e . add ( " ISBNを入力してください。 " ) ;
else if ( f . getIsbn ( ) . length ( ) > 13 ) e . add ( " ISBNは13文字以内で 入力してください。 " ) ;
if ( f . getNmBook ( ) . isEmpty ( ) ) e . add ( " 書籍名を入力してください。 " ) ;
else if ( f . getNmBook ( ) . length ( ) > 50 ) e . add ( " 書籍名は50 文字以内で入力してください。" ) ;
if ( f . getKn Book ( ) . length ( ) > 100 ) e . add ( " 書籍名よみは100文字以内で入力してください。 " ) ;
if ( f . getPublisher ( ) . length ( ) > 50 ) e . add ( " 出版社は50文字以内で 入力してください。" ) ;
return e ;
}
/** 入力チェック( ISBN必須13文字以内、書籍名必須50、よみ100、出版社50) 。※ISBNを数字13桁に限定するかは要確認 */
public List < String > validate ( BookForm f ) {
f . trim ( ) ;
List < String > e = new ArrayList < > ( ) ;
if ( f . getIsbn ( ) . isEmpty ( ) )
e . add ( " ISBNを 入力してください。 " ) ;
else if ( f . getIsbn ( ) . length ( ) > 13 )
e . add ( " ISBNは13 文字以内で入力してください。" ) ;
if ( f . getNm Book ( ) . isEmpty ( ) )
e . add ( " 書籍名を 入力してください。" ) ;
else if ( f . getNmBook ( ) . length ( ) > 50 )
e . add ( " 書籍名は50文字以内で入力してください。 " ) ;
if ( f . getKnBook ( ) . length ( ) > 100 )
e . add ( " 書籍名よみは100文字以内で入力してください。 " ) ;
else if ( ! f . getKnBook ( ) . matches ( " ^[ \\ u3040- \\ u309F]+$ " ) )
e . add ( " 書籍名よみはひらがなで入力してください " ) ;
if ( f . getPublisher ( ) . length ( ) > 50 )
e . add ( " 出版社は50文字以内で入力してください。 " ) ;
return e ;
}
@Transactional ( readOnly = true )
public BookCollection find ( Integer idBook ) {
return bookRepository . findById ( idBook )
. orElseThrow ( ( ) - > new BusinessException ( " 対象の書籍が見つかりません。 " ) ) ;
}
@Transactional ( readOnly = true )
public BookCollection find ( Integer idBook ) {
return bookRepository . findById ( idBook )
. orElseThrow ( ( ) - > new BusinessException ( " 対象の書籍が見つかりません。 " ) ) ;
}
public BookForm toForm ( BookCollection b ) {
BookForm f = new BookForm ( ) ;
f . setIdBook ( b . getIdBook ( ) ) ;
f . setIsbn ( b . getIsbn ( ) ) ;
f . setNmBook ( b . getNmBook ( ) ) ;
f . setKnBook ( b . getKnBook ( ) = = null ? " " : b . getKnBook ( ) ) ;
f . setPublisher ( b . getPublisher ( ) = = null ? " " : b . getPublisher ( ) ) ;
f . setNote ( b . getNote ( ) = = null ? " " : b . getNote ( ) ) ;
return f ;
}
public BookForm toForm ( BookCollection b ) {
BookForm f = new BookForm ( ) ;
f . setIdBook ( b . getIdBook ( ) ) ;
f . setIsbn ( b . getIsbn ( ) ) ;
f . setNmBook ( b . getNmBook ( ) ) ;
f . setKnBook ( b . getKnBook ( ) = = null ? " " : b . getKnBook ( ) ) ;
f . setPublisher ( b . getPublisher ( ) = = null ? " " : b . getPublisher ( ) ) ;
f . setNote ( b . getNote ( ) = = null ? " " : b . getNote ( ) ) ;
return f ;
}
/** 新規登録:貸出可として登録する。 */
@Transactional
public Integer register ( BookForm f , LoginUser admin ) {
check ( f ) ;
BookCollection b = new BookCollection ( ) ;
apply ( b , f ) ;
b . setFlgLending ( Const . LENDING_OK ) ;
b . setIdUpdate ( admin . getIdEmployee ( ) ) ;
b . setDateUpdate ( LocalDateTime . now ( ) ) ;
return bookRepository . save ( b ) . getIdBook ( ) ;
}
/** 新規登録:貸出可として登録する。 */
@Transactional
public Integer register ( BookForm f , LoginUser admin ) {
check ( f ) ;
BookCollection b = new BookCollection ( ) ;
apply ( b , f ) ;
b . setFlgLending ( Const . LENDING_OK ) ;
b . setIdUpdate ( admin . getIdEmployee ( ) ) ;
b . setDateUpdate ( LocalDateTime . now ( ) ) ;
return bookRepository . save ( b ) . getIdBook ( ) ;
}
/** 編集:書籍情報のみ更新する(貸出フラグは変更しない)。 */
@Transactional
public void update ( BookForm f , LoginUser admin ) {
check ( f ) ;
BookCollection b = find ( f . getIdBook ( ) ) ;
apply ( b , f ) ;
b . setIdUpdate ( admin . getIdEmployee ( ) ) ;
b . setDateUpdate ( LocalDateTime . now ( ) ) ;
}
/** 編集:書籍情報のみ更新する(貸出フラグは変更しない)。 */
@Transactional
public void update ( BookForm f , LoginUser admin ) {
check ( f ) ;
BookCollection b = find ( f . getIdBook ( ) ) ;
apply ( b , f ) ;
b . setIdUpdate ( admin . getIdEmployee ( ) ) ;
b . setDateUpdate ( LocalDateTime . now ( ) ) ;
}
private void check ( BookForm f ) {
List < String > errors = validate ( f ) ;
if ( ! errors . isEmpty ( ) ) throw new BusinessException ( String . join ( " " , errors ) ) ;
}
private void check ( BookForm f ) {
List < String > errors = validate ( f ) ;
if ( ! errors . isEmpty ( ) )
throw new BusinessException ( String . join ( " " , errors ) ) ;
}
private static void apply ( BookCollection b , BookForm f ) {
b . setIsbn ( f . getIsbn ( ) ) ;
b . setNm Book ( f . getNm Book ( ) ) ;
b . setKnBook ( f . getKnBook ( ) . isEmpty ( ) ? null : f . getKnBook ( ) ) ;
b . setPublisher ( f . getPublisher ( ) . isEmpty ( ) ? null : f . getPublisher ( ) ) ;
b . setNote ( f . getNote ( ) . isEmpty ( ) ? null : f . getNote ( ) ) ;
}
private static void apply ( BookCollection b , BookForm f ) {
b . setIsbn ( f . getIsbn ( ) ) ;
b . setNmBook ( f . getNmBook ( ) ) ;
b . setKn Book ( f . getKnBook ( ) . isEmpty ( ) ? null : f . getKn Book ( ) ) ;
b . setPublisher ( f . getPublisher ( ) . isEmpty ( ) ? null : f . getPublisher ( ) ) ;
b . setNote ( f . getNote ( ) . isEmpty ( ) ? null : f . getNote ( ) ) ;
}
}