From 589c2905e7722398c772a7a8fe097444e0930d55 Mon Sep 17 00:00:00 2001
From: DewanaGustavus <76590469+DewanaGustavus@users.noreply.github.com>
Date: Fri, 17 Nov 2023 03:00:18 +0700
Subject: [PATCH] feat: get history by id

---
 prisma/schema.prisma                   |  3 +++
 src/handler/history/history.router.ts  | 10 +++++++++
 src/handler/history/history.service.ts | 28 +++++++++++++++++++++++++-
 src/interfaces/History.ts              | 12 ++++++-----
 src/interfaces/HistoryDetail.ts        | 11 +++++-----
 src/interfaces/User.ts                 | 12 +++++------
 6 files changed, 59 insertions(+), 17 deletions(-)

diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 0eadc72..03e3865 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -27,7 +27,9 @@ model History {
   user_id Int
   user User @relation(fields: [user_id], references: [id])
   alamat_tujuan String
+  id_penerima Int
   nama_penerima String
+  biaya_pengiriman Int
   rating Int @default(0)
   history_detail HistoryDetail[]
 }
@@ -38,6 +40,7 @@ model HistoryDetail {
   history      History  @relation(fields: [history_id], references: [id])
   product_name String
   quantity     Int
+  price Int
 }
 
 
diff --git a/src/handler/history/history.router.ts b/src/handler/history/history.router.ts
index a8a8dfa..3e974d4 100644
--- a/src/handler/history/history.router.ts
+++ b/src/handler/history/history.router.ts
@@ -22,4 +22,14 @@ HistoryRouter.get('/detail/:id', async (request: Request, response: Response) =>
     } catch (error: any) {
         return response.status(500).json(error.message);
     }
+})
+
+HistoryRouter.get('/history/:id', async (request: Request, response: Response) => {
+    const id: number = parseInt(request.params.id, 10);
+    try {
+        const history = await HistoryServices.getHistoryById(id);
+        return response.status(200).json(history);
+    } catch (error: any) {
+        return response.status(500).json(error.message);
+    }
 })
\ No newline at end of file
diff --git a/src/handler/history/history.service.ts b/src/handler/history/history.service.ts
index 5ffb5d6..7cff54a 100644
--- a/src/handler/history/history.service.ts
+++ b/src/handler/history/history.service.ts
@@ -13,7 +13,9 @@ export async function getHistory (username : string) : Promise<History[]> {
             id: true,
             user_id: true,
             alamat_tujuan: true,
+            id_penerima: true,
             nama_penerima: true,
+            biaya_pengiriman: true,
             rating: true
         }
     })
@@ -28,7 +30,31 @@ export async function getHistoryDetail (historyId: number) : Promise<HistoryDeta
             id: true,
             history_id: true,
             product_name: true,
-            quantity: true
+            quantity: true,
+            price: true
         }
     })
+}
+
+export async function getHistoryById (historyId: number) : Promise<History> {
+    const history = await db.history.findFirst({
+        where:{
+            id: historyId
+        },
+        select:{
+            id: true,
+            user_id: true,
+            alamat_tujuan: true,
+            id_penerima: true,
+            nama_penerima: true,
+            biaya_pengiriman: true,
+            rating: true
+        }
+    })
+
+    if(history === null) {
+        throw "error";
+    }
+
+    return history;
 }
\ No newline at end of file
diff --git a/src/interfaces/History.ts b/src/interfaces/History.ts
index 3d85e7f..9b5ada7 100644
--- a/src/interfaces/History.ts
+++ b/src/interfaces/History.ts
@@ -1,8 +1,10 @@
 
 export interface History {
-    id: number,
-    user_id : number,
-    alamat_tujuan: string,
-    nama_penerima: string,
-    rating: number,
+    id: number
+    user_id : number
+    alamat_tujuan : string
+    id_penerima : number
+    nama_penerima : string
+    biaya_pengiriman : number
+    rating : number
 }
diff --git a/src/interfaces/HistoryDetail.ts b/src/interfaces/HistoryDetail.ts
index 6c769d4..0c5d4fa 100644
--- a/src/interfaces/HistoryDetail.ts
+++ b/src/interfaces/HistoryDetail.ts
@@ -1,7 +1,8 @@
 
 export interface HistoryDetail {
-    id: number,
-    history_id : number,
-    product_name: string,
-    quantity: number,
-}
\ No newline at end of file
+    id: number
+    history_id : number
+    product_name: string
+    quantity: number
+    price : number
+}
diff --git a/src/interfaces/User.ts b/src/interfaces/User.ts
index d95d488..bb4b95b 100644
--- a/src/interfaces/User.ts
+++ b/src/interfaces/User.ts
@@ -1,9 +1,9 @@
 
 export interface User {
-    id : number,
-    username : string,
-    name : string,
-    email : string,
-    password : string,
-    saldo : number,
+    id : number
+    username : string
+    name : string
+    email : string
+    password : string
+    saldo : number
 }
\ No newline at end of file
-- 
GitLab