Skip to content

Commit

Permalink
Admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
yamada5131 committed Sep 13, 2024
1 parent 10b34a3 commit 0a0ab68
Show file tree
Hide file tree
Showing 49 changed files with 3,609 additions and 976 deletions.
20 changes: 13 additions & 7 deletions ecommerce-app/app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ProductCategory;
use App\Http\Requests\CategoryRequest;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
Expand All @@ -13,6 +12,12 @@ class CategoryController extends Controller
// $categories = ProductCategory::all();
// return view('admin.categories.index',compact('categories'));
// }
public function index()
{
$categories = ProductCategory::all();

return view('admin.categories.index', compact('categories'));
}

public function create()
{
Expand All @@ -32,7 +37,8 @@ public function store(CategoryRequest $request)
public function edit($id)
{
$category = ProductCategory::findOrFail($id);
return view('admin.categories.edit',compact('category'));

return view('admin.categories.edit', compact('category'));
}

public function update(CategoryRequest $request, $id)
Expand All @@ -43,16 +49,16 @@ public function update(CategoryRequest $request, $id)

$category->update($validated);

return redirect()->route('/admin')
->with('success','Category updated successfully');
return redirect()->route('categories.index')
->with('success', 'Category updated successfully');
}

public function destroy($id)
{
$category = ProductCategory::findOrFail($id);
$category->delete();

return redirect()->route('/admin')
->with('success','Category deleted successfully');
return redirect()->route('categories.index')
->with('success', 'Category deleted successfully');
}
}
12 changes: 5 additions & 7 deletions ecommerce-app/app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
function __construct()
{
public function __construct() {}

}

public function index()
{
$products = Product::with('userReviews')
->withAvg('userReviews', 'rating')
->withCount('userReviews')
->get()
->sortByDesc(['trendRating']);
// ->orderByDesc(['trendRating'])
->paginate(20);

// dd($products->toArray());

return view('home', [
'user' => Auth::user(),
Expand Down
61 changes: 60 additions & 1 deletion ecommerce-app/app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,71 @@

namespace App\Http\Controllers;

use App\Models\OrderDetail;
use App\Models\OrderItem;
use App\Models\OrderStatus;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\RedirectResponse;

class OrderController extends Controller
{
public function index(): View
{
return view('orders.index');
// TODO: Replace this with the authenticated user
// $user = Auth::user();
$user = User::all()->random();
$orderItems = $user->shoppingCarts->first()->shoppingCartItems;
$defaultAddress = $user->userAddresses->where('is_default', true)->first()->address;

$totalPrice = $orderItems->sum(function ($item) {
return $item->product->price * $item->qty;
});

return view('components.orders.index', compact('user', 'orderItems', 'defaultAddress', 'totalPrice'));
}

public function store(Request $request): RedirectResponse
{
$orderDetail = null;

DB::transaction(function () use ($request, &$orderDetail) {
// dd(request()->all());
$orderStatus = OrderStatus::create([
'status' => 'Pending',
]);

$orderDetail = OrderDetail::create([
'user_id' => $request->user_id,
'address_id' => $request->address_id,
'order_date' => $request->order_date,
'order_total' => $request->order_total,
'order_status_id' => $orderStatus->id,
]);

foreach ($request->order_items as $item) {
OrderItem::create([
'order_detail_id' => $orderDetail->id,
'product_id' => $item['product_id'],
'qty' => $item['qty'],
'price' => $item['price'],
]);
}
});

return redirect()->route('orders.confirmation', ['order_id' => $orderDetail->id]);
}

public function showConfirmation($order_id): View
{
$orderDetail = OrderDetail::with(['user', 'address'])->findOrFail($order_id);

return view('components.orders.confirmation', [
'orderDetail' => $orderDetail,
'user' => $orderDetail->user,
'address' => $orderDetail->address,
]);
}
}
69 changes: 37 additions & 32 deletions ecommerce-app/app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Http\Requests\ProductRequest;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\UserReview;
use App\Models\ViewedProduct;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class ProductController extends Controller
{
// public function index()
// {
// $products = Product::all();
// return view('admin.products.index', compact('products'));
// }
public function index()
{
$products = Product::all();

return view('admin.crud.products', compact('products'));
}

public function show(Product $product)
{
$userReviews = UserReview::withWhereHas('orderItem', function ($query) use ($product){
$userReviews = UserReview::withWhereHas('orderItem', function ($query) use ($product) {
$query->where('product_id', $product->id);
})->get();
$userReviewCount = $userReviews->count();
$userReviewAverage = $userReviews->avg('rating');

return view('products.show', [
'product' => $product,
'userReviews' => $userReviews,
Expand All @@ -37,45 +36,51 @@ public function show(Product $product)
public function create()
{
$categories = ProductCategory::all();
return view('admin.products.create', compact('categories'));
}

public function store(ProductRequest $request)
{
$product = new Product($request->validated());

if ($request->hasFile('image')) {
$product->image = $request->file('image')->store('products', 'public');
}

$product->save();
return redirect()->route('/admin')
->with('success', 'Product created successfully.');
return view('admin.products.create', compact('categories'));
}

public function edit(Product $product)
{
$categories = ProductCategory::all();

return view('admin.products.edit', compact('product'));
}

public function update(ProductRequest $request, Product $product)
{
$product->update($request->validated());
Product::where('id', $product->id)
->update([
'name' => $request->name,
// 'description' => $request->description,
'price' => $request->price,
'image_url' => $request->image_url,
'qty_in_stock' => $request->qty_in_stock,
]);

if ($request->hasFile('image')) {
$product->image = $request->file('image')->store('products', 'public');
}
return redirect(route('admin.products.index'));
}

public function store(Request $request)
{
$product = new Product();

$product->name = $request->name;
$product->description = $request->description;
$product->price = $request->price;
$product->image_url = $request->image_url;
$product->qty_in_stock = $request->qty_in_stock;
$product->product_category_id = '8ce2ad96-0604-4c5a-8bee-bdd91462d580';

$product->save();
return redirect()->route('/admin')
->with('success', 'Product updated successfully.');

return redirect(route('admin.products.index'));
}

public function destroy(Product $product)
{
$product->delete();
return redirect()->route('/admin')
->with('success', 'Product deleted successfully.');

return redirect(route('admin.products.index'));
}
}
Loading

0 comments on commit 0a0ab68

Please sign in to comment.