Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit 059e9cb

Browse files
committed
feat: clear playrecords and search history
1 parent c7829b2 commit 059e9cb

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

database/database.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ func DeletePlayRecord(source, sourceID string) error {
250250
return err
251251
}
252252

253+
// DeleteAllPlayRecords 删除所有播放记录
254+
func DeleteAllPlayRecords() error {
255+
query := "DELETE FROM play_records"
256+
_, err := DB.Exec(query)
257+
return err
258+
}
259+
253260
// GetAllFavorites 获取所有收藏夹记录
254261
func GetAllFavorites() (map[string]models.Favorite, error) {
255262
query := `
@@ -530,3 +537,13 @@ func DeleteSearchHistoryKeyword(keyword string) error {
530537

531538
return nil
532539
}
540+
541+
// ClearSearchHistory 清空搜索历史,将 id=1 的记录设置为空数组
542+
func ClearSearchHistory() error {
543+
query := `
544+
INSERT INTO search_history (id, record) VALUES (1, '[]')
545+
ON CONFLICT(id) DO UPDATE SET record = '[]'`
546+
547+
_, err := DB.Exec(query)
548+
return err
549+
}

handlers/play_records.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,21 @@ func handlePostPlayRecords(w http.ResponseWriter, r *http.Request) {
8080

8181
func handleDeletePlayRecords(w http.ResponseWriter, r *http.Request) {
8282
key := r.URL.Query().Get("key")
83+
84+
// 如果 key 为空,删除全部播放记录
8385
if key == "" {
84-
http.Error(w, "key parameter is required", http.StatusBadRequest)
86+
err := database.DeleteAllPlayRecords()
87+
if err != nil {
88+
http.Error(w, fmt.Sprintf("Failed to delete all play records: %v", err), http.StatusInternalServerError)
89+
return
90+
}
91+
92+
response := map[string]interface{}{
93+
"success": true,
94+
}
95+
96+
w.Header().Set("Content-Type", "application/json")
97+
sonic.ConfigDefault.NewEncoder(w).Encode(response)
8598
return
8699
}
87100

handlers/search_history.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,21 @@ func handlePostSearchHistory(w http.ResponseWriter, r *http.Request) {
7070

7171
func handleDeleteSearchHistory(w http.ResponseWriter, r *http.Request) {
7272
keyword := r.URL.Query().Get("keyword")
73+
74+
// 如果 keyword 为空,清空所有搜索历史
7375
if keyword == "" {
74-
http.Error(w, "keyword parameter is required", http.StatusBadRequest)
76+
err := database.ClearSearchHistory()
77+
if err != nil {
78+
http.Error(w, "Failed to clear search history", http.StatusInternalServerError)
79+
return
80+
}
81+
82+
response := map[string]interface{}{
83+
"success": true,
84+
}
85+
86+
w.Header().Set("Content-Type", "application/json")
87+
sonic.ConfigDefault.NewEncoder(w).Encode(response)
7588
return
7689
}
7790

0 commit comments

Comments
 (0)