"""
  @author: LinTao
  @time: 2022/10/13 23:12
  @desc:
"""
import time

from flask import session
from sqlalchemy import Table

from common.database import dbconnect

dbsession, md, DBase = dbconnect()


class Favorite(DBase):
    __table__ = Table('favorite', md, autoload=True)

    def insert_favorite(self, articleid):
        """插入文章收藏数据"""
        row = dbsession.query(Favorite).filter_by(articleid=articleid,
                                                  userid=session.get("userid")).first()
        if row is not None:
            row.canceled = 0
        else:
            now = time.strftime('%Y-%m-%d %H:%M:%S')
            favorite = Favorite(articleid=articleid, userid=session.get("userid"),
                                canceled=0, createtime=now, updatetime=now)
            dbsession.add(favorite)
            dbsession.commit()

    def cancel_favorite(self, articleid):
        """取消收藏"""
        row = dbsession.query(Favorite).filter_by(articleid=articleid,
                                                  userid=session.get("userid")).first()
        row.canceled = 1
        dbsession.commit()

    def check_favorite(self, articleid):
        """判断是否已经被收藏"""
        row = dbsession.query(Favorite).filter_by(articleid=articleid,
                                                  userid=session.get("userid")).first()
        if row is None:
            return False
        elif row.canceled == 1:
            return False
        else:
            return True
