|
|
@@ -87,29 +87,37 @@ export class CloudQuery {
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
-
|
|
|
+ include(...fields: string[]) {
|
|
|
+ this.whereOptions["include"] = fields;
|
|
|
+ }
|
|
|
greaterThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$gt"] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$gt"] = value;
|
|
|
}
|
|
|
|
|
|
greaterThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$gte"] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$gte"] = value;
|
|
|
}
|
|
|
|
|
|
lessThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$lt"] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$lt"] = value;
|
|
|
}
|
|
|
|
|
|
lessThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$lte"] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$lte"] = value;
|
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
|
- this.whereOptions[key] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ this.whereOptions["where"][key] = value;
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
async get(id: string) {
|
|
|
@@ -130,13 +138,23 @@ export class CloudQuery {
|
|
|
return json || {};
|
|
|
}
|
|
|
|
|
|
- async find(){
|
|
|
+ async find(): Promise<Array<CloudObject>>{
|
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
- if (Object.keys(this.whereOptions).length) {
|
|
|
- const whereStr = JSON.stringify(this.whereOptions);
|
|
|
- url += `where=${whereStr}`;
|
|
|
- }
|
|
|
+ // 构建查询字符串
|
|
|
+ let queryStr = '';
|
|
|
+ Object.keys(this.whereOptions).forEach(key => {
|
|
|
+ let paramStr = JSON.stringify(this.whereOptions[key]);
|
|
|
+ if (key === "include") {
|
|
|
+ paramStr = this.whereOptions[key]?.join(","); // 将数组转换为逗号分隔字符串
|
|
|
+ }
|
|
|
+ // 添加查询参数到 URL
|
|
|
+ if (queryStr) {
|
|
|
+ url += `&${key}=${paramStr}`;
|
|
|
+ } else {
|
|
|
+ url += `${key}=${paramStr}`;
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
|
@@ -374,19 +392,39 @@ export class CloudUser extends CloudObject {
|
|
|
}
|
|
|
|
|
|
//CloudPost.ts
|
|
|
+interface PostData {
|
|
|
+ title: string; // 标题属性
|
|
|
+ content: string; // 内容属性
|
|
|
+ [key: string]: any; // 其他任意属性
|
|
|
+}
|
|
|
+
|
|
|
export class CloudPost extends CloudObject {
|
|
|
constructor() {
|
|
|
super('FilmPost'); // 调用父类构造函数,指定类名
|
|
|
}
|
|
|
|
|
|
// 设置帖子数据,包括发布者信息
|
|
|
- setPostData(postData: Record<string, any>, userId: string|null) {
|
|
|
+ setPostData(postData: PostData, userId: string | null) {
|
|
|
+ // 确保 title 和 content 是必需的
|
|
|
+ if (!postData.title || !postData.content) {
|
|
|
+ throw new Error('Both title and content are required.');
|
|
|
+ }
|
|
|
+
|
|
|
this.set(postData); // 设置帖子内容
|
|
|
this.data["author"] = { "__type": "Pointer", "className": "_User", "objectId": userId }; // 设置发布者信息
|
|
|
}
|
|
|
|
|
|
+ // 点赞帖子
|
|
|
+ async likePost() {
|
|
|
+ }
|
|
|
+
|
|
|
// 更新帖子数据
|
|
|
- async updatePost(postData: Record<string, any>) {
|
|
|
+ async updatePost(postData: PostData) {
|
|
|
+ // 确保 title 和 content 是必需的
|
|
|
+ if (!postData.title || !postData.content) {
|
|
|
+ throw new Error('Both title and content are required.');
|
|
|
+ }
|
|
|
+
|
|
|
this.set(postData); // 更新帖子内容
|
|
|
return await this.save(); // 保存更新
|
|
|
}
|
|
|
@@ -396,3 +434,125 @@ export class CloudPost extends CloudObject {
|
|
|
return await this.destroy(); // 调用父类的 destroy 方法
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+// CloudComment.ts
|
|
|
+interface CommentData {
|
|
|
+ content: string; // 评论内容
|
|
|
+ postId: string; // 关联的帖子 ID
|
|
|
+ [key: string]: any; // 其他任意属性
|
|
|
+}
|
|
|
+
|
|
|
+export class CloudComment extends CloudObject {
|
|
|
+ constructor() {
|
|
|
+ super('FilmPostComment'); // 调用父类构造函数,指定类名
|
|
|
+ }
|
|
|
+ // 设置评论数据,包括发布者和关联的帖子
|
|
|
+ setCommentData(CommentData: CommentData, userId: string | null) {
|
|
|
+ // 确保 title 和 content 是必需的
|
|
|
+ if (!CommentData.postId || !CommentData.content) {
|
|
|
+ throw new Error('Both post and content are required.');
|
|
|
+ }
|
|
|
+
|
|
|
+ this.set(CommentData); // 设置帖子内容
|
|
|
+ this.data["user"] = { "__type": "Pointer", "className": "_User", "objectId": userId }; // 设置发布者信息
|
|
|
+ this.data["post"] = { "__type": "Pointer", "className": "FilmPost", "objectId": CommentData.postId }; // 设置关联的帖子信息
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 创建评论
|
|
|
+ async createComment(commentData: CommentData, userId: string | null) {
|
|
|
+ this.setCommentData(commentData, userId);
|
|
|
+ try {
|
|
|
+ const savedComment = await this.save();
|
|
|
+ return savedComment; // 返回保存的评论
|
|
|
+ } catch (error) {
|
|
|
+ console.error("Error creating comment:", error);
|
|
|
+ throw error; // 重新抛出错误
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 更新帖子评论计数
|
|
|
+ async updatePostCommentCount(postId: string, increment: number) {
|
|
|
+ const postQuery = new CloudQuery('FilmPost');
|
|
|
+ const post = await postQuery.get(postId);
|
|
|
+ if (!post) {
|
|
|
+ console.error(`Post with ID ${postId} not found.`);
|
|
|
+ throw new Error(`Post with ID ${postId} not found.`);
|
|
|
+ }
|
|
|
+ // 确保 post.data 存在 commentCount 属性
|
|
|
+ if (typeof post.data["commentCount"] === 'undefined') {
|
|
|
+ console.warn(`commentCount property is undefined for postId: ${postId}. Initializing to 0.`);
|
|
|
+ post.data["commentCount"] = 0; // 初始化为 0
|
|
|
+ }
|
|
|
+
|
|
|
+ post.data["commentCount"] = Math.max((post.data["commentCount"] || 0) + increment, 0); // 更新评论计数
|
|
|
+ const result = await post.save(); // 保存更新后的帖子
|
|
|
+ console.log("Post updated successfully:", result);
|
|
|
+}
|
|
|
+
|
|
|
+// 更新评论
|
|
|
+async updateComment(commentData: CommentData) {
|
|
|
+ if (!this.id) {
|
|
|
+ throw new Error('Comment ID is required for updating.');
|
|
|
+ }
|
|
|
+
|
|
|
+ this.set(commentData); // 更新评论内容
|
|
|
+ return await this.save(); // 保存更新
|
|
|
+}
|
|
|
+
|
|
|
+// 删除评论
|
|
|
+async deleteComment() {
|
|
|
+ if (!this.id) {
|
|
|
+ throw new Error('Comment ID is required for deletion.');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取关联的帖子并更新评论计数
|
|
|
+ return await this.destroy(); // 调用父类的 destroy 方法
|
|
|
+}
|
|
|
+
|
|
|
+// 查询评论
|
|
|
+async findComments(postId: string) {
|
|
|
+
|
|
|
+}
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//CloudFeedback.ts
|
|
|
+interface FeedbackData {
|
|
|
+ feedback: string; // 内容属性
|
|
|
+ [key: string]: any; // 其他任意属性
|
|
|
+}
|
|
|
+
|
|
|
+export class CloudFeedback extends CloudObject {
|
|
|
+ constructor() {
|
|
|
+ super('FilmFeedback'); // 调用父类构造函数,指定类名
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置反馈数据,包括发布者信息
|
|
|
+ setFeedbackData(feedbackData: FeedbackData, userId: string | null) {
|
|
|
+ // 确保 feedback 是必需的
|
|
|
+ if (!feedbackData.feedback) {
|
|
|
+ throw new Error('feedback is required.');
|
|
|
+ }
|
|
|
+
|
|
|
+ this.set(feedbackData); // 设置帖子内容
|
|
|
+ this.data["user"] = { "__type": "Pointer", "className": "_User", "objectId": userId }; // 设置发布者信息
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新帖子数据
|
|
|
+ async updateFeedback(feedbackData: FeedbackData) {
|
|
|
+ // 确保 feedback 是必需的
|
|
|
+ if (!feedbackData.feedback) {
|
|
|
+ throw new Error('feedback is required.');
|
|
|
+ }
|
|
|
+
|
|
|
+ this.set(feedbackData); // 更新反馈内容
|
|
|
+ return await this.save(); // 保存更新
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除帖子
|
|
|
+ async deleteFeedback() {
|
|
|
+ return await this.destroy(); // 调用父类的 destroy 方法
|
|
|
+ }
|
|
|
+}
|