<?php


namespace Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class Channels extends Model
{
    protected $table = 'channels';

    public static function getChannel($chatid)
    {

        return DB::connection('sqlite')->table('channels')->where('chatid', $chatid)->first();
    }

    public static function getById($id)
    {
        return DB::connection('sqlite')->table('channels')->where('id', $id)->first();
    }

    public static function getAll()
    {
        return DB::connection('sqlite')->table('channels')->get();
    }

    public static function addChannel($chatid)
    {

        return DB::connection('sqlite')->table('channels')->insertGetId(['chatid' => $chatid]);
    }

    public static function hasChannel($chatid)
    {
        return DB::connection('sqlite')->table('channels')->select('id')->where('chatid', $chatid)->exists();
    }

    public static function removeChannel($chatid)
    {
        return DB::connection('sqlite')->table('channels')->where('chatid', $chatid)->delete();

    }

    public static function removeChannelById($id)
    {
        return DB::connection('sqlite')->table('channels')->where('id', $id)->delete();
    }

    public static function channelCount()
    {
        return DB::connection('sqlite')->table('channels')->select('id')->count();
    }

    public static function getCount($id)
    {
        return DB::connection('sqlite')->table('channels')->where('id', $id)->first()->count;

    }

    public static function addCount($id)
    {
        $get = self::getCount($id) + 1;
        DB::connection('sqlite')->table('channels')->where('id', $id)->update(['count' => $get]);
        return true;
    }

    public static function updateLinks($id, $value)
    {
        DB::connection('sqlite')->table('channels')->where('id', $id)->update(['link' => $value]);
        return true;
    }

}
