2021-12-06 11:52:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Sarga\API\Http\Controllers;
|
2021-12-22 15:53:36 +00:00
|
|
|
|
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;
|
2021-12-22 15:53:36 +00:00
|
|
|
use Sarga\API\Repositories\ProductRepository;
|
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;
|
|
|
|
|
|
2021-12-22 15:53:36 +00:00
|
|
|
public function __construct(ProductRepository $productRepository)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function bulk_upload(){
|
|
|
|
|
|
2021-12-10 14:30:26 +00:00
|
|
|
$products = json_decode(request()->getContent());
|
2021-12-06 12:51:25 +00:00
|
|
|
Storage::put('scrap/products' . time() . '.txt', request()->getContent());
|
2021-12-06 11:52:58 +00:00
|
|
|
|
2021-12-10 14:30:26 +00:00
|
|
|
foreach ($products as $product){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 11:52:58 +00:00
|
|
|
}
|
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){
|
|
|
|
|
return response()->json(['errors'=>$e->getMessage()],400);
|
|
|
|
|
}
|
2021-12-18 20:20:59 +00:00
|
|
|
|
|
|
|
|
$validation = Validator::make($data, [
|
2021-12-22 15:53:36 +00:00
|
|
|
// 'category' => 'required',
|
2022-01-10 13:45:21 +00:00
|
|
|
'product_code' => ['required', 'unique:products,sku', new Slug],
|
2021-12-18 20:20:59 +00:00
|
|
|
'images' => 'required',
|
|
|
|
|
'name' => 'required',
|
|
|
|
|
'url_key'=> 'required',
|
|
|
|
|
'price' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($validation->fails()) {
|
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-10 13:45:21 +00:00
|
|
|
//todo return product id
|
2021-12-22 15:53:36 +00:00
|
|
|
return $this->productRepository->create($data);
|
2021-12-18 20:20:59 +00:00
|
|
|
|
2022-01-10 13:45:21 +00:00
|
|
|
|
2021-12-18 20:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-06 11:52:58 +00:00
|
|
|
}
|