From 829f8dc57b24f222a9c699bffe7c4bc87c12ac98 Mon Sep 17 00:00:00 2001 From: DewanaGustavus <76590469+DewanaGustavus@users.noreply.github.com> Date: Fri, 17 Nov 2023 02:14:23 +0700 Subject: [PATCH] fix: edit some history service code --- src/handler/history/history.router.ts | 6 +++--- src/handler/history/history.service.ts | 7 ++++--- src/utils/getUserId.ts | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/utils/getUserId.ts diff --git a/src/handler/history/history.router.ts b/src/handler/history/history.router.ts index 4ec9397..a8a8dfa 100644 --- a/src/handler/history/history.router.ts +++ b/src/handler/history/history.router.ts @@ -5,16 +5,16 @@ import * as HistoryServices from './history.service' export const HistoryRouter = express.Router() -HistoryRouter.get('/', async (request: Request, response: Response) => { +HistoryRouter.get('/:username', async (request: Request, response: Response) => { try { - const history = await HistoryServices.getHistory(); + const history = await HistoryServices.getHistory(request.params.username); return response.status(200).json(history); } catch (error: any) { return response.status(500).json(error.message); } }) -HistoryRouter.get('/:id', async (request: Request, response: Response) => { +HistoryRouter.get('/detail/:id', async (request: Request, response: Response) => { const id: number = parseInt(request.params.id, 10); try { const history = await HistoryServices.getHistoryDetail(id); diff --git a/src/handler/history/history.service.ts b/src/handler/history/history.service.ts index 357001a..5ffb5d6 100644 --- a/src/handler/history/history.service.ts +++ b/src/handler/history/history.service.ts @@ -1,12 +1,13 @@ import { History } from "../../interfaces/History"; import { HistoryDetail } from "../../interfaces/HistoryDetail"; import {db} from "../../utils/db.server" +import { getUserId } from "../../utils/getUserId"; -export async function getHistory () : Promise<History[]> { - const user_id = 2; //ini buat temp aja karena auth belom dibuat +export async function getHistory (username : string) : Promise<History[]> { + const userId = await getUserId(username); return db.history.findMany({ where:{ - user_id : user_id, + user_id : userId, }, select: { id: true, diff --git a/src/utils/getUserId.ts b/src/utils/getUserId.ts new file mode 100644 index 0000000..a9ca97e --- /dev/null +++ b/src/utils/getUserId.ts @@ -0,0 +1,15 @@ +import {db} from "./db.server" + +export async function getUserId(username : string) { + const result = await db.user.findFirst({ + where: { + username : username + }, + select: { + id : true + } + }); + const userId = result ? result.id : 0; + + return userId; +} \ No newline at end of file -- GitLab