From 82e7df8b9f046a030acfe7e5c80c0149f219e2a1 Mon Sep 17 00:00:00 2001
From: fang_S <221250199@smail.nju.edu.cn>
Date: Fri, 17 Jan 2025 22:19:59 +0800
Subject: [PATCH] =?UTF-8?q?delete=E5=92=8Cdone?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 entry/src/main/ets/public/WebAPI.ets   |  71 ++++++++++++++
 entry/src/main/ets/view/ItemDetail.ets | 130 +++----------------------
 entry/src/main/ets/view/TodoItem.ets   |  19 +++-
 3 files changed, 103 insertions(+), 117 deletions(-)

diff --git a/entry/src/main/ets/public/WebAPI.ets b/entry/src/main/ets/public/WebAPI.ets
index 49bce52..cd7d7e9 100644
--- a/entry/src/main/ets/public/WebAPI.ets
+++ b/entry/src/main/ets/public/WebAPI.ets
@@ -201,6 +201,46 @@ export default class webAPI {
     return result
   }
 
+  static async DoneSingleItem(id:number): Promise<string> {
+    /*
+      鏂板缓浜嬮」锛岃繑鍥瀞tring绫诲瀷缁撴灉
+      * 1. 鎴愬姛锛歴uccess
+      * 2. 澶辫触锛歟rror
+    */
+    if(webAPI.token == '') {
+      promptAction.showToast({ message: '鏈櫥褰曪紝娣诲姞浜嬮」澶辫触' })
+      return 'error'
+    }
+    let httpRequest = http.createHttp()
+    let result = 'error' // 榛樿杩斿洖 error
+    try {
+      const data = await httpRequest.request('localhost:8080/api/todolist/add', {
+        method: http.RequestMethod.POST,
+        header: {
+          'token': webAPI.token
+        },
+        extraData: {
+          "id": id,
+          "isDone": 'TRUE',
+          "userId": webAPI.userId
+        },
+      })
+      if (data.responseCode === 200) {
+        result = 'success'
+      } else {
+        result = 'error'
+      }
+      // for debug
+      // console.log(String(JSON.stringify(data)))
+    } catch (error) {
+      console.error('Registration API error:', error)
+      result = 'error'
+    } finally {
+      httpRequest.destroy()
+    }
+    return result
+  }
+
   static async getDone(): Promise<number> {
     /*
       鑾峰彇褰撳墠鐢ㄦ埛宸插畬鎴愮殑浜嬮」鏁扮洰
@@ -232,4 +272,35 @@ export default class webAPI {
     return -1
   }
 
+  static async DeleteSingleItem(id:number): Promise<string> {
+    /*
+      鐢ㄦ埛鐧诲綍锛岃繑鍥瀞tring绫诲瀷缁撴灉
+      * 1. 鎴愬姛锛歴uccess
+      * 2. 澶辫触锛歠ailed锛堝ぇ姒傜巼鏄墜鏈哄彿/瀵嗙爜閿欒锛�
+    */
+    let httpRequest = http.createHttp()
+    let result = ''  // 鍒濆鍊间负绌哄瓧绗︿覆
+    try {
+      const data = await httpRequest.request(`localhost:8080/api/todolist/update?id=${id}`, {
+        method: http.RequestMethod.POST,
+        header: {
+          'token': webAPI.token
+        }
+      });
+      if (data.responseCode === 200) {
+        result = 'success'
+      } else {
+        result = 'error'
+      }
+      // for debug
+      // console.log(String(JSON.stringify(data)))
+    } catch (error) {
+      console.error('Login API error:', error)
+      result = 'failed'
+    } finally {
+      httpRequest.destroy()
+    }
+    return result
+  }
+
 }
\ No newline at end of file
diff --git a/entry/src/main/ets/view/ItemDetail.ets b/entry/src/main/ets/view/ItemDetail.ets
index 2c2a744..3307c61 100644
--- a/entry/src/main/ets/view/ItemDetail.ets
+++ b/entry/src/main/ets/view/ItemDetail.ets
@@ -2,6 +2,7 @@
 import CommonConstants from '../public/CommonConstant'
 import TodoItemModel from '../viewmodel/TodoItemModel'
 import { promptAction } from '@kit.ArkUI'
+import webAPI from '../public/WebAPI'
 
 @Component
 export default struct ItemDetail {
@@ -73,117 +74,6 @@ export default struct ItemDetail {
 
   @Builder
   MainPage() {
-/*    Column() {
-      Row() {
-        Text('鍚嶅瓧')
-          .fontSize(CommonConstants.detail_title_font_size)
-          .fontWeight(FontWeight.Bold)
-          .width(CommonConstants.detail_width)
-          .margin({
-            left: CommonConstants.detail_margin_Left
-          })
-
-        Text(this.content.itemName)
-          .fontSize(CommonConstants.detail_font_size)
-          .width(CommonConstants.detail_width)
-          .textAlign(TextAlign.Center)
-      }
-    }
-    .margin({
-      top: CommonConstants.list_margin_top
-    })
-    .width(CommonConstants.list_width)
-    .height(CommonConstants.name_height)
-    .backgroundColor($r('app.color.white'))
-    .borderRadius(CommonConstants.border_radius)
-    .justifyContent(FlexAlign.Center)
-    .alignItems(HorizontalAlign.Start)
-
-    Column() {
-      Row() {
-        Text('鎻忚堪')
-          .fontSize(CommonConstants.detail_title_font_size)
-          .fontWeight(FontWeight.Bold)
-          .width(CommonConstants.detail_width)
-          .margin({
-            left: CommonConstants.detail_margin_Left
-          })
-
-        Scroll() {
-          Text(this.content.itemDisc)
-            .fontSize(CommonConstants.detail_font_size)
-            .width(CommonConstants.detail_width)
-            .textAlign(TextAlign.Center)
-        }
-      }
-    }
-    .margin({
-      top: CommonConstants.list_margin_top
-    })
-    .width(CommonConstants.list_width)
-    .height(CommonConstants.disc_height)
-    .backgroundColor($r('app.color.white'))
-    .borderRadius(CommonConstants.border_radius)
-    .justifyContent(FlexAlign.Center)
-    .alignItems(HorizontalAlign.Start)
-
-    Column() {
-      Row() {
-        Text('鏃堕棿')
-          .fontSize(CommonConstants.detail_title_font_size)
-          .fontWeight(FontWeight.Bold)
-          .width(CommonConstants.detail_width)
-          .margin({
-            left: CommonConstants.detail_margin_Left
-          })
-
-        Scroll() {
-          Text(this.content.itemTime)
-            .fontSize(CommonConstants.detail_font_size)
-            .width(CommonConstants.detail_width)
-            .textAlign(TextAlign.Center)
-        }
-      }
-    }
-    .margin({
-      top: CommonConstants.list_margin_top
-    })
-    .width(CommonConstants.list_width)
-    .height(CommonConstants.time_height)
-    .backgroundColor($r('app.color.white'))
-    .borderRadius(CommonConstants.border_radius)
-    .justifyContent(FlexAlign.Center)
-    .alignItems(HorizontalAlign.Start)
-
-    if(this.content.isCompleted) {
-      Column() {
-        Row() {
-          Text('瀹屾垚鏃堕棿')
-            .fontSize(CommonConstants.detail_title_font_size)
-            .fontWeight(FontWeight.Bold)
-            .width(CommonConstants.detail_width)
-            .margin({
-              left: CommonConstants.detail_margin_Left
-            })
-
-          Scroll() {
-            Text(this.content.completeTime)
-              .fontSize(CommonConstants.detail_font_size)
-              .width(CommonConstants.detail_width)
-              .textAlign(TextAlign.Center)
-          }
-        }
-      }
-      .margin({
-        top: CommonConstants.list_margin_top
-      })
-      .width(CommonConstants.list_width)
-      .height(CommonConstants.time_height)
-      .backgroundColor($r('app.color.white'))
-      .borderRadius(CommonConstants.border_radius)
-      .justifyContent(FlexAlign.Center)
-      .alignItems(HorizontalAlign.Start)
-    }*/
     Column() {
       // 鍚嶅瓧琛�
       Row() {
@@ -285,11 +175,19 @@ export default struct ItemDetail {
 
   }
 
-  private deleteItem() {
-    //todo锛氬垹闄わ紙杩樻湁鏇存柊锛�
-    promptAction.showToast({
-      message: '鍒犻櫎鎴愬姛锛�'
-    })
+  private async deleteItem() {
+    // 鍒犻櫎浜嬩欢
+        const res = await webAPI.DeleteSingleItem(this.content.id)
+        if(res == 'success') {
+          promptAction.showToast({ message: '鍒犻櫎鎴愬姛' })
+          setTimeout(() => {
+
+          }, 500)
+        }
+        else {
+          promptAction.showToast({ message: '鍒犻櫎澶辫触' })
+        }
+
     this.LocalPageStack.pop({},true)
   }
 }
\ No newline at end of file
diff --git a/entry/src/main/ets/view/TodoItem.ets b/entry/src/main/ets/view/TodoItem.ets
index 593eb3c..06b634e 100644
--- a/entry/src/main/ets/view/TodoItem.ets
+++ b/entry/src/main/ets/view/TodoItem.ets
@@ -3,6 +3,7 @@ import CommonConstants from '../public/CommonConstant'
 import TodoItemModel from '../viewmodel/TodoItemModel'
 import { promptAction } from '@kit.ArkUI'
 import { http } from '@kit.NetworkKit'
+import webAPI from '../public/WebAPI';
 
 interface HttpError {
   message: string;
@@ -31,7 +32,8 @@ export default struct ToDoItem {
       .margin(CommonConstants.checkbox_margin)
       .onClick(() => {
         // this.myTask.isCompleted = !this.myTask.isCompleted
-        this.markComplete()
+        //this.markComplete()
+        this.DoneItem(this.myTask.id)
       })
   }
 
@@ -115,4 +117,19 @@ export default struct ToDoItem {
     //   })
     // }
   }
+  private async DoneItem(id:number) {
+    // 瀹屾垚浜嬩欢
+    const res = await webAPI.DoneSingleItem(id)
+    if(res == 'success') {
+      promptAction.showToast({ message: '宸插畬鎴愭浜嬩欢' })
+      setTimeout(() => {
+
+      }, 500)
+    }
+    else {
+      promptAction.showToast({ message: '瀹屾垚澶辫触' })
+    }
+
+    this.LocalPageStack.pop({},true)
+  }
 }
\ No newline at end of file
-- 
GitLab