/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(CreateBookRequest $request)
 {
     $user = \Auth::User();
     if (!$user) {
         return view('auth.login')->withErrors('You are not loged in, please loged in !');
     }
     $book = $request->bookFillData();
     $book['user_id'] = $user->id;
     $book['published_at'] = Carbon::parse($request->get('published_at'))->format('Y-m-d');
     $fileSizeValidation = \Config::get('library.image_file_size');
     $newBook = Book::create($book);
     $newBook->syncAuthors($request->get('authors'));
     $this->manager->createDirectory($newBook->id);
     $file = $_FILES['image'];
     if ($file['size'] > 0) {
         // Additional image validation
         if (!starts_with($file['type'], 'image/')) {
             return Redirect::action('BookController@create')->withErrors('Invalid file format, please use image !');
         }
         $fileSize = $file['size'] / 1024;
         if ($fileSize > $fileSizeValidation) {
             return Redirect::action('BookController@create')->withErrors('The image may not be greater than ' . $fileSizeValidation . ' kilobytes. ');
         }
         $img = Image::make($_FILES['image']['tmp_name']);
         $img->resize(140, 140);
         $img->save('.' . \Config::get('library.uploads.webpath') . DIRECTORY_SEPARATOR . $newBook->id . '/cover.jpg');
     }
     return redirect::action('BookController@index')->withSuccess("The book with title '{$newBook->title}' was created.");
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Requests\CreateBookRequest $request)
 {
     $Book = \App\Book::findOrFail($id);
     $input = $request->all();
     if (is_null($Book)) {
         return response()->json(['msg' => 'success', 'Book' => 'Book not found'], 404);
     }
     if ($Book->update($input)) {
         return response()->json(['msg' => 'success'], 200);
     } else {
         return response()->json(['msg' => 'error', 'error' => 'cannot update record'], 400);
     }
 }