Skip to content

Commit 8a6e54f

Browse files
committed
Merge branch 'main' into demo
2 parents a4fa1a7 + 2ce5178 commit 8a6e54f

File tree

80 files changed

+6093
-1857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+6093
-1857
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
# Laravel E-commerce Website
2+
E-commerce application built with Laravel, Vue.js, Tailwind.css and Alpine.js. <br>
3+
4+
> If you want to see every single step how this E-commerce application is build and learn how to build your own Full Stack applications, check my website [thecodeholic.com](https://thecodeholic.com)
5+
6+
## Demo
7+
Admin Panel: https://admin.lcommerce.net
8+
```
9+
Email: admin@example.com
10+
Password: admin123
11+
```
12+
13+
Website: https://lcommerce.net
14+
15+
```
16+
Email: user1@example.com
17+
Password: useruser1
18+
19+
20+
Email: user2@example.com
21+
Password: useruser2
22+
```
223

324
## Installation
4-
Make sure you have environment setup properly. You will need MySQL, PHP8.1 and composer.
25+
Make sure you have environment setup properly. You will need MySQL, PHP8.1, Node.js and composer.
526

627
### Install Laravel Website + API
728
1. Download the project (or clone using GIT)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Order;
6+
use Illuminate\Console\Command;
7+
8+
class DeleteUnpaidOrders extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'delete:unpaid-orders {hours=24}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Delete unpaid orders after x amount of hours';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return int
28+
*/
29+
public function handle()
30+
{
31+
$hours = $this->argument('hours');
32+
$count = Order::deleteUnpaidOrders($hours);
33+
$this->info("$count unpaid orders were deleted");
34+
return Command::SUCCESS;
35+
}
36+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\StoreCategoryRequest;
7+
use App\Http\Requests\UpdateCategoryRequest;
8+
use App\Http\Resources\CategoryResource;
9+
use App\Http\Resources\CategoryTreeResource;
10+
use App\Models\Category;
11+
12+
class CategoryController extends Controller
13+
{
14+
/**
15+
* Display a listing of the resource.
16+
*/
17+
public function index()
18+
{
19+
$sortField = request('sort_field', 'updated_at');
20+
$sortDirection = request('sort_direction', 'desc');
21+
22+
$categories = Category::query()
23+
->orderBy($sortField, $sortDirection)
24+
->latest()
25+
->get();
26+
27+
return CategoryResource::collection($categories);
28+
}
29+
30+
public function getAsTree()
31+
{
32+
return Category::getActiveAsTree(CategoryTreeResource::class);
33+
}
34+
35+
/**
36+
* Store a newly created resource in storage.
37+
*/
38+
public function store(StoreCategoryRequest $request)
39+
{
40+
$data = $request->validated();
41+
$data['created_by'] = $request->user()->id;
42+
$data['updated_by'] = $request->user()->id;
43+
$category = Category::create($data);
44+
45+
return new CategoryResource($category);
46+
}
47+
48+
/**
49+
* Update the specified resource in storage.
50+
*/
51+
public function update(UpdateCategoryRequest $request, Category $category)
52+
{
53+
$data = $request->validated();
54+
$data['updated_by'] = $request->user()->id;
55+
$category->update($data);
56+
57+
return new CategoryResource($category);
58+
}
59+
60+
/**
61+
* Remove the specified resource from storage.
62+
*/
63+
public function destroy(Category $category)
64+
{
65+
$category->delete();
66+
67+
return response()->noContent();
68+
}
69+
}

app/Http/Controllers/Api/CustomerController.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Models\CustomerAddress;
1515
use http\Env\Request;
1616
use Illuminate\Support\Facades\DB;
17+
use Illuminate\Support\Facades\Log;
1718

1819
class CustomerController extends Controller
1920
{
@@ -30,15 +31,14 @@ public function index()
3031
$sortDirection = request('sort_direction', 'desc');
3132

3233
$query = Customer::query()
33-
->orderBy("customers.$sortField", $sortDirection)
34-
;
34+
->with('user')
35+
->orderBy("customers.$sortField", $sortDirection);
3536
if ($search) {
3637
$query
3738
->where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'like', "%{$search}%")
3839
->join('users', 'customers.user_id', '=', 'users.id')
3940
->orWhere('users.email', 'like', "%{$search}%")
40-
->orWhere('customers.phone', 'like', "%{$search}%")
41-
;
41+
->orWhere('customers.phone', 'like', "%{$search}%");
4242
}
4343

4444
$paginator = $query->paginate($perPage);
@@ -72,23 +72,34 @@ public function update(CustomerRequest $request, Customer $customer)
7272
$shippingData = $customerData['shippingAddress'];
7373
$billingData = $customerData['billingAddress'];
7474

75-
$customer->update($customerData);
75+
DB::beginTransaction();
76+
try {
77+
$customer->update($customerData);
7678

77-
if ($customer->shippingAddress) {
78-
$customer->shippingAddress->update($shippingData);
79-
} else {
80-
$shippingData['customer_id'] = $customer->user_id;
81-
$shippingData['type'] = AddressType::Shipping->value;
82-
CustomerAddress::create($shippingData);
83-
}
84-
if ($customer->billingAddress) {
85-
$customer->billingAddress->update($billingData);
86-
} else {
87-
$billingData['customer_id'] = $customer->user_id;
88-
$billingData['type'] = AddressType::Billing->value;
89-
CustomerAddress::create($billingData);
79+
if ($customer->shippingAddress) {
80+
$customer->shippingAddress->update($shippingData);
81+
} else {
82+
$shippingData['customer_id'] = $customer->user_id;
83+
$shippingData['type'] = AddressType::Shipping->value;
84+
CustomerAddress::create($shippingData);
85+
}
86+
87+
if ($customer->billingAddress) {
88+
$customer->billingAddress->update($billingData);
89+
} else {
90+
$billingData['customer_id'] = $customer->user_id;
91+
$billingData['type'] = AddressType::Billing->value;
92+
CustomerAddress::create($billingData);
93+
}
94+
} catch (\Exception $e) {
95+
DB::rollBack();
96+
97+
Log::critical(__METHOD__ . ' method does not work. '. $e->getMessage());
98+
throw $e;
9099
}
91100

101+
DB::commit();
102+
92103
return new CustomerResource($customer);
93104
}
94105

app/Http/Controllers/Api/OrderController.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Models\Api\Product;
1212
use App\Models\Order;
1313
use Illuminate\Http\Request;
14+
use Illuminate\Support\Facades\DB;
1415
use Illuminate\Support\Facades\Mail;
1516

1617
class OrderController extends Controller
@@ -28,6 +29,8 @@ public function index()
2829
$sortDirection = request('sort_direction', 'desc');
2930

3031
$query = Order::query()
32+
->withCount('items')
33+
->with('user.customer')
3134
->where('id', 'like', "%{$search}%")
3235
->orderBy($sortField, $sortDirection)
3336
->paginate($perPage);
@@ -37,6 +40,8 @@ public function index()
3740

3841
public function view(Order $order)
3942
{
43+
$order->load('items.product');
44+
4045
return new OrderResource($order);
4146
}
4247

@@ -47,10 +52,27 @@ public function getStatuses()
4752

4853
public function changeStatus(Order $order, $status)
4954
{
50-
$order->status = $status;
51-
$order->save();
55+
DB::beginTransaction();
56+
try {
57+
$order->status = $status;
58+
$order->save();
59+
60+
if ($status === OrderStatus::Cancelled->value) {
61+
foreach ($order->items as $item) {
62+
$product = $item->product;
63+
if ($product && $product->quantity !== null) {
64+
$product->quantity += $item->quantity;
65+
$product->save();
66+
}
67+
}
68+
}
69+
Mail::to($order->user)->send(new OrderUpdateEmail($order));
70+
} catch (\Exception $e) {
71+
DB::rollBack();
72+
throw $e;
73+
}
5274

53-
Mail::to($order->user)->send(new OrderUpdateEmail($order));
75+
DB::commit();
5476

5577
return response('', 200);
5678
}

0 commit comments

Comments
 (0)