Browse Source

增加UserFavController

dev
李可松 4 years ago
parent
commit
83bcdc22b4
  1. 50
      app/Http/Controllers/Api/UserFavController.php

50
app/Http/Controllers/Api/UserFavController.php

@ -0,0 +1,50 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\AgentProduct;
use App\Models\UserFav;
class UserFavController extends Controller
{
public function index()
{
$page = request()->input('page');
if ($page && !ctype_digit($page)) {
return $this->error('页码错误');
}
$list = UserFav::query()->with([
'agentProduct' => fn($query) => $query->select('id', 'price', 'original_price'),
'product' => fn($query) => $query->select('id', 'title', 'pictures'),
])
->where('user_id', $this->user_id)
->select('id', 'agent_product_id', 'product_id', 'created_at')
->orderBy('id', 'DESC')
->simplePaginate(15);
return $this->success($list);
}
public function create()
{
$id = request()->input('id');
if (!$id || !ctype_digit($id)) {
return $this->error('无效的ID');
}
$agent_product = AgentProduct::find($id);
if (!$agent_product) {
return $this->error('产品不存在或已被删除');
}
//如果不存在则创建
UserFav::query()->firstOrCreate([
'user_id' => $this->user_id,
'agent_product_id' => $agent_product->id,
'product_id' => $agent_product->product_id,
]);
return $this->success();
}
}
Loading…
Cancel
Save