diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 64b09b7280d011a6691acef763470470625d1427..4dcf1ebcf96c1b4dd6e06a4e9fb16e189ac989e9 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 f86f9b2308ba6760f394e2bd9be7519ad1675642..191ce24158eef59d2aad30a49f284f4acf19cfea 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