Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Task#80295] Inplement checkout page and logic #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ecommerce-app/.php-cs-fixer.cache

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ public function store(RegisterRequest $request): RedirectResponse
$user->telephone = $request->telephone;
$user->is_admin = false;

/*
if ($request->hasFile('photo')) {
$avatar = new ImageController();
$imageUrl = $avatar->storeImage($request, 'profile');
$user->photo = $imageUrl;
}
*/

$user->save();

Expand Down
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');
}
}
49 changes: 49 additions & 0 deletions ecommerce-app/app/Http/Controllers/GoogleAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;

use Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use Laravel\Socialite\Facades\Socialite;

class GoogleAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('google')->redirect();
}

public function callbackGoogle()
{
try {
$google_user = Socialite::driver('google')->user();
$user = User::where('google_id', $google_user->getId())->first();

if (!$user) {
$new_user = User::create([
'username' => $google_user->getName(),
'password' => Hash::make('123456789'),
'first_name' => "",
'last_name' => "",
'email' => $google_user->getEmail(),
'google_id' => $google_user->getId(),
'telephone' => "",
]);

Auth::login($new_user);

return redirect()->intended('dashboard');
} else {
Auth::login($user);

return redirect()->intended('dashboard');
}
} catch (\Throwable $th) {
dd("Something went wrong!" . $th->getMessage());
}
}
}
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'));
}
}
9 changes: 4 additions & 5 deletions ecommerce-app/app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use App\Http\Requests\ProfileUpdateRequest;

use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

class ProfileController extends Controller
{
Expand All @@ -30,16 +30,15 @@ public function edit(Request $request): View
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$user = User::find($request->user()->id);
$user->name = $request->name;
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->email = $request->email;

/*
if ($request->hasFile('photo')) {
$avatar = new ImageController();
$imageUrl = $avatar->storeImage($request, 'profile');
$user->photo = $imageUrl;
}
*/

$user->save();

Expand Down
Loading