first commit
This commit is contained in:
58
routes/first_router.ts
Normal file
58
routes/first_router.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import express from 'express';
|
||||
import type { Request, Response } from 'express';
|
||||
import type { UploadedFile } from 'express-fileupload';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import axios from 'axios';
|
||||
|
||||
// import model
|
||||
import { SiswaModel } from '../models/siswa_model'; // import SiswaModel
|
||||
import { GuruModel } from '../models/guru_model';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/first_pages/index.html'));
|
||||
});
|
||||
|
||||
router.get('/login', (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/first_pages/login.html'));
|
||||
});
|
||||
|
||||
router.post('/login', async (req: Request, res: Response) => {
|
||||
const { username, password, role } = req.body;
|
||||
// console.log(username, password, role);
|
||||
if (role == 'guru') {
|
||||
let cek_guru = await GuruModel.findOne({ nig: username , password: password })!;
|
||||
|
||||
// console.log(cek_guru.nama || 'gagal');
|
||||
|
||||
if (cek_guru) {
|
||||
// console.log(cek_guru.nama);
|
||||
cek_guru = JSON.parse(JSON.stringify(cek_guru));
|
||||
const responseData = { ...cek_guru, role: 'guru' };
|
||||
console.log(responseData.nama);
|
||||
return res.status(200).json({data : responseData , message: "Login Berhasil , <br>Selamat Datang " + responseData.nama});
|
||||
}
|
||||
|
||||
return res.status(401).json('Login Gagal , Username dan Password Salah' );
|
||||
}
|
||||
|
||||
if (role == 'siswa') {
|
||||
// check the SiswaModel
|
||||
let siswa = await SiswaModel.findOne({ nis: username });
|
||||
|
||||
// change the siswa to json
|
||||
siswa = JSON.parse(JSON.stringify(siswa));
|
||||
if (siswa && siswa.password == password) {
|
||||
const responseData = { ...siswa, role: 'siswa' };
|
||||
return res.status(200).json({data : responseData , message: "Login Berhasil , <br>Selamat Datang " + responseData.nama});
|
||||
}
|
||||
return res.status(401).json('Login Gagal , Username dan Password Salah' );
|
||||
}
|
||||
res.status(200).json('Login Gagal , Username dan Password Salah' );
|
||||
});
|
||||
|
||||
|
||||
|
||||
export default router;
|
||||
128
routes/guru_router.ts
Normal file
128
routes/guru_router.ts
Normal file
@ -0,0 +1,128 @@
|
||||
import express from 'express';
|
||||
import type { Request, Response } from 'express';
|
||||
import type { UploadedFile } from 'express-fileupload';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import axios from 'axios';
|
||||
|
||||
// import model
|
||||
import { GuruModel } from '../models/guru_model'; // import GuruModel
|
||||
|
||||
// Function to calculate time conditions
|
||||
interface TimeCheckResult {
|
||||
isBefore: boolean;
|
||||
isWithin: boolean;
|
||||
isAfter: boolean;
|
||||
secondsLeft?: number; // Optional, only present if isWithin is true
|
||||
}
|
||||
|
||||
function checkTimeRange(
|
||||
jam_mulai: string,
|
||||
jam_selesai: string,
|
||||
tanggal_kerja: Date
|
||||
): TimeCheckResult {
|
||||
const now = new Date(); // Current date and time: 01:19 AM WITA on October 08, 2025
|
||||
const currentDateTime = new Date(now);
|
||||
|
||||
// Create start and end Date objects
|
||||
const startDateTime = new Date(tanggal_kerja);
|
||||
[startDateTime.setHours(Number(jam_mulai.split(':')[0])), startDateTime.setMinutes(Number(jam_mulai.split(':')[1]))];
|
||||
|
||||
const endDateTime = new Date(tanggal_kerja);
|
||||
[endDateTime.setHours(Number(jam_selesai.split(':')[0])), endDateTime.setMinutes(Number(jam_selesai.split(':')[1]))];
|
||||
|
||||
// Check conditions
|
||||
const isBefore = currentDateTime < startDateTime;
|
||||
const isWithin = currentDateTime >= startDateTime && currentDateTime <= endDateTime;
|
||||
const isAfter = currentDateTime > endDateTime;
|
||||
|
||||
// Calculate seconds left if within range
|
||||
let secondsLeft: number | undefined;
|
||||
if (isWithin) {
|
||||
const timeDiff = endDateTime.getTime() - currentDateTime.getTime();
|
||||
secondsLeft = Math.max(0, Math.floor(timeDiff / 1000)); // Ensure non-negative
|
||||
}
|
||||
|
||||
return {
|
||||
isBefore,
|
||||
isWithin,
|
||||
isAfter,
|
||||
...(secondsLeft !== undefined && { secondsLeft }) // Only include secondsLeft if defined
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/guru_pages/index.html'));
|
||||
});
|
||||
|
||||
router.post('/check', async (req: Request, res: Response) => {
|
||||
const data = req.body;
|
||||
// console.log(data);
|
||||
try {
|
||||
// find on SiswaModel by data because the data container all the data
|
||||
const siswa = await GuruModel.findOne({ nig: data.nig, password: data.password, _id: data._id, no_telpon: data.no_telpon, jk: data.jk });
|
||||
console.log("ada data");
|
||||
return res.status(200).json("data found");
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(400).json("data not found");
|
||||
}
|
||||
// res.status(200).json("data not found");
|
||||
})
|
||||
|
||||
router.get('/quiz', async (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/guru_pages/quiz.html'));
|
||||
})
|
||||
|
||||
router.get('/tambah_soal', async (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/guru_pages/tambah_soal.html'));
|
||||
})
|
||||
|
||||
|
||||
router.get('/check_status_soal/:soal', async (req: Request, res: Response) => {
|
||||
const soal = req.params.soal;
|
||||
if (soal == "soal1") {
|
||||
const result = checkTimeRange("8:50", "10:10", new Date("10-15-2025"));
|
||||
|
||||
if (result.isBefore)
|
||||
return res.status(200).json({ label: "default", status: "Belum Waktunya", bool: true });
|
||||
if (result.isAfter)
|
||||
return res.status(200).json({ label: "default", status: "Sudah Waktunya", bool: true });
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/soal1', async (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/guru_pages/soal1.html'));
|
||||
})
|
||||
|
||||
|
||||
router.post('/tambah_data_guru', async (req: Request, res: Response) => {
|
||||
const data = {
|
||||
"nig": "-",
|
||||
"nama": "-",
|
||||
"jk": "Perempuan",
|
||||
"no_telpon": "-",
|
||||
"password": "12345678",
|
||||
"listmatapelajaran": [
|
||||
{
|
||||
"matpel": "Informatika"
|
||||
},
|
||||
{
|
||||
"matpel": "Bahasa Indonesia"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const result = await GuruModel.create(data);
|
||||
console.log(result);
|
||||
res.status(200).json(result);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
export default router;
|
||||
198
routes/siswa_router.ts
Normal file
198
routes/siswa_router.ts
Normal file
@ -0,0 +1,198 @@
|
||||
import express from 'express';
|
||||
import type { Request, Response } from 'express';
|
||||
// di bawah import saja
|
||||
import type { UploadedFile } from 'express-fileupload';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import axios from 'axios';
|
||||
|
||||
// dibawah import model
|
||||
import { SiswaModel } from '../models/siswa_model'; // import SiswaModel
|
||||
import { JawabanSoalModel } from '../models/jawaban_model'; // import JawabanSoalModel
|
||||
|
||||
|
||||
// Function to calculate time conditions
|
||||
interface TimeCheckResult {
|
||||
isBefore: boolean;
|
||||
isWithin: boolean;
|
||||
isAfter: boolean;
|
||||
secondsLeft?: number; // Optional, only present if isWithin is true
|
||||
}
|
||||
|
||||
function checkTimeRange(
|
||||
jam_mulai: string,
|
||||
jam_selesai: string,
|
||||
tanggal_kerja: Date
|
||||
): TimeCheckResult {
|
||||
const now = new Date(); // Current date and time: 01:19 AM WITA on October 08, 2025
|
||||
const currentDateTime = new Date(now);
|
||||
|
||||
// Create start and end Date objects
|
||||
const startDateTime = new Date(tanggal_kerja);
|
||||
[startDateTime.setHours(Number(jam_mulai.split(':')[0])), startDateTime.setMinutes(Number(jam_mulai.split(':')[1]))];
|
||||
|
||||
const endDateTime = new Date(tanggal_kerja);
|
||||
[endDateTime.setHours(Number(jam_selesai.split(':')[0])), endDateTime.setMinutes(Number(jam_selesai.split(':')[1]))];
|
||||
|
||||
// Check conditions
|
||||
const isBefore = currentDateTime < startDateTime;
|
||||
const isWithin = currentDateTime >= startDateTime && currentDateTime <= endDateTime;
|
||||
const isAfter = currentDateTime > endDateTime;
|
||||
|
||||
// Calculate seconds left if within range
|
||||
let secondsLeft: number | undefined;
|
||||
if (isWithin) {
|
||||
const timeDiff = endDateTime.getTime() - currentDateTime.getTime();
|
||||
secondsLeft = Math.max(0, Math.floor(timeDiff / 1000)); // Ensure non-negative
|
||||
}
|
||||
|
||||
return {
|
||||
isBefore,
|
||||
isWithin,
|
||||
isAfter,
|
||||
...(secondsLeft !== undefined && { secondsLeft }) // Only include secondsLeft if defined
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/siswa_pages/index.html'));
|
||||
});
|
||||
|
||||
router.post('/check', async (req: Request, res: Response) => {
|
||||
const data = req.body;
|
||||
// console.log(data);
|
||||
try {
|
||||
// find on SiswaModel by data because the data container all the data
|
||||
const siswa = await SiswaModel.findOne({ nis: data.nis, password: data.password, _id: data._id, kelas: data.kelas, jurusan: data.jurusan, jk: data.jk });
|
||||
console.log("ada data");
|
||||
return res.status(200).json("data found");
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(400).json("data not found");
|
||||
}
|
||||
// res.status(200).json("data not found");
|
||||
})
|
||||
|
||||
router.get('/quiz', (req: Request, res: Response) => {
|
||||
res.sendFile(path.join(__dirname, '../ui/siswa_pages/quiz.html'));
|
||||
})
|
||||
|
||||
|
||||
router.post('/submit-answers/:quiz', async (req: Request, res: Response) => {
|
||||
const { quiz } = req.params;
|
||||
const { _id, nis, jawaban } = req.body;
|
||||
console.log(_id, nis, jawaban);
|
||||
console.log(quiz);
|
||||
|
||||
try {
|
||||
// check if jawaban of this soal and this nis already exist
|
||||
const jawabanExist = await JawabanSoalModel.findOne({ nis: nis, soal: quiz });
|
||||
if (jawabanExist) {
|
||||
return res.status(400).json('Jawaban Sudah di Kirim');
|
||||
}
|
||||
await JawabanSoalModel.create({ nis: nis, jawaban: jawaban, soal: quiz, waktu: new Date() });
|
||||
res.status(200).json('Jawaban Berhasil di Kirim');
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(400).json('Jawaban Gagal di Kirim');
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
router.get('/check_status_list_soal/:quiz/:nis', async (req: Request, res: Response) => {
|
||||
const { quiz, nis } = req.params;
|
||||
// console.log(quiz, nis);
|
||||
|
||||
// check if jawaban of this soal and this nis already exist
|
||||
const jawabanExist = await JawabanSoalModel.findOne({ nis: nis, soal: quiz });
|
||||
if (jawabanExist) {
|
||||
return res.status(200).json({ label: "success", status: "Sudah Dijawab", bool: true });
|
||||
}
|
||||
|
||||
if (quiz == "soal1") {
|
||||
const result = checkTimeRange("8:50", "10:10", new Date("10-15-2025"));
|
||||
|
||||
if (result.isBefore)
|
||||
return res.status(200).json({ label: "default", status: "Belum Waktunya", bool: true });
|
||||
if (result.isAfter)
|
||||
return res.status(200).json({ label: "default", status: "Sudah Waktunya", bool: true });
|
||||
}
|
||||
res.status(200).json({ label: "info", status: "Belum Dijawab", bool: false });
|
||||
})
|
||||
|
||||
// ini soal1 hardcode
|
||||
router.get('/soal1', (req: Request, res: Response) => {
|
||||
|
||||
const result = checkTimeRange("8:50", "10:10", new Date("10-15-2025"));
|
||||
// console.log(result);
|
||||
|
||||
// if (result.isBefore)
|
||||
// return res.sendFile(path.join(__dirname, '../ui/first_pages/jam_belum.html'));
|
||||
|
||||
// if (result.isAfter)
|
||||
// return res.sendFile(path.join(__dirname, '../ui/first_pages/jam_sudah.html'));
|
||||
|
||||
|
||||
res.sendFile(path.join(__dirname, '../ui/siswa_pages/soal1.html'));
|
||||
});
|
||||
|
||||
router.get('/check_timer/:waktu_mulai/:waktu_selesai/:tanggal', async (req: Request, res: Response) => {
|
||||
const { waktu_mulai, waktu_selesai, tanggal } = req.params;
|
||||
|
||||
const result = checkTimeRange(waktu_mulai, waktu_selesai, new Date(tanggal));
|
||||
// console.log(result);
|
||||
res.status(200).json(result);
|
||||
})
|
||||
|
||||
|
||||
router.post('/check-jawaban/:quiz', async (req: Request, res: Response) => {
|
||||
const { quiz } = req.params;
|
||||
const { nis, _id } = req.body;
|
||||
|
||||
// check if jawaban of this soal and this nis already exist
|
||||
const jawabanExist = await JawabanSoalModel.findOne({ nis: nis, soal: quiz });
|
||||
if (jawabanExist) {
|
||||
return res.status(200).json(jawabanExist);
|
||||
}
|
||||
res.status(200).json({ label: "info", status: "Belum Dijawab", bool: false });
|
||||
// console.log(quiz, nis, _id);
|
||||
// res.status(200).json("data not found");
|
||||
})
|
||||
|
||||
router.post('/datasiswa', async (req: Request, res: Response) => {
|
||||
const jsonnya = {
|
||||
"kelas": "3",
|
||||
"jurusan": "Farmasi",
|
||||
"siswa": [
|
||||
{ "nis": "2023011", "nama": "Aril Ramadhan", "jk": "Laki-laki" },
|
||||
{ "nis": "2023013", "nama": "Erwin", "jk": "Laki-laki" },
|
||||
{ "nis": "2023005", "nama": "Mardiana", "jk": "Perempuan" },
|
||||
{ "nis": "2023002", "nama": "Muh. Baim", "jk": "Laki-laki" },
|
||||
{ "nis": "2023012", "nama": "Muh. Rezki R", "jk": "Laki-laki" },
|
||||
{ "nis": "2023007", "nama": "Nabila", "jk": "Perempuan" },
|
||||
{ "nis": "2023006", "nama": "Nasila", "jk": "Perempuan" },
|
||||
{ "nis": "2023009", "nama": "St. Aini", "jk": "Perempuan" },
|
||||
{ "nis": "2023010", "nama": "St. Fatimah", "jk": "Perempuan" },
|
||||
]
|
||||
}
|
||||
|
||||
// insert to siswa
|
||||
for (let i = 0; i < jsonnya.siswa.length; i++) {
|
||||
await SiswaModel.create({
|
||||
nis: jsonnya.siswa[i].nis,
|
||||
nama: jsonnya.siswa[i].nama,
|
||||
kelas: jsonnya.kelas,
|
||||
jurusan: jsonnya.jurusan,
|
||||
jk: jsonnya.siswa[i].jk,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json(jsonnya);
|
||||
});
|
||||
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user