/**
  * @param Reservation $reservation
  * @param User $user
  * @param array $inputs
  * @return Reservation
  * @throws PreconditionException
  */
 protected function store(Reservation $reservation, User $user, array $inputs)
 {
     /** @var Book $book */
     $book = Book::where('asin', $inputs['asin'])->first();
     if (empty($book)) {
         throw new PreconditionException('book_not_found');
     }
     if ($book->inventory < $inputs['quantity']) {
         throw new PreconditionException('not_enough_book_inventory');
     }
     DB::transaction(function () use($user, $book, &$reservation, $inputs) {
         $affectedRows = $book->decrementInventory($inputs['quantity']);
         if ($affectedRows !== 1) {
             throw new PreconditionException('not_enough_book_inventory');
         }
         $reservation->user_id = $user->id;
         $reservation->book_id = $book->id;
         $reservation->quantity = $inputs['quantity'];
         if (empty($reservation->reservation_code)) {
             $reservation->reservation_code = $reservation->generateReservationCode();
         }
         $reservation->save();
     });
     return $reservation;
 }
 /**
  *
  */
 public function run()
 {
     Reservation::truncateWithIgnoreForeignKeyChecks();
     Book::truncateWithIgnoreForeignKeyChecks();
     User::truncateWithIgnoreForeignKeyChecks();
     DB::table('users')->insert([['id' => 1, 'api_token' => 'token1', 'name' => '氏名1'], ['id' => 2, 'api_token' => 'token2', 'name' => '氏名2']]);
     DB::table('books')->insert([['id' => 1, 'asin' => 'asin1', 'title' => '書籍1', 'price' => 1000, 'inventory' => 10]]);
     DB::table('reservations')->insert([['id' => 1, 'user_id' => 1, 'book_id' => 1, 'quantity' => 1, 'reservation_code' => 'code1'], ['id' => 2, 'user_id' => 1, 'book_id' => 1, 'quantity' => 2, 'reservation_code' => 'code2'], ['id' => 3, 'user_id' => 2, 'book_id' => 1, 'quantity' => 1, 'reservation_code' => 'code3']]);
 }