Skip to content

Commit

Permalink
01 add UsersTableSeeder
Browse files Browse the repository at this point in the history
02 adding product update
03 add comments to the product (model, migration, controller, view)
04 cosmetic changes to existing mapping and style files
  • Loading branch information
yo committed May 21, 2019
1 parent 233036b commit 64e3d54
Show file tree
Hide file tree
Showing 17 changed files with 644 additions and 112 deletions.
15 changes: 15 additions & 0 deletions app/Comment.php
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
protected $guarded = [];
// protected $fillable = ['comment_string'];

public function product() {
return $this->belongsTo(Product::class);
}
}
73 changes: 73 additions & 0 deletions app/Http/Controllers/ProductCommentsController.php
@@ -0,0 +1,73 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use App\Product;
use Illuminate\Support\Facades\Validator;

class ProductCommentsController extends Controller
{
public function index() { // testing deletion of product comments upon product removal
return $comments = Comment::all();
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Product $product) {

// $validator = Validator::make($request->all(), [
// 'product_id' => 'required|integer',
// 'user_id' => 'required|integer',
// 'guest_name' => 'nullable|string',
// 'comment_string' => 'required|string',
// ]);

// if ($validator->fails()) {
// return back()->withErrors($validator)->withInput();
// }

$comment = Comment::create([
'product_id' => $product->id,
'user_id' => request('user_id'),
'guest_name' => request('guest_name'),
'comment_string' => request('comment_string'),
]);

// return back();
return redirect('/products/' . $product->id . '#comment_' . $comment->id);
}

public function update(Comment $comment) {
// dd($comment);
// dd(request()->all());

$comment->update([
'comment_string' => request('comment_string'),
]);

// return redirect()->route('productsShow', ['product' => $comment->product_id]);
return redirect('/products/' . $comment->product_id . '#comment_' . $comment->id);
}

/**
* Remove the specified resource from storage.
*
* @param \App\Comment $comment
* @return \Illuminate\Http\Response
*/
public function destroy(Comment $comment)
{
// dd($comment);
$product_id = $comment->product_id;
$comment->delete();

return redirect()->route('productsShow', ['product' => $product_id]);
}

}
144 changes: 74 additions & 70 deletions app/Http/Controllers/ProductsController.php
Expand Up @@ -42,6 +42,16 @@ public function create()
return view('products.create');
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}

/**
* Store a newly created resource in storage.
*
Expand Down Expand Up @@ -79,24 +89,16 @@ public function store(Request $request)
return back()->withErrors(['something wrong!'])->withInput();
}

if ($request->file('image')) {

$directory = 'public/images/products/' . $product->id;
Storage::makeDirectory($directory);

$file = $request->file('image');
$filename = $file->getClientOriginalName();
if (request()->file('image')) {

Storage::putFileAs($directory, $file, $filename);
$product->image = $this->storeImage(request()->file('image'), $product->id);

$product->image = $filename;

if (!$product->update()) {
if (!$product->image or !$product->update()) {
return back()->withErrors(['something wrong. err' . __line__])->withInput();
}

}
return redirect()->route('products');

return redirect()->route('productsShow', ['product' => $product->id]);
}

/**
Expand All @@ -106,63 +108,44 @@ public function store(Request $request)
* @param int $id
* @return \Illuminate\Http\Response
*/
// public function update(Request $request, $id)
// {
// $validator = Validator::make($request->all(), [
// 'name' => 'required|max:255',
// 'manufacturer' => 'nullable|string',
// 'materials' => 'nullable|string',
// 'description' => 'nullable|string',
// 'image' => 'image',
// 'year_manufacture' => 'nullable|integer',
// 'price' => 'nullable|integer',
// 'added_by_user_id' => 'required|integer',
// ]);

// if ($validator->fails()) {
// return back()->withErrors($validator)->withInput();
// }

// $product = Product::find($id);

// $product->name = $request->get('name');
// $product->manufacturer = $request->get('manufacturer') ?? '';
// $product->materials = $request->get('materials') ?? '';
// $product->description = $request->get('description') ?? '';
// $product->year_manufacture = $request->get('year_manufacture') ?? 0;
// $product->price = $request->get('price') ?? 0;
// $product->added_by_user_id = $request->get('added_by_user_id');

// if (!$product->update()) {
// return back()->withErrors(['something wrong!'])->withInput();
// }

// if ($request->file('image')) {

// $upload_folder = 'public/images/' . $product->id;
// $upload_folder_ = __DIR__ . '/../../../storage/app/' . $upload_folder;

// // if (!mkdir($upload_folder_)){
// // // return back()->withErrors(['failed to create upload_folder'])->withInput();
// // }
// if (!is_dir($upload_folder_)) {
// mkdir($upload_folder_);
// }

// $file = $request->file('image');
// $filename = $file->getClientOriginalName();

// Storage::putFileAs($upload_folder, $file, $filename);

// $product->image = $filename;

// if ($product->update()) {
// return redirect()->route('products');
// }
// return back()->withErrors(['something wrong!!'])->withInput();
// }
// return redirect()->route('products');
// }
public function update(Product $product)
{
$validator = Validator::make(request()->all(), [
'name' => 'required|max:255',
'manufacturer' => 'nullable|string',
'materials' => 'nullable|string',
'description' => 'nullable|string',
'image' => 'image',
'year_manufacture' => 'nullable|integer',
'price' => 'nullable|integer',
'edited_by_user_id' => 'required|integer',
]);

if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}

$product->update([
'name' => request('name'),
'manufacturer' => request('manufacturer'),
'materials' => request('materials'),
'description' => request('description'),
'year_manufacture' => request('year_manufacture'),
'edited_by_user_id' => request('edited_by_user_id'),
]);


if (request()->file('image')) {

$product->image = $this->storeImage(request()->file('image'), $product->id);

if (!$product->image or !$product->update()) {
return back()->withErrors(['something wrong. err' . __line__])->withInput();
}
}

return redirect()->route('productsShow', ['product' => $product->id]);
}

/**
* Remove the specified resource from storage.
Expand All @@ -181,11 +164,32 @@ public function destroy($id)
}

// destroy product comments
$product->comments()->delete();

// destroy product
$product->delete();

return redirect()->route('products');
}

/**
* Store image product.
*
* @param \Illuminate\Http\Request $request
* @return string $filename or false
*/
private function storeImage($image, $product_id) {

$directory = 'public/images/products/' . $product_id;
$filename = $image->getClientOriginalName();

if (Storage::makeDirectory($directory)) {
if (Storage::putFileAs($directory, $image, $filename)) {
return $filename;
}
}

return FALSE;
}

}
6 changes: 5 additions & 1 deletion app/Product.php
Expand Up @@ -6,5 +6,9 @@

class Product extends Model
{
//
protected $guarded = [];

public function comments() {
return $this->hasMany(Comment::class);
}
}
12 changes: 12 additions & 0 deletions database/factories/CommentFactory.php
@@ -0,0 +1,12 @@
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Comment;
use Faker\Generator as Faker;

$factory->define(Comment::class, function (Faker $faker) {
return [
//
];
});
Expand Up @@ -19,10 +19,11 @@ public function up()
$table->string('manufacturer')->nullable();
$table->string('materials')->nullable();
$table->text('description')->nullable();
$table->string('image')->nullable();
$table->string('image')->nullable()->charset('utf8'); // mimes:jpeg,bmp,png
$table->integer('year_manufacture')->nullable();
$table->float('price', 8, 2)->nullable();
$table->integer('added_by_user_id');
$table->unsignedInteger('added_by_user_id');
$table->unsignedInteger('edited_by_user_id')->nullable();
$table->timestamps();
});
}
Expand Down
36 changes: 36 additions & 0 deletions database/migrations/2019_05_20_010314_create_comments_table.php
@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('product_id');
$table->unsignedInteger('user_id')->nullable();
// ??? $table->foreign('user_id')->references('id')->on('users');
$table->string('guest_name')->nullable();
$table->text('comment_string');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Expand Up @@ -15,6 +15,7 @@ public function run()

$this->call([
ProductsTableSeeder::class,
UsersTableSeeder::class,
]);
}
}
21 changes: 21 additions & 0 deletions database/seeds/UsersTableSeeder.php
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'Admin Name',
'email' => 'admin@gmail.com',
// 'role_id' => 1,
'password' => bcrypt('111111'),
]);
}
}

0 comments on commit 64e3d54

Please sign in to comment.