Yes, practice makes perfect.
Keep on building stuff, that is the way you learn.
I suggest that you get a subscription here, and check out some of the project courses, like
https://laracasts.com/series/code-breaking-workshop
I built my first project and it is an E-Commerce API written in Laravel. I have noticed that I lacked knowledge with "business logic" and what it was and what it meant. I recently implemented business logic with help from Gemini AI, and now understand that without business logic there is no solution. So my question is, will building a lot of different projects like social networks, CRM, CMS, WMS, ORM, Music streamers, and Personal projects help me be a better web developer with the fact that implementing the "business logic" is the key to programming? Obviously there is system design also and fundamentals and other things like DevOps. But is the business logic and building different projects with that business logic, the key to programming or am I mistaken? I feel that this makes sense to me. Like business logic is the meat in programming, and with it I can learn more by making more projects and understand programming better also as I create each project?
<?php
// Here is a sample of what I mean, at first my project looked very simple and just a CRUD
//but with business logic it makes sense!
// This is a CartService class for an E-Commerce Backend API used by the CartController class
namespace App\Services;
use App\Models\Cart;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Exception;
use Illuminate\Database\Eloquent\Collection;
class CartService {
public function getCart(): Cart {
return auth()->user()->cart()->firstOrCreate();
}
public function store(string $productId, int $quantity = 1): Cart {
return DB::transaction(function () use ($productId, $quantity) {
$product = Product::findOrFail($productId);
$isActive = true;
if($product->is_active == $isActive) {
if($product->stock_quantity >= $quantity) {
$cart = $this->getCart();
$cartItem = $cart->cartItems()->where('product_id', $product->id)->first();
if ($cartItem) {
$cartItem->quantity += $quantity;
$cartItem->save();
return $cart;
} else {
$cart->cartItems()->create([
'product_id' => $product->id,
'quantity' => $quantity,
]);
return $cart;
}
} else {
throw new Exception('Product out of stock: ' . $product->name);
}
} else {
throw new Exception('Product is not active: ' . $product->name);
}
});
}
public function show(string $id): Cart {
$cart = Cart::with('user', 'cartItems')->findOrFail($id);
return $cart;
}
public function index(): Collection {
$cart = Cart::where('user_id', auth()->user()->id)->with('cartItems')->get();
if ($cart->isEmpty()) {
return $cart;
}
return $cart;
}
public function delete(string $productId): Cart {
$cart = auth()->user()->cart();
$product = Product::findOrFail($productId);
$cart->cartItems()->where('product_id', $product->id)->delete();
return $cart;
}
public function update(string $productId, int $quantity): Cart {
return DB::transaction(function () use ($productId) {
$product = Product::findOrFail($productId);
$isActive = true;
if($product->is_active == $isActive) {
if($product->stock_quantity >= $quantity) {
$cart = $this->getCart();
$cartItem = $cart->cartItems()->where('product_id', $product->id)->first();
if ($cartItem) {
if ($quantity > 0) {
$cartItem->quantity = $quantity;
$cartItem->save();
return $cart;
} else {
$cartItem->delete();
return $cart;
}
}
} else {
throw new Exception('Product out of stock: ' . $product->name);
}
} else {
throw new Exception('Product not active: ' . $product->name);
}
});
}
public function calculateTotalAmountForCart(array $data): float {
$cart = auth()->user()->cart();
$totalAmount = 0;
foreach($cart->cartItems as $cartItem) {
$totalAmount += $cartItem->product->price * $cartItem->quantity;
}
if($data['shippingCost'] === 'Ground') {
$totalAmount += 12.00;
}
if($data['shippingCost'] === 'Standard') {
$totalAmount += 14.00;
}
if($data['shippingCost'] === 'Express') {
$totalAmount += 16.00;
}
$totalAmount += $data['tax_amount'];
return $totalAmount;
}
}
<?php
// This is a OrderService class for an E-Commerce Backend API used by the OrderController class
namespace App\Services;
use App\Models\Order;
use App\Models\Product;
use App\Models\Cart;
use App\Models\CartItem;
use App\Models\OrderItem;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use App\Events\OrderPlaced;
class OrderService {
public function store(array $data): Order {
return DB::transaction(function () use ($data) {
$user = auth()->user();
$cart = Cart::find($user->id);
$order = $user->orders()->create($data);
foreach($cart->cartItems as $cartItem) {
if ($cartItem->product->stock_quantity < $cartItem->quantity) {
throw new Exception('Product not in stock: ' . $cartItem->product->name);
}
}
foreach($cart->cartItems as $cartItem){
$order->orderItems()->create([
'product_id' => $cartItem->product->id,
'quantity' => $cartItem->quantity,
'product_name' => $cartItem->product->name,
'product_price' => $cartItem->product->price,
'subtotal' => $cartItem->product->price * $cartItem->quantity,
'product_sku_code' => $cartItem->product->sku,
]);
$product = Product::findOrFail($cartItem->product->id);
$option = 'decrement';
OrderPlaced::dispatch($product, $cartItem->quantity, $option);
}
$totalAmount = 0;
foreach($order->orderItems as $orderItem){
$totalAmount += $orderItem->subtotal;
}
$totalAmount += $order->tax_amount;
if($order->shipping_method === 'Ground'){
$order->update(['shipping_cost' => 12.00]);
$totalAmount += $order->shipping_cost;
}
if($order->shipping_method === 'Standard'){
$order->update(['shipping_cost' => 14.00]);
$totalAmount += $order->shipping_cost;
}
if($order->shipping_method === 'Express'){
$order->update(['shipping_cost' => 16.00]);
$totalAmount += $order->shipping_cost;
}
$order->update(['total_amount' => $totalAmount]);
return $order;
});
}
public function index(): LengthAwarePaginator {
$order = Order::with('user', 'orderItems')->paginate(15);
if ($order->isEmpty()) {
throw (new ModelNotFoundException)->setModel(Order::class);
}
return $order;
}
public function show(string $id): Order {
$order = Order::with('user', 'orderItems')->findOrFail($id);
return $order;
}
public function delete(string $id){
$order = Order::findOrFail($id);
$order->delete();
return $order;
}
public function getOrderItemsForUser(string $id){
$orders = Order::where('user_id', $id)->with('orderItems')->get();
if ($orders->isEmpty()) {
return $orders;;
}
return $orders;
}
}
Please or to participate in this conversation.