mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-14 09:38:13 +08:00
✨ 帖子收藏 #100
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
/**
|
||||
* @file plugins/Sqlite/index.ts
|
||||
* @description Sqlite 数据库操作类
|
||||
* @since Beta v0.4.4
|
||||
* @since Beta v0.4.5
|
||||
*/
|
||||
|
||||
import { app } from "@tauri-apps/api";
|
||||
import Database from "tauri-plugin-sql-api";
|
||||
|
||||
import initDataSql from "./sql/initData";
|
||||
import initTableSql from "./sql/initTable";
|
||||
import {
|
||||
importAbyssData,
|
||||
insertAbyssData,
|
||||
insertAppData,
|
||||
insertGameAccountData,
|
||||
insertPostCollectData,
|
||||
insertRecordData,
|
||||
insertRoleData,
|
||||
} from "./sql/insertData";
|
||||
@@ -66,12 +66,12 @@ class Sqlite {
|
||||
|
||||
/**
|
||||
* @description 初始化数据库
|
||||
* @since Beta v0.3.3
|
||||
* @since Beta v0.4.5
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async initDB(): Promise<void> {
|
||||
const db = await this.getDB();
|
||||
const sql = [...initTableSql(), ...(await initDataSql())];
|
||||
const sql = await initDataSql();
|
||||
for (const item of sql) {
|
||||
await db.execute(item);
|
||||
}
|
||||
@@ -181,7 +181,6 @@ class Sqlite {
|
||||
let isVerified = false;
|
||||
const sqlT = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
|
||||
const res: Array<{ name: string }> = await db.select(sqlT);
|
||||
// 只检测已有的表是否具备,不检测总表数目
|
||||
if (this.tables.every((item) => res.map((i) => i.name).includes(item))) {
|
||||
isVerified = true;
|
||||
}
|
||||
@@ -491,6 +490,70 @@ class Sqlite {
|
||||
if (res.length === 0) return undefined;
|
||||
return res[0].id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测特定表是否存在
|
||||
* @since Beta v0.4.5
|
||||
* @param {string} table 表名
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async checkTableExist(table: string): Promise<boolean> {
|
||||
const db = await this.getDB();
|
||||
const sql = `SELECT name
|
||||
FROM sqlite_master
|
||||
WHERE type='table'
|
||||
AND name='${table}';`;
|
||||
const res: Array<{ name: string }> = await db.select(sql);
|
||||
return res.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测帖子是否已收藏
|
||||
* @since Beta v0.4.5
|
||||
* @param {string} postId 帖子 id
|
||||
* @returns {Promise<false|string>} 当该帖子被归到多个分类时,返回分类数量
|
||||
*/
|
||||
async checkPostCollect(postId: string): Promise<false | string> {
|
||||
const db = await this.getDB();
|
||||
const sql = `SELECT collect
|
||||
FROM UserCollection
|
||||
WHERE postId = '${postId}';`;
|
||||
const res: Array<{ collect: string }> = await db.select(sql);
|
||||
if (res.length === 0) return false;
|
||||
return res[0].collect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 收藏单个帖子
|
||||
* @since Beta v0.4.5
|
||||
* @param {TGApp.Plugins.Mys.Post.FullData} post 帖子
|
||||
* @param {Array<string>} collect 分类
|
||||
* @param {string} uid 用户 uid
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async collectPost(
|
||||
post: TGApp.Plugins.Mys.Post.FullData,
|
||||
collect: string[],
|
||||
uid?: string,
|
||||
): Promise<void> {
|
||||
const db = await this.getDB();
|
||||
const sql = insertPostCollectData(post, collect, uid);
|
||||
await db.execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 取消收藏
|
||||
* @since Beta v0.4.5
|
||||
* @param {string} postId 帖子 id
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async cancelCollect(postId: string): Promise<void> {
|
||||
const db = await this.getDB();
|
||||
const sql = `DELETE
|
||||
FROM UserCollection
|
||||
WHERE postId = '${postId}';`;
|
||||
await db.execute(sql);
|
||||
}
|
||||
}
|
||||
|
||||
const TGSqlite = new Sqlite();
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/**
|
||||
* @file plugins Sqlite sql initData.ts
|
||||
* @file plugins/Sqlite/sql/initData.ts
|
||||
* @description Sqlite 初始化数据 sql 语句
|
||||
* @since Alpha v0.2.0
|
||||
* @since Beta v0.4.5
|
||||
*/
|
||||
|
||||
import { app } from "@tauri-apps/api";
|
||||
|
||||
import initTableSql from "./initTable";
|
||||
import {
|
||||
insertAchievementData,
|
||||
insertAchievementSeriesData,
|
||||
@@ -31,19 +32,21 @@ async function initAppData(): Promise<string[]> {
|
||||
const buildTime: string = getBuildTime();
|
||||
// 初始化应用版本
|
||||
sqlRes.push(`
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('appVersion', '${appVersion}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO UPDATE SET value = '${appVersion}', updated = datetime('now', 'localtime');`);
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('appVersion', '${appVersion}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO UPDATE SET value = '${appVersion}',
|
||||
updated = datetime('now', 'localtime');`);
|
||||
// 初始化应用数据更新时间
|
||||
sqlRes.push(`
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('dataUpdated', '${buildTime}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO UPDATE SET value = '${buildTime}', updated = datetime('now', 'localtime');`);
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('dataUpdated', '${buildTime}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO UPDATE SET value = '${buildTime}',
|
||||
updated = datetime('now', 'localtime');`);
|
||||
// 初始化 cookie
|
||||
sqlRes.push(`
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('cookie', '{}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO NOTHING;`);
|
||||
INSERT INTO AppData (key, value, updated)
|
||||
VALUES ('cookie', '{}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(key) DO NOTHING;`);
|
||||
return sqlRes;
|
||||
}
|
||||
|
||||
@@ -85,11 +88,12 @@ function initCharacterData(): string[] {
|
||||
|
||||
/**
|
||||
* @description 初始化数据
|
||||
* @since Alpha v0.2.0
|
||||
* @since Beta v0.4.5
|
||||
* @returns {Promise<string[]>} sql
|
||||
*/
|
||||
async function initDataSql(): Promise<string[]> {
|
||||
const sqlRes: string[] = [];
|
||||
sqlRes.push(...initTableSql());
|
||||
sqlRes.push(...(await initAppData()));
|
||||
sqlRes.push(...initAchievementSeriesData());
|
||||
sqlRes.push(...initAchievementData());
|
||||
|
||||
@@ -269,3 +269,28 @@ export function insertRoleData(uid: string, data: TGApp.Game.Character.ListItem[
|
||||
});
|
||||
return sql.join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 插入帖子收藏数据
|
||||
* @since Beta v0.4.5
|
||||
* @param {TGApp.Plugins.Mys.Post.FullData} data 帖子数据
|
||||
* @param {Array<string>} collect 合集
|
||||
* @param {string} uid 用户 UID
|
||||
* @returns {string} sql
|
||||
*/
|
||||
export function insertPostCollectData(
|
||||
data: TGApp.Plugins.Mys.Post.FullData,
|
||||
collect: string[],
|
||||
uid?: string,
|
||||
): string {
|
||||
return `
|
||||
INSERT INTO UserCollection (postId, title, content, collect, uid, updated)
|
||||
VALUES (${data.post.post_id}, '${data.post.subject}', '${JSON.stringify(data)}',
|
||||
'${JSON.stringify(collect)}', '${uid}', datetime('now', 'localtime'))
|
||||
ON CONFLICT DO UPDATE
|
||||
SET title = '${data.post.subject}',
|
||||
content = '${JSON.stringify(data)}',
|
||||
collect = '${JSON.stringify(collect)}',
|
||||
updated = datetime('now', 'localtime');
|
||||
`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user