-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentLoader.cs
More file actions
138 lines (114 loc) · 4.63 KB
/
Copy pathCommentLoader.cs
File metadata and controls
138 lines (114 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using RYCBEditorX.Utils;
namespace RYCBEditorX.MySQL;
public class CommentLoader(ISQLConnectionUtils sqlUtils, string condition = "")
{
public List<Comment> LoadCommentsAsync()
{
var comments = new List<Comment>();
// 查询数据库中的评论数据
var rows = sqlUtils?.Select("comments", condition: condition);
if (rows is null) { return []; }
foreach (var row in rows)
{
var comment = new Comment
{
User = row["usr"].ToString(),
Uid = row["uid"].ToString(),
CommentText = row["commentText"].ToString(),
Time = row["time"] != DBNull.Value ? Convert.ToDateTime(row["time"]).ToString("yyyy-MM-dd HH:mm:ss") : "Unknown",
Likes = row["likes"] != DBNull.Value ? Convert.ToInt32(row["likes"]) : 0,
Target = row["target"].ToString().Replace("\uffff", "'")
};
comments.Add(comment);
}
return comments;
}
public bool DeleteComment(string uid)
{
if (int.TryParse(uid, out var id))
{
if (id >= 0 && id <= 60)
{
return false;
}
}
var condition = $"uid='{uid}'";
var success = sqlUtils.Delete("comments", condition);
if (success)
{
GlobalConfig.CurrentLogger.Log($"成功删除UID为'{uid}'的评论。", module: EnumLogModule.SQL);
}
else
{
GlobalConfig.CurrentLogger.Log($"删除UID为'{uid}'的评论失败。", module: EnumLogModule.SQL);
}
return success;
}
public bool UpdateLikes(string uid, int newLikes)
{
var fields = new List<string> { "likes" };
var values = new List<string> { newLikes.ToString() };
var condition = $"uid = '{uid}'";
var success = sqlUtils.Update("comments", fields, values, condition);
if (success)
{
GlobalConfig.CurrentLogger.Log($"成功将UID为'{uid}'的评论点赞数更新为{newLikes}。", module:EnumLogModule.SQL);
}
else
{
GlobalConfig.CurrentLogger.Log($"将UID为'{uid}'的评论点赞数更新为{newLikes}失败。", module: EnumLogModule.SQL);
}
return success;
}
public bool AddComment(Comment newComment)
{
// 定义表名
var tableName = "comments";
// 定义字段列表
var fieldsList = "usr, uid, commentText, time, likes, target";
// 定义值列表,并对特殊字符进行转义(例如引号)
var valuesList = $"'{newComment.User}', '{newComment.Uid}', '{newComment.CommentText}', '{newComment.Time}', {newComment.Likes}, '{newComment.Target.Replace("'", "\uffff")}'";
// 执行插入操作
var success = sqlUtils.Insert(tableName, fieldsList, valuesList);
if (success)
{
GlobalConfig.CurrentLogger.Log($"成功插入新的评论,UID为'{newComment.Uid}'。", module: EnumLogModule.SQL);
}
else
{
GlobalConfig.CurrentLogger.Log($"插入新的评论失败,UID为'{newComment.Uid}'。", module: EnumLogModule.SQL);
}
return success;
}
public bool AddComment(string markdownedCommentText, string user, string target = "")
{
var newComment = new Comment()
{
CommentText = markdownedCommentText,
User = user,
Uid= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ComputeMd5(),
Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Likes = 0,
Target = target
};
// 定义表名
var tableName = "comments";
// 定义字段列表
var fieldsList = "usr, uid, commentText, time, likes, target";
// 定义值列表,并对特殊字符进行转义(例如引号)
var valuesList = $"'{newComment.User}', '{newComment.Uid}', '{newComment.CommentText}', '{newComment.Time}', {newComment.Likes}, '{newComment.Target.Replace("'", "\uffff")}'";
// 执行插入操作
var success = sqlUtils.Insert(tableName, fieldsList, valuesList);
if (success)
{
GlobalConfig.CurrentLogger.Log($"成功插入新的评论,UID为'{newComment.Uid}',命中'{newComment.Target}'。", module: EnumLogModule.SQL);
}
else
{
GlobalConfig.CurrentLogger.Log($"插入新的命中'{newComment.Target}'的评论失败,本应插入的UID为'{newComment.Uid}'。", module: EnumLogModule.SQL);
}
return success;
}
}