325 lines
10 KiB
PHP
325 lines
10 KiB
PHP
<?php
|
|
|
|
namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
use Cms\Classes\Controller;
|
|
use DB;
|
|
use Config;
|
|
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use RainLab\Blog\Models\Post;
|
|
use TPS\Birzha\Classes\BlogPostResource;
|
|
use TPS\Birzha\Models\SliderApp;
|
|
use TPS\Birzha\Models\UserSliders;
|
|
use TPS\Birzha\Models\Product;
|
|
use TPS\Birzha\Models\City;
|
|
use TPS\Birzha\Models\Favourites;
|
|
use TPS\Birzha\Models\Notification;
|
|
use TPS\Birzha\Models\Comment;
|
|
use RainLab\User\Models\User;
|
|
|
|
class BlogPostsApiController extends Controller
|
|
{
|
|
protected $Post;
|
|
|
|
protected $helpers;
|
|
|
|
public function __construct(Post $Post, Favourites $Favourites, Comment $Comment, Helpers $helpers)
|
|
{
|
|
parent::__construct();
|
|
$this->Post = $Post;
|
|
$this->Favourites = $Favourites;
|
|
$this->Comment = $Comment;
|
|
$this->helpers = $helpers;
|
|
}
|
|
|
|
//posts list
|
|
public function index(Request $request)
|
|
{
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'locale' => 'required|in:ru,en,tm',
|
|
'per_page' => 'numeric',
|
|
'sort_order' => 'in:asc,desc'
|
|
]);
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
|
|
return response()->json(
|
|
BlogPostResource::collection($this->Post::with(['featured_images', 'translations:locale,model_id,attribute_data',])
|
|
->orderBy('published_at', $data['sort_order'] ?? 'desc')
|
|
->paginate($data['per_page'] ?? 7))->response()->getData(), 200);
|
|
}
|
|
|
|
public function getNotifications(){
|
|
|
|
$data = Notification::with([
|
|
'translations:locale,model_id,attribute_data'
|
|
])->paginate(9);
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'ok', [$data]);
|
|
|
|
}
|
|
|
|
|
|
public function getAccountSliders($id)
|
|
{
|
|
$data = UserSliders::where('user_id', $id)->paginate(9);
|
|
|
|
if($data) {
|
|
$data->each(function ($item, $key) {
|
|
$item->img = 'http://78.111.88.8:9086' . \Config::get('cms.storage.media.path') . $item->img;
|
|
});
|
|
}
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
|
|
public function createComment(Request $request)
|
|
{
|
|
$currentUser = \JWTAuth::parseToken()->authenticate();
|
|
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'product_id' => 'required',
|
|
'comment' => 'required',
|
|
'rating' => 'required|numeric'
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
|
|
|
|
$comment = new $this->Comment;
|
|
//dd($favourite);
|
|
$comment->user_id = $currentUser->id;
|
|
$comment->product_id = (int)$data['product_id'];
|
|
$comment->comment = $data['comment'];
|
|
$comment->rating = $data['rating'];
|
|
|
|
$comment->save();
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(201, 'ok', ['comment' => $comment]);
|
|
|
|
}
|
|
|
|
|
|
public function getComment(Request $request)
|
|
{
|
|
//$data = $request->all();
|
|
// $validator = Validator::make($data, [
|
|
// 'product_id' => 'required',
|
|
// ]);
|
|
|
|
// if($validator->fails()) {
|
|
// return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
// }
|
|
|
|
$currentUser = \JWTAuth::parseToken()->authenticate();
|
|
$comment = $this->Comment::where('user_id', $currentUser->id)->paginate(9);
|
|
//dd($favourite);
|
|
|
|
return response()->json($comment, 200);
|
|
|
|
}
|
|
|
|
public function getProductComment(Request $request)
|
|
{
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'product_id' => 'required',
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
|
|
$comment = $this->Comment::where('product_id', $data["product_id"])->where('is_approve', 1)->paginate(15);
|
|
//dd($favourite);
|
|
|
|
return response()->json($comment, 200);
|
|
|
|
}
|
|
|
|
|
|
public function createFav(Request $request)
|
|
{
|
|
$currentUser = \JWTAuth::parseToken()->authenticate();
|
|
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'product_id' => 'required'
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
|
|
|
|
$favourite = new $this->Favourites;
|
|
//dd($favourite);
|
|
$favourite->user_id = $currentUser->id;
|
|
$favourite->product_id = (int)$data['product_id'];
|
|
|
|
$favourite->save();
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(201, 'ok', ['favourite' => $favourite]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public function getFav(Request $request)
|
|
{
|
|
$currentUser = \JWTAuth::parseToken()->authenticate();
|
|
|
|
|
|
$favourite = $this->Favourites::where('user_id', $currentUser->id)
|
|
->with(['product' => function($q){
|
|
$q->approved()->with(['images', 'place', 'files', 'translations:locale,model_id,attribute_data']);
|
|
}])->paginate(9);
|
|
//dd($favourite);
|
|
|
|
return response()->json($favourite, 200);
|
|
|
|
}
|
|
|
|
public function deleteFav($id){
|
|
|
|
$favourite = $this->Favourites->find($id);
|
|
$favourite->delete();
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', ['message' => 'Data has been deleted successfully']);
|
|
}
|
|
|
|
|
|
public function getAccountProducts($id)
|
|
{
|
|
$data = Product::where('vendor_id', $id)
|
|
->with('categories:id,name')
|
|
->with([
|
|
'translations:locale,model_id,attribute_data',
|
|
'images:attachment_id,attachment_type,disk_name,file_name',
|
|
])
|
|
->approved()
|
|
->paginate(9);
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
public function getPlaces()
|
|
{
|
|
$data = City::with([
|
|
'translations:locale,model_id,attribute_data',
|
|
])
|
|
->paginate(100);
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
|
|
public function getAccountDatas($id)
|
|
{
|
|
//$data = User::find($id)->first;
|
|
|
|
|
|
$data = User::where('id', $id)->select('id', 'name', 'email', 'username', 'type', 'logo', 'shop_title', 'slogan', 'work_time', 'description', 'map', 'banner', 'is_instagram')
|
|
->with(['categories' => function($q){
|
|
$q->with('translations:locale,model_id,attribute_data');
|
|
}])
|
|
->with('sliders')
|
|
->first();
|
|
|
|
$data->pathUrl = 'http://78.111.88.8:9086' . \Config::get('cms.storage.media.path');
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAccounts(Request $request)
|
|
{
|
|
$data = $request->all();
|
|
|
|
$dataAccounts = User::select('id', 'name', 'email', 'username', 'type', 'logo', 'shop_title', 'slogan', 'work_time', 'description', 'map', 'banner', 'is_instagram')
|
|
->with('categories:id,name,slug,icon')
|
|
->paginate(9);
|
|
|
|
if($data && $data["type"] == "home"){
|
|
|
|
$dataAccounts = User::where('is_featured', 1)
|
|
->select('id', 'name', 'email', 'username', 'type', 'logo', 'shop_title', 'slogan', 'work_time', 'description', 'map', 'banner', 'is_instagram')
|
|
->with('categories:id,name,slug,icon')
|
|
->paginate(9);
|
|
|
|
}elseif($data && $data["type"] == "category"){
|
|
|
|
$dataAccounts = User::where('is_category', 1)
|
|
->select('id', 'name', 'email', 'username', 'type', 'logo', 'shop_title', 'slogan', 'work_time', 'description', 'map', 'is_instagram')
|
|
->with('categories:id,name,slug,icon')
|
|
->paginate(9);
|
|
|
|
}
|
|
//$dataSlider["img"] = $path.$dataSlider["img"];
|
|
|
|
if($dataAccounts){
|
|
$dataAccounts->each(function ($item, $key) {
|
|
$item->logo = 'http://78.111.88.8:9086'.Config::get('cms.storage.media.path').$item->logo;
|
|
$item->banner = 'http://78.111.88.8:9086'.Config::get('cms.storage.media.path').$item->banner;
|
|
});
|
|
}
|
|
|
|
return response()->json($dataAccounts, 200);
|
|
}
|
|
|
|
|
|
public function getSliders(Request $request)
|
|
{
|
|
$path = Config::get('cms.storage.media.path');
|
|
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
//'locale' => 'required|in:ru,en,tm',
|
|
'type' => 'required'
|
|
]);
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
$dataSlider = SliderApp::where('type', $data['type'])->orderBy('order', 'desc')->select('id', 'img', 'type', 'order')->paginate(9);
|
|
|
|
//$dataSlider["img"] = $path.$dataSlider["img"];
|
|
|
|
if($dataSlider){
|
|
$dataSlider->each(function ($item, $key) {
|
|
$item->img = 'http://78.111.88.8:9086'.Config::get('cms.storage.media.path').$item->img;
|
|
});
|
|
}
|
|
|
|
return response()->json($dataSlider, 200);
|
|
}
|
|
|
|
//posts item
|
|
public function show($id, Request $request)
|
|
{
|
|
$data = $request->all();
|
|
$validator = Validator::make($data, [
|
|
'locale' => 'required|in:ru,en,tm',
|
|
]);
|
|
if($validator->fails()) {
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
|
}
|
|
|
|
if(!$post = $this->Post::find($id)) {
|
|
return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => "Resource id = {$id} could not be found"]);
|
|
} else {
|
|
return new BlogPostResource($post);
|
|
}
|
|
}
|
|
}
|