diff --git a/prisma/migrations/20231121091000_typechange/migration.sql b/prisma/migrations/20231121091000_typechange/migration.sql new file mode 100644 index 0000000000000000000000000000000000000000..6b2798b8df4ca0eaa892ddbb96ec26238f2ba5b9 --- /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 8055ecee520ef6d2a7ec029670a26152b4456a62..1b14b6562115badfb054f0c8310d439f39fb955f 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 0000000000000000000000000000000000000000..c370f5d6979172651794efcbc036e9dd9688ae9e --- /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 d3c82f07e03ebec2ddc718841aef4272d0135ddb..8856e6727c74db75045b29518069d6bc4f73e895 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 0000000000000000000000000000000000000000..3d0da3a629f999ecf37c2ef4f7dac3fdcb50368f --- /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 335862a65f47d62c61ca966910b112b3f49da52c..a0c2a03cc01501a00827d3aed8c8e89bc841b0b2 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