sarga/packages/Sarga/API/Http/Controllers/IntegrationController.php

77 lines
2.3 KiB
PHP
Raw Normal View History

2021-12-06 11:52:58 +00:00
<?php
namespace Sarga\API\Http\Controllers;
2021-12-22 15:53:36 +00:00
2022-01-20 07:14:24 +00:00
use Illuminate\Support\Facades\Log;
2021-12-06 11:52:58 +00:00
use Illuminate\Support\Facades\Storage;
2021-12-18 20:20:59 +00:00
use Illuminate\Support\Facades\Validator;
2021-12-06 11:52:58 +00:00
use Webkul\API\Http\Controllers\Shop\Controller;
use Webkul\Core\Contracts\Validations\Slug;
use Sarga\Shop\Repositories\ProductRepository;
2022-01-19 08:32:04 +00:00
use Webkul\Marketplace\Repositories\SellerRepository;
2021-12-06 11:52:58 +00:00
2021-12-10 14:30:26 +00:00
class IntegrationController extends Controller
2021-12-06 11:52:58 +00:00
{
2021-12-18 20:20:59 +00:00
protected $productRepository;
2022-01-19 08:32:04 +00:00
protected $sellerRepository;
2021-12-18 20:20:59 +00:00
2022-01-19 08:32:04 +00:00
public function __construct(ProductRepository $productRepository, SellerRepository $sellerRepository)
2021-12-18 20:20:59 +00:00
{
2022-01-19 08:32:04 +00:00
$this->sellerRepository = $sellerRepository;
2021-12-18 20:20:59 +00:00
$this->productRepository = $productRepository;
}
2021-12-06 11:52:58 +00:00
public function store(){
if(!request()->has('product')){
return response()->json(['status' =>false, 'message' => 'bad request'],405);
}
$product = json_decode(request('product'),true);
$this->validate($product, [
'sku' => ['required', 'unique:products,sku', new Slug],
]);
// $product = $this->productRepository->create(request()-
return $product;
}
2021-12-18 20:20:59 +00:00
public function create(){
2021-12-22 15:53:36 +00:00
try {
$data = json_decode(request()->getContent(),true);
}
catch (\Exception $e){
2022-01-20 12:13:30 +00:00
Log::error($e);
2021-12-22 15:53:36 +00:00
return response()->json(['errors'=>$e->getMessage()],400);
}
2021-12-18 20:20:59 +00:00
$validation = Validator::make($data, [
2022-01-20 12:25:29 +00:00
'categories' => 'required',
2022-01-20 13:36:41 +00:00
// 'sku' => ['required', 'unique:products,sku', new Slug],
2021-12-18 20:20:59 +00:00
'images' => 'required',
'name' => 'required',
'url_key'=> 'required',
'price' => 'required',
2022-01-19 08:32:04 +00:00
'vendor' => 'required'
2021-12-18 20:20:59 +00:00
]);
if ($validation->fails()) {
2022-01-25 07:25:49 +00:00
2021-12-22 15:53:36 +00:00
return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
2021-12-18 20:20:59 +00:00
}
2022-01-20 13:36:41 +00:00
if($product = $this->productRepository->findOneByField('sku',$data['sku'])){
return response()->json(['success'=>true,'product_id' => $product->id]);
}
elseif($product = $this->productRepository->create($data)){
2022-01-26 15:28:48 +00:00
2022-01-19 08:32:04 +00:00
return response()->json(['success'=>true,'product_id' => $product->id]);
2022-01-12 07:35:04 +00:00
}else{
2022-01-20 12:11:09 +00:00
Log::info('creat product fails fails');
2022-01-20 12:13:30 +00:00
return response()->json(['success'=>false],400);
2022-01-12 07:35:04 +00:00
}
2021-12-18 20:20:59 +00:00
}
2021-12-06 11:52:58 +00:00
}