<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Collection;
use App\Models\Course;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;

class CollectionController extends Controller
{
    //我的收藏
    public function getCollection(Request $request)
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $num=$page_size*($page_index-1);
        $user_id=Auth::user()->id;
        $count=Collection::where('user_id',$user_id)->count();
        $list=Collection::with(['course:id,title'])
            ->where('user_id',$user_id)
            ->skip($num)->take($page_size)
            ->get();
    return $this->success_list($list,'',$count);
    }

    //添加收藏
    public function addCollection(Request $request)
    {
        $user_id=Auth::user()->id;
        $course_id=$request->input('course_id');
        try{
            Collection::create([
                'user_id'=>$user_id,
                'course_id'=>$course_id
            ]);
            Course::where('id',$course_id)->increment('collection');

            return $this->success([]);
        }catch(\Exception $e){
            return $this->error($e->getMessage());
        }
    }
}
