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

96 lines
3.0 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
2022-04-12 06:58:43 +00:00
public function __construct(protected ProductRepository $productRepository,
2022-04-12 08:50:55 +00:00
protected SellerRepository $sellerRepository){}
2021-12-18 20:20:59 +00:00
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);
2022-04-14 06:21:40 +00:00
if(!$data){
return response()->json(['message'=>'data not found'],405);
}
2021-12-22 15:53:36 +00:00
}
catch (\Exception $e){
2022-04-07 09:53:59 +00:00
Log::error($e->getMessage());
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-04-07 10:36:31 +00:00
'vendor' => 'required',
'weight' => 'required'
2021-12-18 20:20:59 +00:00
]);
if ($validation->fails()) {
2022-04-07 10:42:06 +00:00
Log::info($data);
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-04-12 06:58:43 +00:00
if($product = $this->productRepository->findOneByField('sku',$data['product_group_id']))
{//product_group_id
2022-01-20 13:36:41 +00:00
return response()->json(['success'=>true,'product_id' => $product->id]);
}
2022-04-12 08:50:55 +00:00
elseif($product = $this->productRepository->createProduct($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-04-05 11:46:12 +00:00
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
}
2022-04-10 12:00:58 +00:00
public function update(){
try {
$data = json_decode(request()->getContent(),true);
}
catch (\Exception $e){
2022-05-20 10:01:34 +00:00
Log::info($e->getMessage());
2022-04-10 12:00:58 +00:00
return response()->json(['errors'=>$e->getMessage()],400);
}
2022-04-14 06:29:09 +00:00
if(! $product = $this->productRepository->findOneByField('sku',$data['product_group_id'])){
2022-04-10 12:00:58 +00:00
return response()->json(['success'=> false,'message' => 'product not found'],400);
}
2022-04-12 08:50:55 +00:00
if($this->productRepository->updateProduct($product,$data)){
return response()->json(['success'=>true,'product_id' => $product->id]);
}
2022-04-10 12:00:58 +00:00
}
2021-12-06 11:52:58 +00:00
}