From 1ec3d2f62584499f0a474dfa46cea0d784b7b699 Mon Sep 17 00:00:00 2001
From: kinanasih <18221044@std.stei.itb.ac.id>
Date: Wed, 22 Nov 2023 10:23:06 +0700
Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=9FFEAT=20:=20Added=20Backend=20for=20?=
 =?UTF-8?q?Student,=20Schedule,=20Ins?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../20231121091000_typechange/migration.sql   | 14 ++++
 prisma/schema.prisma                          |  6 +-
 src/controllers/schedController.ts            | 77 +++++++++++++++++
 src/controllers/studController.ts             |  8 +-
 src/controllers/studschedController.ts        | 83 +++++++++++++++++++
 src/index.ts                                  |  6 +-
 6 files changed, 186 insertions(+), 8 deletions(-)
 create mode 100644 prisma/migrations/20231121091000_typechange/migration.sql
 create mode 100644 src/controllers/schedController.ts
 create mode 100644 src/controllers/studschedController.ts

diff --git a/prisma/migrations/20231121091000_typechange/migration.sql b/prisma/migrations/20231121091000_typechange/migration.sql
new file mode 100644
index 0000000..6b2798b
--- /dev/null
+++ b/prisma/migrations/20231121091000_typechange/migration.sql
@@ -0,0 +1,14 @@
+/*
+  Warnings:
+
+  - You are about to drop the column `umurSiswaa` on the `Student` table. All the data in the column will be lost.
+  - Added the required column `umurSiswa` to the `Student` table without a default value. This is not possible if the table is not empty.
+
+*/
+-- AlterTable
+ALTER TABLE "Schedule" ALTER COLUMN "jamMulai" SET DATA TYPE TIME,
+ALTER COLUMN "jamSelesai" SET DATA TYPE TIME;
+
+-- AlterTable
+ALTER TABLE "Student" DROP COLUMN "umurSiswaa",
+ADD COLUMN     "umurSiswa" INTEGER NOT NULL;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 8055ece..1b14b65 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -21,7 +21,7 @@ model Student {
   idStudent    Int            @id @default(autoincrement())
   namaSiswa    String
   kelasSiswa   Int
-  umurSiswaa   Int
+  umurSiswa   Int
   noTelpSiswa  String
   alamatSiswa  String
   studentSched StudentSched[]
@@ -60,8 +60,8 @@ model Schedule {
   idJadwal        Int               @id @default(autoincrement())
   hari            String
   sesi            Int
-  jamMulai        DateTime          @db.Timestamp()
-  jamSelesai      DateTime          @db.Timestamp()
+  jamMulai        DateTime          @db.Time()
+  jamSelesai      DateTime          @db.Time()
   instructorSched InstructorSched[]
   studentSched    StudentSched[]
   vehicleSched    VehicleSched[]
diff --git a/src/controllers/schedController.ts b/src/controllers/schedController.ts
new file mode 100644
index 0000000..c370f5d
--- /dev/null
+++ b/src/controllers/schedController.ts
@@ -0,0 +1,77 @@
+import express, { Express } from "express"
+import { PrismaClient } from '@prisma/client'
+const route = express.Router(); 
+const prisma = new PrismaClient()
+
+route.get('/', async (req,res)=>{
+    const schedules = await prisma.schedule.findMany()
+    res.json(schedules)
+})
+
+route.get('/:id', async (req,res)=>{
+    const id = Number(req.params.id)
+    try{
+        const schedule = await prisma.schedule.findFirstOrThrow(
+            {
+                where: { idJadwal: id}
+            }
+        )
+        res.json(schedule)
+    }catch(error){
+        res.json("Jadwal tidak ditemukan!")
+    }
+})
+
+route.post('/', async (req, res)=>{
+    const { hari,sesi,jamMulai,jamSelesai } = req.body
+    try {
+        const CreateSchedule = await prisma.schedule.create({
+            data : {
+                hari,
+                sesi,
+                jamMulai,
+                jamSelesai
+            }
+        })
+        res.json(CreateSchedule)
+    } catch (error){
+        res.json("Gagal Membuat Schedule")
+    }
+})
+
+route.put('/:id', async (req, res)=>{
+    const id = Number(req.params.id)
+    const { hari,sesi,jamMulai,jamSelesai } = req.body
+    try {
+        const updateSchedule = await prisma.schedule.update({
+            where: {
+                idJadwal: id
+            },
+            data : {
+                hari,
+                sesi,
+                jamMulai,
+                jamSelesai
+            }
+        })
+        res.json(updateSchedule)
+    } catch {
+        res.json("Gagal Update Schedule")
+    }
+})
+
+route.delete('/:id', async (req, res)=>{
+    const id = Number(req.params.id)
+    try {
+        const deleteSchedule = await prisma.schedule.delete({
+            where : {
+                idJadwal : id
+            }
+        })
+        res.json(deleteSchedule)
+    } catch (error){
+        res.json("Gagal Hapus Schedule")
+    }
+})
+
+export default route; 
\ No newline at end of file
diff --git a/src/controllers/studController.ts b/src/controllers/studController.ts
index d3c82f0..8856e67 100644
--- a/src/controllers/studController.ts
+++ b/src/controllers/studController.ts
@@ -23,13 +23,13 @@ route.get('/:id', async (req,res)=>{
 })
 
 route.post('/', async (req, res)=>{
-    const { namaSiswa,kelasSiswa,umurSiswaa,noTelpSiswa,alamatSiswa } = req.body
+    const { namaSiswa,kelasSiswa,umurSiswa,noTelpSiswa,alamatSiswa } = req.body
     try {
         const CreateStudent = await prisma.student.create({
             data : {
                 namaSiswa,
                 kelasSiswa, 
-                umurSiswaa,
+                umurSiswa,
                 noTelpSiswa,
                 alamatSiswa
             }
@@ -42,7 +42,7 @@ route.post('/', async (req, res)=>{
 
 route.put('/:id', async (req, res)=>{
     const id = Number(req.params.id)
-    const { namaSiswa,kelasSiswa,umurSiswaa,noTelpSiswa,alamatSiswa } = req.body
+    const { namaSiswa,kelasSiswa,umurSiswa,noTelpSiswa,alamatSiswa } = req.body
     try {
         const updateStudent = await prisma.student.update({
             where: {
@@ -51,7 +51,7 @@ route.put('/:id', async (req, res)=>{
             data : {
                 namaSiswa,
                 kelasSiswa, 
-                umurSiswaa,
+                umurSiswa,
                 noTelpSiswa,
                 alamatSiswa
             }
diff --git a/src/controllers/studschedController.ts b/src/controllers/studschedController.ts
new file mode 100644
index 0000000..3d0da3a
--- /dev/null
+++ b/src/controllers/studschedController.ts
@@ -0,0 +1,83 @@
+import express, { Express } from "express"
+import { PrismaClient } from '@prisma/client'
+const route = express.Router(); 
+const prisma = new PrismaClient()
+
+route.get('/', async (req,res)=>{
+    const studschedules = await prisma.studentSched.findMany()
+    res.json(studschedules)
+})
+
+route.get('/:id', async (req,res)=>{
+    const id = Number(req.params.id)
+    try{
+        const studschedule = await prisma.studentSched.findFirstOrThrow(
+            {
+                where: { idStudent: id}
+            }
+        )
+        res.json(studschedule)
+    }catch(error){
+        res.json("Jadwal siswa tidak ditemukan!")
+    }
+})
+
+route.post('/', async (req, res)=>{
+    const { idStudent,idJadwal } = req.body
+    try {
+        const CreateStudSchedule = await prisma.studentSched.create({
+            data : {
+                idStudent,
+                idJadwal
+            }
+        })
+        res.json(CreateStudSchedule)
+    } catch (error){
+        res.json("Gagal Membuat Jadwal Siswa")
+    }
+})
+
+route.put('/:idStud/:idJad', async (req, res) => {
+    const idStud = Number(req.params.idStud)
+    const idJad = Number(req.params.idJad)
+    const { idStudent,idJadwal } = req.body;
+  
+    try {
+      const updateStudSchedule = await prisma.studentSched.update({
+        where: {
+          idStudent_idJadwal: {
+            idStudent: idStud,
+            idJadwal: idJad
+          },
+        },
+        data: {
+            idStudent,
+            idJadwal
+        },
+      });
+      res.json(updateStudSchedule);
+    } catch (error) {
+      res.json('Gagal Update Jadwal Siswa');
+    }
+});
+  
+
+route.delete('/:idStud/:idJad', async (req, res)=>{
+    const idStud = Number(req.params.idStud)
+    const idJad = Number(req.params.idJad)
+    try {
+        const deleteStudSchedule = await prisma.studentSched.delete({
+            where: {
+                idStudent_idJadwal: {
+                  idStudent: idStud,
+                  idJadwal: idJad
+                },
+            },
+        })
+        res.json(deleteStudSchedule)
+    } catch (error){
+        res.json("Gagal Hapus Jadwal Siswa")
+    }
+})
+
+export default route; 
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index 335862a..a0c2a03 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -4,6 +4,8 @@ const port = process.env.PORT || 5000;
 import userRoutes from './controllers/userController';
 import studentRoutes from './controllers/studController';
 import instructorRoutes from './controllers/insController';
+import scheduleRoutes from './controllers/schedController';
+import studscheduleRoutes from './controllers/studschedController';
 
 app.listen(port, () =>{
     console.log(`Server is running on port : ${port}`)
@@ -12,4 +14,6 @@ app.listen(port, () =>{
 app.use(express.json())
 app.use('/user', userRoutes)
 app.use('/student', studentRoutes)
-app.use('/instructor', instructorRoutes)
\ No newline at end of file
+app.use('/instructor', instructorRoutes)
+app.use('/schedule', scheduleRoutes)
+app.use('/studschedule', studscheduleRoutes)
\ No newline at end of file
-- 
GitLab