restore composer.json, add mysqli extension
This commit is contained in:
177
app/Http/Controllers/API/AuthController.php
Executable file
177
app/Http/Controllers/API/AuthController.php
Executable file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Cart;
|
||||
use App\Models\User;
|
||||
use App\Services\API\SmsService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function resend(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'phone' => 'required',
|
||||
]);
|
||||
|
||||
$phone = preg_replace("/[^0-9]/", "", $request->phone);
|
||||
|
||||
// Define the same rate limit key to share attempts between auth and resend
|
||||
$rateLimitKey = 'verify_attempts_' . $phone;
|
||||
|
||||
// Set the same maximum attempts and decay time
|
||||
$maxAttempts = 5;
|
||||
$decayMinutes = 1;
|
||||
|
||||
// Check if the phone number has exceeded the rate limit
|
||||
if (RateLimiter::tooManyAttempts($rateLimitKey, $maxAttempts)) {
|
||||
$seconds = RateLimiter::availableIn($rateLimitKey);
|
||||
return response()->json([
|
||||
'message' => 'Too many attempts. Please try again in ' . $seconds . ' seconds.'
|
||||
], 429); // 429 Too Many Requests
|
||||
}
|
||||
|
||||
$user = User::where('phone', $phone)->first();
|
||||
|
||||
if ($user) {
|
||||
// send SMS with verify code
|
||||
$verify_code = SmsService::send($phone);
|
||||
|
||||
$user->update([
|
||||
'verify_code' => $verify_code,
|
||||
]);
|
||||
|
||||
// Increase the attempt count
|
||||
RateLimiter::hit($rateLimitKey, $decayMinutes * 60);
|
||||
|
||||
return response()->json(['phone' => $phone]);
|
||||
}
|
||||
|
||||
// return can't find user message
|
||||
return response()->json(['message' => 'User not found']);
|
||||
}
|
||||
|
||||
public function auth(Request $request)
|
||||
{
|
||||
$phone = preg_replace("/[^0-9]/", "", $request->input('phone'));
|
||||
|
||||
// Define the rate limit key based on the phone number.
|
||||
$rateLimitKey = 'verify_attempts_' . $phone;
|
||||
|
||||
// Set the maximum attempts and the decay time (e.g., 5 attempts every 1 minute)
|
||||
$maxAttempts = 5;
|
||||
$decayMinutes = 1;
|
||||
|
||||
// Check if the phone number has exceeded the rate limit
|
||||
if (RateLimiter::tooManyAttempts($rateLimitKey, $maxAttempts)) {
|
||||
$seconds = RateLimiter::availableIn($rateLimitKey);
|
||||
return response()->json([
|
||||
'message' => 'Too many attempts. Please try again in ' . $seconds . ' seconds.'
|
||||
], 429); // 429 Too Many Requests
|
||||
}
|
||||
|
||||
$user = User::where('phone', $phone)->first();
|
||||
|
||||
// send SMS with verify code
|
||||
$verify_code = SmsService::send($phone);
|
||||
|
||||
if ($user) {
|
||||
$user->update([
|
||||
'verify_code' => $verify_code,
|
||||
]);
|
||||
} else {
|
||||
User::create([
|
||||
'phone' => $phone,
|
||||
'verify_code' => $verify_code,
|
||||
]);
|
||||
}
|
||||
|
||||
// Increase the attempt count
|
||||
RateLimiter::hit($rateLimitKey, $decayMinutes * 60);
|
||||
|
||||
return response()->json(['phone' => $phone]);
|
||||
}
|
||||
|
||||
public function verify(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'phone' => 'required',
|
||||
'verify_code' => 'required',
|
||||
]);
|
||||
|
||||
$phone = preg_replace("/[^0-9]/", "", $request->phone);
|
||||
|
||||
$user = User::where('phone', $phone)->first();
|
||||
|
||||
// check if user exists
|
||||
if (!$user) {
|
||||
return response()->json([
|
||||
'message' => 'User not found'
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
// check if verify code is correct
|
||||
if ($user->verify_code != $request->verify_code) {
|
||||
return response([
|
||||
'message' => 'Verify code is incorrect'
|
||||
], Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$token = $user->createToken('token')->plainTextToken;
|
||||
$cookie = cookie('jwt', $token, 60 * 24 * 365, null, null, secure: true, httpOnly: true); // 1 year expiration
|
||||
|
||||
// clear verify code
|
||||
$user->update([
|
||||
'verify_code' => null,
|
||||
]);
|
||||
|
||||
// Update cart, remove token and add user_id
|
||||
$this->updateCart($user);
|
||||
|
||||
return response([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'phone' => $user->phone,
|
||||
'access_token' => $token
|
||||
]
|
||||
])->withCookie($cookie);
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
$cookie = Cookie::forget('jwt');
|
||||
|
||||
// logout
|
||||
auth()->user()->tokens()->delete();
|
||||
|
||||
return response([
|
||||
'message' => 'Success'
|
||||
])->withCookie($cookie);
|
||||
}
|
||||
|
||||
private function getLocalToken()
|
||||
{
|
||||
return request()->header('X-Application-Token');
|
||||
}
|
||||
|
||||
private function updateCart($user)
|
||||
{
|
||||
if ($this->getLocalToken()) {
|
||||
// Update carts, remove token and add user_id
|
||||
$carts = Cart::where('token', $this->getLocalToken())->get();
|
||||
foreach ($carts as $cart) {
|
||||
$cart->update([
|
||||
'token' => null,
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
app/Http/Controllers/API/BranchController.php
Executable file
17
app/Http/Controllers/API/BranchController.php
Executable file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\BranchResource;
|
||||
use App\Models\Branch;
|
||||
|
||||
class BranchController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$branches = Branch::all();
|
||||
|
||||
return BranchResource::collection($branches);
|
||||
}
|
||||
}
|
||||
18
app/Http/Controllers/API/BrandController.php
Executable file
18
app/Http/Controllers/API/BrandController.php
Executable file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\BrandPaginationResource;
|
||||
use App\Models\Brand;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BrandController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$brands = Brand::query()->orderBy("position")->paginate($request->limit ?? 10);
|
||||
|
||||
return (new BrandPaginationResource($brands))->response();
|
||||
}
|
||||
}
|
||||
338
app/Http/Controllers/API/CartController.php
Executable file
338
app/Http/Controllers/API/CartController.php
Executable file
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CartResource;
|
||||
use App\Models\PersonalAccessToken;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Cart;
|
||||
use App\Models\Currency;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CartController extends Controller
|
||||
{
|
||||
public function getCart()
|
||||
{
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
$bearer = $this->getBearerToken();
|
||||
|
||||
if ($bearer) {
|
||||
[$personalAccessTokenId, $token] = explode('|', $bearer, 2);
|
||||
$personalAccessToken = PersonalAccessToken::find($personalAccessTokenId);
|
||||
|
||||
if ($personalAccessToken && hash_equals($personalAccessToken->token, hash('sha256', $token))) {
|
||||
// Authenticated
|
||||
$cart = Cart::with('product')->whereHas('product', function ($product) {
|
||||
$product->where('child_id', null);
|
||||
})->where('user_id', $personalAccessToken->tokenable_id)->orderBy('id', 'desc')->get();
|
||||
}
|
||||
} else {
|
||||
// Check for local token in request headers
|
||||
$localToken = $this->getLocalToken();
|
||||
if ($localToken) {
|
||||
$cart = Cart::with('product')->whereHas('product', function ($product) {
|
||||
$product->where('child_id', null);
|
||||
})->where('token', $localToken)->get();
|
||||
}
|
||||
}
|
||||
|
||||
$price_solutions = 0;
|
||||
$discount_solutions = 0;
|
||||
$total_solutions = 0;
|
||||
|
||||
$price = 0;
|
||||
$discount = 0;
|
||||
$total = 0;
|
||||
|
||||
$getReadySolutionProductsCart = collect([]);
|
||||
$getSingleProductsCart = collect([]);
|
||||
|
||||
if (isset($cart)) {
|
||||
$getReadySolutionProductsCart = $cart->filter(function ($cart) {
|
||||
return $cart->product->is_ready_solution == true;
|
||||
});
|
||||
|
||||
$ready_solutions_count = 0;
|
||||
$price_solutions = $getReadySolutionProductsCart->filter(function ($cart) {
|
||||
return $cart->product->count >= $cart->count;
|
||||
})->map(function ($cart) use (&$ready_solutions_count) {
|
||||
$ready_solutions_count += $cart->count;
|
||||
$cart->product->price_total = !empty($cart->product->price_discount) ? $cart->product->price_discount * $cart->count : $cart->product->price * $cart->count;
|
||||
$cart->product->price_discount_total = !empty($cart->product->price_discount) ? ($cart->product->price - $cart->product->price_discount) * $cart->count : 0;
|
||||
$cart->product->price_products = $cart->product->price * $cart->count;
|
||||
return $cart;
|
||||
});
|
||||
|
||||
$discount_solutions = $price_solutions->sum('product.price_discount_total');
|
||||
$total_solutions = $price_solutions->sum('product.price_total');
|
||||
$price_solutions = $price_solutions->sum('product.price_products');
|
||||
|
||||
$single_products_count = 0;
|
||||
$getSingleProductsCart = collect($cart)->filter(function ($cart) {
|
||||
return $cart->product->is_ready_solution == false;
|
||||
});
|
||||
|
||||
$price = $getSingleProductsCart->filter(function ($cart) {
|
||||
return $cart->product->count >= $cart->count;
|
||||
})->map(function ($cart) use (&$single_products_count) {
|
||||
$single_products_count += $cart->count;
|
||||
$cart->product->price_total = !empty($cart->product->price_discount) ? $cart->product->price_discount * $cart->count : $cart->product->price * $cart->count;
|
||||
$cart->product->price_discount_total = !empty($cart->product->price_discount) ? ($cart->product->price - $cart->product->price_discount) * $cart->count : 0;
|
||||
$cart->product->price_products = $cart->product->price * $cart->count;
|
||||
return $cart;
|
||||
});
|
||||
|
||||
$discount = $price->sum('product.price_discount_total');
|
||||
$total = $price->sum('product.price_total');
|
||||
$price = $price->sum('product.price_products');
|
||||
}
|
||||
|
||||
// get currency from cache
|
||||
$currency = cache()->get('currency');
|
||||
|
||||
return [
|
||||
'data' => [
|
||||
'ready_solutions' => [
|
||||
'products' => CartResource::collection($getReadySolutionProductsCart),
|
||||
'price' => ceiling($price_solutions * $currency->dollar, 100),
|
||||
'discount' => ceiling($discount_solutions * $currency->dollar, 100),
|
||||
'total' => ceiling($total_solutions * $currency->dollar, 100),
|
||||
],
|
||||
'ready_solutions_count' => $ready_solutions_count,
|
||||
'single_products' => [
|
||||
'products' => CartResource::collection($getSingleProductsCart),
|
||||
'price' => ceiling($price * $currency->dollar, 100),
|
||||
'discount' => ceiling($discount * $currency->dollar, 100),
|
||||
'total' => ceiling($total * $currency->dollar, 100),
|
||||
],
|
||||
'single_products_count' => $single_products_count
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
private function getUser()
|
||||
{
|
||||
$bearer = $this->getBearerToken();
|
||||
|
||||
$type = null;
|
||||
$user = null;
|
||||
$token = null;
|
||||
|
||||
if ($bearer) {
|
||||
[$personalAccessTokenId, $token] = explode('|', $bearer, 2);
|
||||
$personalAccessToken = PersonalAccessToken::find($personalAccessTokenId);
|
||||
|
||||
if ($personalAccessToken && hash_equals($personalAccessToken->token, hash('sha256', $token))) {
|
||||
// Authenticated
|
||||
$type = 'user';
|
||||
$user = User::findOrFail($personalAccessToken->tokenable_id);
|
||||
$token = null;
|
||||
}
|
||||
} else {
|
||||
// Check for local token in request headers
|
||||
$localToken = $this->getLocalToken();
|
||||
if ($localToken) {
|
||||
$type = 'token';
|
||||
$user = null;
|
||||
$token = $localToken;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => $type,
|
||||
'user' => $user,
|
||||
'token' => $token
|
||||
];
|
||||
}
|
||||
|
||||
public function addToCart(Request $request)
|
||||
{
|
||||
try {
|
||||
// Validate the request
|
||||
$request->validate([
|
||||
'product_id' => 'required|exists:products,id',
|
||||
]);
|
||||
|
||||
// Retrieve the bearer token from the request
|
||||
$bearer = $this->getBearerToken();
|
||||
|
||||
if ($bearer) {
|
||||
[$personalAccessTokenId, $token] = explode('|', $bearer, 2);
|
||||
$personalAccessToken = PersonalAccessToken::find($personalAccessTokenId);
|
||||
|
||||
if ($personalAccessToken && hash_equals($personalAccessToken->token, hash('sha256', $token))) {
|
||||
// Authenticated
|
||||
$user = User::find($personalAccessToken->tokenable_id);
|
||||
|
||||
if ($user) {
|
||||
$this->updateOrCreateCart($user, $request->product_id);
|
||||
return response()->json(['status' => true, 'data' => null]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for local token in request headers
|
||||
$localToken = $this->getLocalToken();
|
||||
if (!$localToken) {
|
||||
return response()->json(['message' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
$this->updateOrCreateCartByToken($localToken, $request->product_id);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'message' => $e->getMessage()
|
||||
], 403);
|
||||
}
|
||||
|
||||
return response()->json(['status' => true, 'data' => null]);
|
||||
}
|
||||
|
||||
public function add(Cart $cart)
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
if (empty($user['type']))
|
||||
abort(403);
|
||||
|
||||
if ($user['type'] == 'token') {
|
||||
if ($cart->token != $user['token'])
|
||||
abort(403);
|
||||
} else if ($user['type'] == 'user') {
|
||||
if ($cart->user_id != $user['user']->id)
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$cart->update([
|
||||
'count' => $cart->count + 1
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'message' => trans('errors.try_again')
|
||||
])->setStatusCode(403);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->getCart();
|
||||
}
|
||||
|
||||
public function decrease(Cart $cart)
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
if (empty($user['type']))
|
||||
abort(403);
|
||||
|
||||
if ($user['type'] == 'token') {
|
||||
if ($cart->token != $user['token'])
|
||||
abort(403);
|
||||
} else if ($user['type'] == 'user') {
|
||||
if ($cart->user_id != $user['user']->id)
|
||||
abort(403);
|
||||
}
|
||||
|
||||
if ($cart->count <= 1) {
|
||||
$cart->delete();
|
||||
} else {
|
||||
$cart->update([
|
||||
'count' => $cart->count - 1
|
||||
]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'message' => trans('errors.try_again')
|
||||
])->setStatusCode(403);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->getCart();
|
||||
}
|
||||
|
||||
public function delete($cartId)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
Cart::find($cartId)->delete();
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'message' => trans('errors.try_again')
|
||||
])->setStatusCode(403);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->getCart();
|
||||
}
|
||||
|
||||
protected function updateOrCreateCart($user, $productId)
|
||||
{
|
||||
$cart = $user->cart()->where('product_id', $productId)->first();
|
||||
|
||||
try {
|
||||
if ($cart) {
|
||||
// Update the existing cart item
|
||||
Cart::where('id', $cart->id)->update([
|
||||
'count' => $cart->count + 1
|
||||
]);
|
||||
} else {
|
||||
// Create a new cart item
|
||||
$user->cart()->create([
|
||||
'product_id' => $productId,
|
||||
'count' => 1,
|
||||
]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return response()->json([
|
||||
'message' => trans('errors.try_again')
|
||||
])->setStatusCode(403);
|
||||
}
|
||||
}
|
||||
|
||||
protected function updateOrCreateCartByToken($localToken, $productId)
|
||||
{
|
||||
$cart = Cart::where('token', $localToken)
|
||||
->where('product_id', $productId)
|
||||
->first();
|
||||
|
||||
if ($cart) {
|
||||
// Update the existing cart item
|
||||
Cart::where('id', $cart->id)->update([
|
||||
'count' => $cart->count + 1
|
||||
]);
|
||||
} else {
|
||||
// Create a new cart item
|
||||
Cart::create([
|
||||
'token' => $localToken,
|
||||
'product_id' => $productId,
|
||||
'count' => 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function getLocalToken()
|
||||
{
|
||||
return request()->header('X-Application-Token');
|
||||
}
|
||||
|
||||
private function getBearerToken()
|
||||
{
|
||||
return request()->bearerToken();
|
||||
}
|
||||
}
|
||||
141
app/Http/Controllers/API/CategoryController.php
Executable file
141
app/Http/Controllers/API/CategoryController.php
Executable file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\BrandResource;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\ProductPaginationResource;
|
||||
use App\Models\Category;
|
||||
use App\Models\Currency;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$categories = Category::where('parent_id', null)->orderBy('position', 'asc')->get();
|
||||
return CategoryResource::collection($categories);
|
||||
}
|
||||
|
||||
public function products(Request $request, int $category_id)
|
||||
{
|
||||
$category = Category::find($category_id);
|
||||
|
||||
// check if category exists
|
||||
if (!$category) {
|
||||
return response()->json(['message' => 'Category not found'], 404);
|
||||
}
|
||||
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
// filter products by views (popular)
|
||||
// filter products by price (cheaper, expensive)
|
||||
// filter products by created_at (new)
|
||||
// filter products by brand_id
|
||||
// filter products by power (less_power, more_power)
|
||||
$products = $category->products()->where('published', true);
|
||||
|
||||
if ($request->has('sort_by')) {
|
||||
switch ($request->sort_by) {
|
||||
case 'popular':
|
||||
$products->orderBy('views', 'desc');
|
||||
break;
|
||||
case 'cheaper':
|
||||
// If price_discount is not null, order by price_discount; otherwise, order by price
|
||||
$products->orderByRaw('CASE WHEN price_discount IS NOT NULL THEN price_discount ELSE price END ASC');
|
||||
break;
|
||||
case 'expensive':
|
||||
// If price_discount is not null, order by price_discount; otherwise, order by price
|
||||
$products->orderByRaw('CASE WHEN price_discount IS NOT NULL THEN price_discount ELSE price END DESC');
|
||||
break;
|
||||
case 'new':
|
||||
$products->orderBy('created_at', 'desc');
|
||||
break;
|
||||
case 'less_power':
|
||||
$products->orderBy('power', 'asc');
|
||||
break;
|
||||
case 'more_power':
|
||||
$products->orderBy('power', 'desc');
|
||||
break;
|
||||
}
|
||||
}
|
||||
// elseif ($category->is_filter_power) {
|
||||
// $products->orderBy('power', 'asc');
|
||||
// }
|
||||
else { // default sort by price
|
||||
$products->orderBy('price', 'asc');
|
||||
}
|
||||
|
||||
|
||||
if ($request->has('brand_id')) {
|
||||
$products->where('brand_id', $request->brand_id);
|
||||
}
|
||||
|
||||
// price range filter
|
||||
if ($request->has('price_from') || $request->has('price_to')) {
|
||||
$products->where(function ($query) use ($request, $currency) {
|
||||
if ($request->has('price_from')) {
|
||||
$query->where(function ($subQuery) use ($request, $currency) {
|
||||
$subQuery->where(function ($q) use ($request, $currency) {
|
||||
$q->whereNotNull('price_discount')
|
||||
->where('price_discount', '>=', $request->price_from / $currency->dollar);
|
||||
})->orWhere(function ($q) use ($request, $currency) {
|
||||
$q->whereNull('price_discount')
|
||||
->where('price', '>=', $request->price_from / $currency->dollar);
|
||||
});
|
||||
});
|
||||
}
|
||||
if ($request->has('price_to')) {
|
||||
$query->where(function ($subQuery) use ($request, $currency) {
|
||||
$subQuery->where(function ($q) use ($request, $currency) {
|
||||
// dd($q->first());
|
||||
$q->whereNotNull('price_discount')
|
||||
->where('price_discount', '<=', $request->price_to / $currency->dollar);
|
||||
})->orWhere(function ($q) use ($request, $currency) {
|
||||
$q->whereNull('price_discount')
|
||||
->where('price', '<=', $request->price_to / $currency->dollar);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$products = $products->paginate($request->limit ?? 20);
|
||||
|
||||
return (new ProductPaginationResource($products))->response();
|
||||
}
|
||||
|
||||
public function filter($category_id, Request $request)
|
||||
{
|
||||
$category = Category::find($category_id);
|
||||
|
||||
$lang = $request->header('Accept-Language') ?? 'ru';
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'sort_by' => [
|
||||
[
|
||||
'name' => $lang == 'ru' ? 'Популярные' : 'Mashhur',
|
||||
'slug' => 'popular'
|
||||
],
|
||||
[
|
||||
'name' => $lang == 'ru' ? 'Сначало подешевле' : 'Avval arzonlari',
|
||||
'slug' => 'cheaper'
|
||||
],
|
||||
[
|
||||
'name' => $lang == 'ru' ? 'Сначало подороже' : 'Avval qimmatlari',
|
||||
'slug' => 'expensive'
|
||||
],
|
||||
[
|
||||
'name' => $lang == 'ru' ? 'Новые' : 'Yangi kelganlar',
|
||||
'slug' => 'new'
|
||||
]
|
||||
],
|
||||
'brands' => BrandResource::collection($category->brands)
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
app/Http/Controllers/API/CompanyController.php
Executable file
17
app/Http/Controllers/API/CompanyController.php
Executable file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CompanyResource;
|
||||
use App\Models\Company;
|
||||
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$company = Company::first();
|
||||
|
||||
return new CompanyResource($company);
|
||||
}
|
||||
}
|
||||
31
app/Http/Controllers/API/CompilationController.php
Executable file
31
app/Http/Controllers/API/CompilationController.php
Executable file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CompilationResource;
|
||||
use App\Http\Resources\ProductPaginationResource;
|
||||
use App\Models\Compilation;
|
||||
use App\Models\Currency;
|
||||
|
||||
class CompilationController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$compilations = Compilation::where('published', 1)->get();
|
||||
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
return CompilationResource::collection($compilations);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$compilation = Compilation::findOrFail($id);
|
||||
$products = $compilation->products()->paginate($request->limit ?? 10);
|
||||
|
||||
return (new ProductPaginationResource($products));
|
||||
}
|
||||
}
|
||||
65
app/Http/Controllers/API/FavoriteController.php
Executable file
65
app/Http/Controllers/API/FavoriteController.php
Executable file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\FavoritePaginationResource;
|
||||
use App\Models\Currency;
|
||||
use App\Models\Favorite;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class FavoriteController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
$user = $request->user();
|
||||
$favorites = Favorite::where('user_id', $user->id)->whereHas('product', function ($q) {
|
||||
$q->whereNull('child_id');
|
||||
})->paginate($request->limit ?? 10);
|
||||
|
||||
return (new FavoritePaginationResource($favorites))->response();
|
||||
}
|
||||
|
||||
public function store(Request $request, $product_id)
|
||||
{
|
||||
$product = Product::findOrFail($product_id);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
// check if product already in favorites
|
||||
$favorite = Favorite::where('user_id', $user->id)->where('product_id', $product_id)->first();
|
||||
|
||||
if (!$favorite) {
|
||||
Favorite::create([
|
||||
'user_id' => $user->id,
|
||||
'product_id' => $product_id
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Successfully added to favorites'
|
||||
])->setStatusCode(200);
|
||||
}
|
||||
|
||||
public function destroy(Request $request, $product_id)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$user = $request->user();
|
||||
|
||||
Favorite::where('user_id', $user->id)->where('product_id', $product_id)->delete();
|
||||
DB::commit();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['message' => $e->getMessage()])->setStatusCode(500);
|
||||
}
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
||||
21
app/Http/Controllers/API/FeedbackController.php
Executable file
21
app/Http/Controllers/API/FeedbackController.php
Executable file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Setting;
|
||||
|
||||
class FeedbackController extends Controller
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$setting = Setting::first();
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'phone' => $setting->phone['default'],
|
||||
'telegram_support' => isset($setting->socials['telegram']) ? $setting->socials['telegram'] : null,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
37
app/Http/Controllers/API/FirebaseController.php
Executable file
37
app/Http/Controllers/API/FirebaseController.php
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Firebase\Request;
|
||||
use App\Models\Firebase;
|
||||
|
||||
class FirebaseController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
// get bearer token
|
||||
$token = $request->bearerToken();
|
||||
if ($token) {
|
||||
$user = getAuthUser();
|
||||
if ($user) {
|
||||
Firebase::where('token', $token)->update([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
// store firebase token
|
||||
Firebase::create([
|
||||
'token' => $request->device_token,
|
||||
'device_id' => $request->device_id,
|
||||
'device_name' => $request->device_name,
|
||||
'device_type' => $request->device_type,
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => md5($request->device_token . $request->device_id)
|
||||
]);
|
||||
}
|
||||
}
|
||||
269
app/Http/Controllers/API/OrderController.php
Executable file
269
app/Http/Controllers/API/OrderController.php
Executable file
@@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Checkout\CheckoutRequest;
|
||||
use App\Http\Resources\OrderPaginationResource;
|
||||
use App\Http\Resources\OrderShowResource;
|
||||
use App\Http\Resources\ProductResource;
|
||||
use App\Http\Resources\StatusResource;
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use App\Models\DeliveryPrice;
|
||||
use App\Models\Order;
|
||||
use App\Models\Power;
|
||||
use App\Models\Product;
|
||||
use App\Models\Setting;
|
||||
use App\Services\API\OrderService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Services\API\ProductService;
|
||||
use App\Services\BotService;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
public function checkout(CheckoutRequest $request)
|
||||
{
|
||||
$currency = Currency::getCurrency()->dollar;
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$data = $request->validated();
|
||||
$orderService = new OrderService($data);
|
||||
$row = $orderService->createOrder($data);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'message' => $e->getMessage()
|
||||
], 403);
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
$url = null;
|
||||
|
||||
if (!in_array($row->order->payment_type, ['cash', 'bank'])) {
|
||||
if ($row->order->payment_type == 'click') {
|
||||
$merchant_id = env("CLICK_MERCHANT_ID");
|
||||
$service_id = env("CLICK_SERVICE_ID");
|
||||
$url = "https://my.click.uz/services/pay?service_id={$service_id}&merchant_id={$merchant_id}&amount={$row->billing->amount}&transaction_param={$row->billing->id}";
|
||||
} else {
|
||||
$amount = $row->billing->amount * 100;
|
||||
$payme_url = "https://checkout.paycom.uz/" . base64_encode("m=66faa5c44f9ee150b7ff81cf;ac.key={$row->billing->id};a={$amount}");
|
||||
$url = $payme_url; //route('payment.merchant', [$row->order->payment_type, $row->billing->id, $row->billing->amount]);
|
||||
}
|
||||
}
|
||||
|
||||
$order = Order::find($row->order->id);
|
||||
$contract = $order->contracts()->latest()->first();
|
||||
$products = $order->products()
|
||||
->with(['product' => function ($query) {
|
||||
$query->select('id', 'name', 'price'); // Faqat kerakli ustunlarni tanlash
|
||||
}])
|
||||
->get()
|
||||
->map(function ($orderProduct) use ($currency) {
|
||||
return [
|
||||
'name' => "📦 " . $orderProduct->product->name['uz'],
|
||||
'price' => ceiling($orderProduct->price * $currency, 100),
|
||||
'count' => $orderProduct->count,
|
||||
];
|
||||
});
|
||||
|
||||
// set lang
|
||||
app()->setLocale('uz');
|
||||
|
||||
$group_id = Setting::query()->first()->group_id;
|
||||
$user = Auth::user();
|
||||
$service = new BotService();
|
||||
$service->sendMessage([
|
||||
"group_id" => $group_id,
|
||||
"order_id" => $row->order->id,
|
||||
"order_url" => route("dashboard.orders.view", ["order" => $row->order->id]),
|
||||
'client_type' => trans('admin.contract-templates.' . $row->order->client_type),
|
||||
'delivery_type' => trans('admin.orders.type_delivery.' . $row->order->delivery_type),
|
||||
"products" => $products,
|
||||
"client" => $user->first_name . " " . $user->last_name,
|
||||
"phone" => $user->phone,
|
||||
'time' => now(),
|
||||
'summa' => ceiling($order->price_total * $currency, 100),
|
||||
]);
|
||||
|
||||
if ($row->order->client_type == 'physical') {
|
||||
$client = $row->order->full_name;
|
||||
} else {
|
||||
$client = $row->order->legalInfo->company_name;
|
||||
}
|
||||
|
||||
// download contract from s3
|
||||
// $path = $contract->path; // S3 file path
|
||||
// $localDirectory = storage_path('downloads');
|
||||
// $localPath = storage_path('downloads/' . basename($contract->path));
|
||||
|
||||
// // Ensure 'downloads' directory exists
|
||||
// if (!is_dir($localDirectory)) {
|
||||
// mkdir($localDirectory, 0755, true);
|
||||
// }
|
||||
|
||||
// // Retrieve the file from S3 and save locally
|
||||
// $content = Storage::disk('s3')->get($path);
|
||||
// file_put_contents($localPath, $content);
|
||||
|
||||
// $this->sendFileWithData($group_id, $row, $client, $localPath);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
"id" => $row->order->id,
|
||||
"payment_status" => new StatusResource($order->getPaymentStatus),
|
||||
// "billing_id" => !in_array($row->order->payment_type, ['cash', 'bank']) ? $row->billing->id : null,
|
||||
"payment_type" => $row->order->payment_type,
|
||||
"pay_url" => $url,
|
||||
// "pay_url" => !in_array($row->order->payment_type, ['cash', 'bank']) ? "https://checkout.payme.uz" : null,
|
||||
"contract_url" => $contract?->getPath() ?? null
|
||||
]
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function preview(Request $request)
|
||||
{
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
$products = $this->getProducts($request);
|
||||
|
||||
$countCollection = collect($request->products);
|
||||
$is_install = false;
|
||||
|
||||
foreach ($products as $product) {
|
||||
// Find the corresponding count for the current product
|
||||
if ((new ProductService($product))->isInstall()) {
|
||||
$is_install = true; // Set the flag to true if any product is installed
|
||||
}
|
||||
$countItem = $countCollection->firstWhere('id', $product->id);
|
||||
|
||||
if ($countItem) {
|
||||
// Update the product's count attribute (assuming you have a 'count' attribute in your Product model)
|
||||
$product->count = $countItem['count'];
|
||||
}
|
||||
}
|
||||
|
||||
$power = Power::where('power', '>=', $products->sum('power'))->first();
|
||||
if (empty($power)) {
|
||||
$power_id = 0;
|
||||
} else {
|
||||
$power_id = $power->id;
|
||||
}
|
||||
|
||||
// delivery price
|
||||
if (!empty($request->city_id)) {
|
||||
$city = City::where('id', $request->city_id)->first();
|
||||
$delivery_price = DeliveryPrice::where('power_id', $power_id)->where('region_id', $city->region_id)->first();
|
||||
} else {
|
||||
$delivery_price = 0;
|
||||
}
|
||||
|
||||
// installation price
|
||||
$settings = Setting::query()->first();
|
||||
$power = $products->sum('power') / 1000;
|
||||
|
||||
if ($request->type == 'ready_solutions') {
|
||||
$installation_price = $power * ($settings->master_price / $currency->dollar);
|
||||
} else {
|
||||
$installation_price = 0;
|
||||
}
|
||||
|
||||
// product price
|
||||
$product_price = collect($products)->sum(function ($product) {
|
||||
$count = collect(request()->products)->firstWhere('id', $product->id)['count'] ?? 0;
|
||||
|
||||
return $product->finalPrice * $count;
|
||||
});
|
||||
|
||||
if (!empty($delivery_price)) {
|
||||
$delivery_price = $delivery_price->price;
|
||||
} else {
|
||||
$delivery_price = 0;
|
||||
}
|
||||
|
||||
$total = $product_price + ($delivery_price / $currency->dollar) + $installation_price;
|
||||
return response()->json([
|
||||
'data' => [
|
||||
"is_install" => $is_install,
|
||||
'products' => ProductResource::collection($products),
|
||||
'delivery_price' => ceiling($delivery_price, 100),
|
||||
'installation_price' => ceiling($installation_price * $currency->dollar, 100),
|
||||
'total_price' => ceiling($total * $currency->dollar, 100),
|
||||
'product_price' => ceiling($product_price * $currency->dollar, 100),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
private function getProducts(Request $request)
|
||||
{
|
||||
$products_cart = collect($request->only('products'))->flatten(1);
|
||||
$product_ids = $products_cart->pluck('id');
|
||||
$products = Product::whereIn('id', $product_ids)->get();
|
||||
|
||||
$products = $products->map(function ($product) use ($products_cart) {
|
||||
$product->power = $product->power * $products_cart->filter(function ($cart) use ($product) {
|
||||
return $cart['id'] == $product->id;
|
||||
})->value('count');
|
||||
return $product;
|
||||
});
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
|
||||
public function list(Request $request)
|
||||
{
|
||||
$user = getAuthUser();
|
||||
$orders = $user->orders()->orderBy('id', 'desc')->with('getCurrency')->paginate($request->limit ?? 10);
|
||||
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
return (new OrderPaginationResource($orders))->response();
|
||||
}
|
||||
|
||||
public function show($order_id)
|
||||
{
|
||||
$user = getAuthUser();
|
||||
$order = $user->orders()->findOrFail($order_id);
|
||||
|
||||
$currency = $order->getCurrency;
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
// chech if user has this order
|
||||
if (!$order) {
|
||||
return response()->json([
|
||||
'message' => 'Order not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return new OrderShowResource($order);
|
||||
}
|
||||
|
||||
public function checkPaymentStatus($order_id)
|
||||
{
|
||||
// check if this order belongs to the user
|
||||
$user = request()->user();
|
||||
|
||||
$order = $user->orders()->find($order_id);
|
||||
|
||||
// chech if user has this order
|
||||
if (!$order) {
|
||||
return response()->json([
|
||||
'message' => 'Order not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'payment_status' => $order->payment_status //new StatusResource($order->getPaymentStatus),
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
app/Http/Controllers/API/PageController.php
Executable file
42
app/Http/Controllers/API/PageController.php
Executable file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\PageResource;
|
||||
use App\Http\Resources\PowerResource;
|
||||
use App\Http\Resources\RegionWithCityResource;
|
||||
use App\Models\Page;
|
||||
use App\Models\Power;
|
||||
use App\Models\Region;
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
public function policy()
|
||||
{
|
||||
$policy = Page::where('slug', 'policy')->first();
|
||||
|
||||
return new PageResource($policy);
|
||||
}
|
||||
|
||||
public function about()
|
||||
{
|
||||
$about = Page::where('slug', 'about')->first();
|
||||
|
||||
return new PageResource($about);
|
||||
}
|
||||
|
||||
public function powers()
|
||||
{
|
||||
$powers = Power::all();
|
||||
|
||||
return PowerResource::collection($powers);
|
||||
}
|
||||
|
||||
public function regions()
|
||||
{
|
||||
$regions = Region::all();
|
||||
|
||||
return RegionWithCityResource::collection($regions);
|
||||
}
|
||||
}
|
||||
44
app/Http/Controllers/API/PartnerController.php
Executable file
44
app/Http/Controllers/API/PartnerController.php
Executable file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Partner\PartnerRequest;
|
||||
use App\Http\Resources\PartnerRequestResource;
|
||||
use App\Http\Resources\PartnerResource;
|
||||
use App\Http\Resources\PartnerShowResource;
|
||||
use App\Models\Partner;
|
||||
use App\Models\PartnerRequest as ModelsPartnerRequest;
|
||||
|
||||
class PartnerController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$partners = Partner::query()->orderBy("position")->get();
|
||||
|
||||
return PartnerResource::collection($partners);
|
||||
}
|
||||
|
||||
public function show($partner_id)
|
||||
{
|
||||
$partner = Partner::findOrFail($partner_id);
|
||||
|
||||
return new PartnerShowResource($partner);
|
||||
}
|
||||
|
||||
public function store(PartnerRequest $request)
|
||||
{
|
||||
$user = getAuthUser();
|
||||
$partner = ModelsPartnerRequest::create([
|
||||
'user_id' => $user->id,
|
||||
'partner_id' => $request->partner_id,
|
||||
'city_id' => $request->city_id,
|
||||
'price' => $request->price,
|
||||
'comment' => $request->comment,
|
||||
'phone' => $request->phone,
|
||||
'full_name' => $request->full_name,
|
||||
]);
|
||||
|
||||
return new PartnerRequestResource($partner);
|
||||
}
|
||||
}
|
||||
25
app/Http/Controllers/API/PaymentSystemController.php
Executable file
25
app/Http/Controllers/API/PaymentSystemController.php
Executable file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\PaymentSystemResource;
|
||||
use App\Models\PaymentSystemModel;
|
||||
|
||||
class PaymentSystemController extends Controller
|
||||
{
|
||||
public function legal($legal)
|
||||
{
|
||||
if ($legal === 'legal') {
|
||||
$systems = PaymentSystemModel::with('items')->whereIn('slug', ['money_transfer', 'cash'])->get();
|
||||
} else {
|
||||
$systems = PaymentSystemModel::with('items')->whereIn('slug', ['payment_systems', 'cash'])->get();
|
||||
}
|
||||
|
||||
return [
|
||||
'data' => [
|
||||
'payment_types' => PaymentSystemResource::collection($systems)
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
64
app/Http/Controllers/API/ProductController.php
Executable file
64
app/Http/Controllers/API/ProductController.php
Executable file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ProductDetailResource;
|
||||
use App\Http\Resources\ProductPaginationResource;
|
||||
use App\Models\Currency;
|
||||
use App\Models\Product;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function show($product_id)
|
||||
{
|
||||
$product = Product::where('published', true)->find($product_id);
|
||||
if (!$product) {
|
||||
return response()->json(['message' => 'Product not found'], 404);
|
||||
}
|
||||
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
// increment views
|
||||
$product->increment('views');
|
||||
|
||||
return ['data' => new ProductDetailResource($product)];
|
||||
}
|
||||
|
||||
public function search()
|
||||
{
|
||||
if (request()->has('query') && request('query') !== '' && request('query') !== null) {
|
||||
$products = Product::query()->where('published', true);
|
||||
|
||||
$products->where('name->uz', 'ILIKE', '%' . request('query') . '%')
|
||||
->orWhere('name->ru', 'ILIKE', '%' . request('query') . '%');
|
||||
|
||||
return (new ProductPaginationResource($products->paginate($request->limit ?? 10)))->response();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
"pagination" => [
|
||||
"current" => 1,
|
||||
"previous" => null,
|
||||
"next" => null,
|
||||
"total" => 0,
|
||||
"perPage" => 10,
|
||||
"totalItems" => 0
|
||||
],
|
||||
'data' => []
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function productsByBrand($brand_id)
|
||||
{
|
||||
$currency = Currency::latest()->first();
|
||||
// add currency to cache
|
||||
cache()->put('currency', $currency, now()->addMinutes(60));
|
||||
|
||||
$products = Product::where('brand_id', $brand_id)->where('published', true)->paginate(10);
|
||||
|
||||
return (new ProductPaginationResource($products))->response();
|
||||
}
|
||||
}
|
||||
38
app/Http/Controllers/API/RequestsController.php
Executable file
38
app/Http/Controllers/API/RequestsController.php
Executable file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\RequestPaginationResource;
|
||||
use App\Http\Resources\ServiceRequestResource;
|
||||
use App\Models\ServiceRequest as ModelsServiceRequest;
|
||||
use App\Models\PartnerRequest as ModelsPartnerRequest;
|
||||
class RequestsController extends Controller
|
||||
{
|
||||
public function getServiceRequests()
|
||||
{
|
||||
$user = request()->user();
|
||||
|
||||
$requests = ModelsServiceRequest::where('user_id', $user->id)->orderBy('id', 'desc')->paginate(10);
|
||||
|
||||
return (new RequestPaginationResource($requests))->response();
|
||||
}
|
||||
|
||||
public function getPartnerRequests()
|
||||
{
|
||||
$user = request()->user();
|
||||
|
||||
$requests = ModelsPartnerRequest::where('user_id', $user->id)->orderBy('id', 'desc')->paginate(10);
|
||||
|
||||
|
||||
return (new RequestPaginationResource($requests))->response();
|
||||
}
|
||||
|
||||
public function show($service_request_id)
|
||||
{
|
||||
$user = request()->user();
|
||||
$request = ModelsServiceRequest::where('user_id', $user->id)->where('id', $service_request_id)->firstOrFail();
|
||||
|
||||
return new ServiceRequestResource($request);
|
||||
}
|
||||
}
|
||||
52
app/Http/Controllers/API/ServiceController.php
Executable file
52
app/Http/Controllers/API/ServiceController.php
Executable file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Service\ServiceRequest;
|
||||
use App\Http\Resources\ProblemResource;
|
||||
use App\Http\Resources\ServiceRequestResource;
|
||||
use App\Http\Resources\ServiceResource;
|
||||
use App\Models\Problem;
|
||||
use App\Models\Service;
|
||||
use App\Models\ServiceRequest as ModelsServiceRequest;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$services = Service::orderBy('position', 'asc')->get();
|
||||
return ServiceResource::collection($services);
|
||||
}
|
||||
|
||||
public function store(ServiceRequest $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
$serviceRequest = ModelsServiceRequest::create([
|
||||
'user_id' => $user->id,
|
||||
'service_id' => $request->service_id,
|
||||
'power_id' => $request->power_id,
|
||||
'city_id' => $request->city_id,
|
||||
'phone' => $request->phone,
|
||||
'comment' => $request->comment,
|
||||
'full_name' => $request->full_name,
|
||||
'status' => 'pending',
|
||||
'problem_id' => $request->problem_id
|
||||
]);
|
||||
|
||||
return new ServiceRequestResource($serviceRequest);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$service = Service::findOrFail($id);
|
||||
return new ServiceResource($service);
|
||||
}
|
||||
|
||||
public function getProblems()
|
||||
{
|
||||
$problems = Problem::all();
|
||||
return ProblemResource::collection($problems);
|
||||
}
|
||||
}
|
||||
44
app/Http/Controllers/API/SliderController.php
Executable file
44
app/Http/Controllers/API/SliderController.php
Executable file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\SliderResource;
|
||||
use App\Models\Post;
|
||||
use App\Models\Slider;
|
||||
|
||||
class SliderController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$lang = request()->header('Accept-Language') ?? 'ru';
|
||||
$sliders = Post::where('language', $lang)->orderBy('position')->get();
|
||||
|
||||
return SliderResource::collection($sliders);
|
||||
}
|
||||
|
||||
public function show($slider_id)
|
||||
{
|
||||
$slider = Post::find($slider_id);
|
||||
|
||||
if (!$slider) {
|
||||
return response()->json([
|
||||
'message' => 'Slider not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$slider->views += 1;
|
||||
$slider->save();
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $slider->id,
|
||||
'name' => $slider->name,
|
||||
'image' => $slider->getImage(),
|
||||
'description' => $slider->content,
|
||||
'views' => $slider->views,
|
||||
'created_at' => $slider->created_at->format('Y-m-d H:i:s'),
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
52
app/Http/Controllers/API/SupportController.php
Normal file
52
app/Http/Controllers/API/SupportController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\BotService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SupportController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$service = new BotService();
|
||||
$message = "Support: \n\nName: " . $request->input("name") . "\n" . "Phone: " . $request->input("phone") . "\n";
|
||||
$service->sendSupportMessage($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
39
app/Http/Controllers/API/UsefulInfoController.php
Executable file
39
app/Http/Controllers/API/UsefulInfoController.php
Executable file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\UsefulInfoItemResource;
|
||||
use App\Http\Resources\UsefulInfoResource;
|
||||
use App\Models\UsefulInfo;
|
||||
|
||||
class UsefulInfoController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$usefulInfos = UsefulInfo::orderBy('position')->get();
|
||||
|
||||
return UsefulInfoResource::collection($usefulInfos);
|
||||
}
|
||||
|
||||
public function items($id)
|
||||
{
|
||||
$usefulInfo = UsefulInfo::find($id);
|
||||
|
||||
// chech if useful info not found
|
||||
if (!$usefulInfo) {
|
||||
return response()->json([
|
||||
'message' => 'Useful info not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return UsefulInfoItemResource::collection($usefulInfo->items);
|
||||
}
|
||||
|
||||
public function itemShow($id, $itemId)
|
||||
{
|
||||
$usefulInfo = UsefulInfo::find($id);
|
||||
|
||||
return new UsefulInfoItemResource($usefulInfo->items->find($itemId));
|
||||
}
|
||||
}
|
||||
104
app/Http/Controllers/API/UserController.php
Executable file
104
app/Http/Controllers/API/UserController.php
Executable file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function me()
|
||||
{
|
||||
$user = getAuthUser();
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'first_name' => $user->first_name,
|
||||
'last_name' => $user->last_name,
|
||||
'middle_name' => $user->middle_name,
|
||||
'language' => $user->language,
|
||||
'phone' => $user->phone,
|
||||
'gender' => $user->gender == 1 ? true : false
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function changeLang(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'language' => 'required|string|in:ru,uz',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'error' => 'Validation error',
|
||||
'details' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
$user = getAuthUser();
|
||||
$user->update($request->only('language'));
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'first_name' => $user->first_name,
|
||||
'last_name' => $user->last_name,
|
||||
'middle_name' => $user->middle_name,
|
||||
'language' => $user->language,
|
||||
'phone' => $user->phone,
|
||||
'gender' => $user->gender
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// validate
|
||||
$request->validate([
|
||||
'first_name' => 'required|string',
|
||||
'last_name' => 'required|string',
|
||||
'middle_name' => 'nullable|string',
|
||||
'gender' => 'required|boolean',
|
||||
]);
|
||||
|
||||
getAuthUser()->update($request->only('first_name', 'last_name', 'middle_name', 'gender'));
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => getAuthUser()->id,
|
||||
'first_name' => getAuthUser()->first_name,
|
||||
'last_name' => getAuthUser()->last_name,
|
||||
'middle_name' => getAuthUser()->middle_name,
|
||||
'language' => getAuthUser()->language,
|
||||
'phone' => getAuthUser()->phone,
|
||||
'gender' => getAuthUser()->gender
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$user = getAuthUser();
|
||||
// delte name
|
||||
$user->first_name = null;
|
||||
$user->last_name = null;
|
||||
$user->middle_name = null;
|
||||
$user->avatar = null;
|
||||
$user->email = null;
|
||||
$user->verify_code = null;
|
||||
$user->birth_day = null;
|
||||
$user->postal_address = null;
|
||||
$user->category_id = null;
|
||||
$user->notification = 0;
|
||||
$user->save();
|
||||
// delete
|
||||
$user->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'User deleted'
|
||||
], 204);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user