backend: iplement marks search

This commit is contained in:
OleSTEEP 2025-10-05 20:49:59 +03:00
parent 23a00d2c06
commit 59d092c9d0
2 changed files with 62 additions and 54 deletions

View file

@ -51,8 +51,8 @@ def new_mark(mark: Mark):
@app.post("/api/marks") @app.post("/api/marks")
def search_marks(part: str): def search_marks(query: Mark):
raise HTTPException(status_code=501, detail="Method is not implemented yet!") return database.search_mark(query)
@app.post("/api/thumb") @app.post("/api/thumb")

View file

@ -1,74 +1,84 @@
import sqlite3 import sqlite3
from utils import asset from utils import asset
from vntypes import Mark
class VNDB: class VNDB:
def __init__(self, db_name=asset('vn_database.db')): def __init__(self, db_name=asset('vn_database.db')):
self.__db_name = db_name self.__db_name = db_name
connection = sqlite3.connect(self.__db_name) with sqlite3.connect(self.__db_name) as connection:
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(''' cursor.execute('''
CREATE TABLE IF NOT EXISTS marks ( CREATE TABLE IF NOT EXISTS marks (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
tag TEXT NOT NULL, type TEXT NOT NULL,
value TEXT NOT NULL value TEXT NOT NULL
); );
''') ''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS authors (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
thumbnail TEXT NOT NULL
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS novels (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author INTEGER NOT NULL
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS posts_log (
novel INTEGER NOT NULL,
channel INTEGER NOT NULL,
marks JSON NOT NULL,
cursor.execute('''
CREATE TABLE IF NOT EXISTS authors (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL, title TEXT NOT NULL,
description TEXT NOT NULL, description TEXT NOT NULL,
thumbnail TEXT NOT NULL
);
''')
cursor.execute(''' author INTEGER NOT NULL,
CREATE TABLE IF NOT EXISTS novels (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author INTEGER NOT NULL
);
''')
cursor.execute(''' files_on_disk JSON NOT NULL,
CREATE TABLE IF NOT EXISTS posts_log ( post_info JSON,
post_at INTEGER NOT NULL,
created_at INTEGER NOT NULL
);
''')
novel INTEGER NOT NULL, connection.commit()
channel INTEGER NOT NULL,
marks JSON NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
author INTEGER NOT NULL,
files_on_disk JSON NOT NULL,
post_info JSON,
post_at INTEGER NOT NULL,
created_at INTEGER NOT NULL
);
''')
connection.commit()
connection.close()
def __execute(self, sql: str, params: tuple): def __execute(self, sql: str, params: tuple):
connection = sqlite3.connect(self.__db_name) with sqlite3.connect(self.__db_name) as connection:
connection.cursor().execute(sql, params) connection.cursor().execute(sql, params)
connection.commit() connection.commit()
connection.close()
def __fetch(self, sql: str, params: tuple):
with sqlite3.connect(self.__db_name) as connection:
cursor = connection.cursor()
cursor.execute(sql, params)
return cursor.fetchall()
def insert_mark(self, type: str, value: str): def insert_mark(self, type: str, value: str):
self.__execute("INSERT INTO marks (tag, value) " self.__execute("INSERT INTO marks (type, value) "
"VALUES (?, ?)", (type, value)) "VALUES (?, ?)", (type, value))
def search_mark(self, query: Mark):
return self.__fetch("SELECT type, value FROM marks "
"WHERE value LIKE ? "
"AND type = ?", ('%'+query.value+'%',query.type))
def insert_novel(self, title: str, author_id: int): def insert_novel(self, title: str, author_id: int):
self.__execute("INSERT INTO novels (title, author) " self.__execute("INSERT INTO novels (title, author) "
"VALUES (?, ?)", (title, author_id)) "VALUES (?, ?)", (title, author_id))
@ -76,5 +86,3 @@ class VNDB:
def insert_author(self, title: str, desc: str, thumb: str): def insert_author(self, title: str, desc: str, thumb: str):
self.__execute("INSERT INTO authors (title, description, thumbnail) " self.__execute("INSERT INTO authors (title, description, thumbnail) "
"VALUES (?, ?, ?)", (title, desc, thumb)) "VALUES (?, ?, ?)", (title, desc, thumb))