26 lines
640 B
Python
26 lines
640 B
Python
import sqlite3
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
|
|
|
|
@dataclass
|
|
class Link:
|
|
link: str
|
|
user_id: str
|
|
id: int = None
|
|
read_at: datetime = None
|
|
added_at: datetime = None
|
|
|
|
def create(self, db: sqlite3.Connection):
|
|
self.added_at = datetime.now()
|
|
cur = db.cursor()
|
|
rows = (cur.execute(
|
|
"INSERT INTO links(link, user_id, added_at) VALUES(?, ?, ?) RETURNING id;",
|
|
(self.link, self.user_id, self.added_at)
|
|
))
|
|
r = list(rows)
|
|
assert len(r) == 1
|
|
assert len(r[0]) == 1
|
|
assert int(r[0][0])
|
|
self.id = r[0][0]
|