From 629389cdea319d7c5ce231e53801290d366b61ad Mon Sep 17 00:00:00 2001
From: Aulianyb <18221066@std.stei.itb.ac.id>
Date: Sun, 19 Nov 2023 21:18:34 +0700
Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=9FFEAT=20:=20BE=20User=20CRUD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 prisma/schema.prisma              | 18 ++++----
 src/controllers/userController.ts | 72 ++++++++++++++++++++++++++-----
 2 files changed, 71 insertions(+), 19 deletions(-)

diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 64b09b7..4dcf1eb 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,18 +10,18 @@ datasource db {
   url      = env("DATABASE_URL")
 }
 
-model User{
-  idUser Int @id @default(autoincrement())
-  username String
+model User {
+  idUser   Int    @id @default(autoincrement())
+  username String @unique
   password String
-  status String
+  status   String
 }
 
-model Student{
-  idStudent Int @id @default(autoincrement())
-  namaSiswa String
-  kelasSiswa Int
-  umurSiswaa Int
+model Student {
+  idStudent   Int    @id @default(autoincrement())
+  namaSiswa   String
+  kelasSiswa  Int
+  umurSiswaa  Int
   noTelpSiswa String
   alamatSiswa String
 }
diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts
index f86f9b2..191ce24 100644
--- a/src/controllers/userController.ts
+++ b/src/controllers/userController.ts
@@ -4,21 +4,73 @@ const route = express.Router();
 const port = 3000
 const prisma = new PrismaClient()
 
-route.get('/users', async (req,res)=>{
+route.get('/user', async (req,res)=>{
     const users = await prisma.user.findMany()
     res.json(users)
 })
 
+route.get('/user/:id', async (req,res)=>{
+    const id = Number(req.params.id)
+    try{
+        const user = await prisma.user.findFirstOrThrow(
+            {
+                where: { idUser: id}
+            }
+        )
+        res.json(user)
+    }catch(error){
+        res.json("User tidak ditemukan!")
+    }
+})
+
 route.post('/user', async (req, res)=>{
-    const { username, password, status} = req.body
-    const user = await prisma.user.create({
-        data : {
-            username,
-            password, 
-            status
-        }
-    })
-    res.json(user)
+    const { username, password, status } = req.body
+    try {
+        const CreateUser = await prisma.user.create({
+            data : {
+                username,
+                password, 
+                status
+            }
+        })
+        res.json(CreateUser)
+    } catch (error){
+        res.json("Gagal Membuat User")
+    }
+})
+
+route.put('/user/:id', async (req, res)=>{
+    const id = Number(req.params.id)
+    const { username, password, status } = req.body
+    try {
+        const updateUser = await prisma.user.update({
+            where: {
+                idUser : id
+            },
+            data : {
+                username : username,
+                password : password, 
+                status : status
+            }
+        })
+        res.json(updateUser)
+    } catch {
+        res.json("Gagal Update User")
+    }
+})
+
+route.delete('/user/:id', async (req, res)=>{
+    const id = Number(req.params.id)
+    try {
+        const deleteUser = await prisma.user.delete({
+            where : {
+                idUser : id
+            }
+        })
+        res.json(deleteUser)
+    } catch (error){
+        res.json("Gagal Hapus User")
+    }
 })
 
 export default route; 
\ No newline at end of file
-- 
GitLab