first commit

This commit is contained in:
kicap1992
2022-04-27 01:19:12 +08:00
parent 1ac188d93a
commit c3d0676355
38 changed files with 11606 additions and 1486 deletions

View File

@ -0,0 +1,211 @@
const express = require('express');
const router = express.Router()
const db = require('../database/index.js')
const tb_admin = db.admin
const tb_dokter = db.dokter
// const tb_login = db.login
const tb_tindakan = db.tindakan
const tb_obat = db.obat
const Op = db.Sequelize.Op
const app = require('express')()
const basicAuth = require('express-basic-auth')
const basicAuthMiddleware = basicAuth({
users: { 'kicapkaran_admin': 'karan456_admin' },
challenge: true,
unauthorizedResponse: getUnauthenticatedResponse
})
function getUnauthenticatedResponse(req) {
const { user } = req.auth?.user ?? {}
return user ? `invalid credentials for user '${user}'` : 'no credentials provided';
}
// create / get request
router.get('/', basicAuthMiddleware, async (req, res) => {
res.send({ status: true, message: 'connected to admin' })
})
// create /tindakan post request
router.post('/tindakan', basicAuthMiddleware, async (req, res) => {
console.log("sini untuk tambah tindakan")
try {
const tindakan = req.body.tindakan
let cek_tindakan = await tb_tindakan.findOne({
where: {
nama_tindakan: tindakan
}
})
// if()
if (cek_tindakan) {
return res.status(400).send({ status: false, message: `Tindakan ${tindakan} sudah ada` })
}
// create tindakan
const new_tindakan = await tb_tindakan.create({
nama_tindakan: tindakan
})
res.status(200).send({ status: true, message: `Tindakan ${tindakan} berhasil ditambahkan` })
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
// create /tindaakan get request
router.get('/tindakan', basicAuthMiddleware, async (req, res) => {
console.log("sini untuk tindakan get")
try {
console.log("ambil all tinakan")
let tindakan = await tb_tindakan.findAll()
res.status(200).send({ status: true, data: tindakan })
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
// create /tindakan delete request
router.delete('/tindakan', basicAuthMiddleware, async (req, res) => {
console.log("sini untuk tindakan delete")
try {
const id = req.query.id
// console.log(id, "ini idnya di delete")
let tindakan = await tb_tindakan.findOne({
where: {
id_tindakan: id
}
})
if (!tindakan) {
return res.status(400).send({ status: false, message: `Tindakan dengan id ${id} tidak ditemukan` })
}
await tindakan.destroy()
res.status(200).send({ status: true, message: `Tindakan dengan id ${id} berhasil dihapus` })
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
// create /obat post request
router.post('/obat', basicAuthMiddleware, async (req, res) => {
console.log("sini untuk obat post")
try {
const obat = req.body.obat
let cek_obat = await tb_obat.findOne({
where: {
nama_obat: obat
}
})
if (cek_obat) {
return res.status(400).send({ status: false, message: `Obat ${obat} sudah ada` })
}
// create obat
const new_obat = await tb_obat.create({
nama_obat: obat,
jenis: req.body.jenis,
jumlah: req.body.jumlah,
harga: req.body.harga,
history: JSON.stringify(req.body.history)
})
res.status(200).send({ status: true, message: `Obat ${obat} berhasil ditambahkan` })
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
// create /obat get request
router.get('/obat', basicAuthMiddleware, async (req, res) => {
// console.log("sini untuk obat get")
try {
let id = req.query.id
// if id is not null
if (id) {
let obat = await tb_obat.findOne({
where: {
id_obat: id
}
})
if (!obat) {
return res.status(400).send({ status: false, message: `Obat dengan id ${id} tidak ditemukan` })
}
obat = JSON.parse(obat.dataValues.history)
res.status(200).send({ status: true, data: obat})
} else {
// console.log("ambil all obat")
let obat = await tb_obat.findAll()
res.status(200).send({ status: true, data: obat })
}
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
// create /obat put request
router.put('/obat', basicAuthMiddleware, async (req, res) => {
console.log("sini untuk obat put")
try {
const id = req.query.id
const detail = req.query.detail
let obat = await tb_obat.findOne({
where: {
id_obat: id
}
})
if (!obat) {
return res.status(400).send({ status: false, message: `Obat dengan id ${id} tidak ditemukan` })
}
if (!detail || detail != "edit_data") {
return res.status(400).send({ status: false, message: `Detail Tidak Diketahui` })
}
// get the history from obat
let history = JSON.parse(obat.dataValues.history)
// add the req.body.history to the history
history.push(req.body.history)
// console.log(history)
await obat.update({
nama_obat: req.body.obat,
jenis: req.body.jenis,
jumlah: req.body.jumlah,
harga: req.body.harga,
history: JSON.stringify(history)
})
res.status(200).send({ status: true, message: `Obat dengan id ${id} berhasil diupdate` })
} catch (err) {
console.log(err)
res.status(500).send({ status: false, message: "internal server error" })
}
})
module.exports = router

View File

@ -0,0 +1,119 @@
// create express router
const express = require('express');
const router = express.Router()
const db = require('../database/index.js')
const tb_admin = db.admin
const tb_dokter = db.dokter
const tb_login = db.login
const Op = db.Sequelize.Op
var ironSession = require("iron-session/express").ironSession;
var session = ironSession({
cookieName: "myapp_cookiename",
// password: process.env.SECRET_COOKIE_PASSWORD,
password: 'complex_password_at_least_32_characters_long',
cookieOptions: {
secure: process.env.NODE_ENV === "production",
},
});
// create get request
// router.get('/', async (req, res) => {
// try {
// const admin = {
// nik: 214280224,
// nama: "aran",
// role: 'admin'
// }
// // find if admin with nik exists
// const admin_exist = await tb_admin.findByPk(admin.nik)
// console.log(admin_exist);
// // if admin exist
// if (admin_exist) {
// return res.status(400).send({status: false, message: `Admin dengan nik ${admin.nik} sudah ada`})
// }
// after_create_admin = await tb_admin.create(admin)
// const login = {
// username: 'kicap92',
// password: '5c188ab394811451656f8c7f33680127',
// role: 'admin',
// id_admin: 214280224
// }
// await tb_login.create(login)
// res.send({ status: true, message: "ini login get router" })
// } catch (error) {
// res.status(500).send({ status: false, message: error.message })
// }
// })
// create get request
router.get('/', session, async (req, res) => {
try {
const username = req.query.username
const password = req.query.password
const role = req.query.role
// console.log(username, password, role);
let cek_login = await tb_login.findOne({
where: {
username: username,
password: password,
role: role
}
})
// console.log(cek_login);
if (!cek_login) {
return res.status(400).send({ status: false, message: 'Username dan password salah' })
}
let cek_data;
if (role == 'Admin') {
cek_data = await tb_admin.findOne({
where: {
nik: cek_login.id_admin
}
})
} else if (role == 'Dokter') {
cek_data = await tb_dokter.findOne({
where: {
nik: cek_login.id_dokter
}
})
}
// add role to cek_data
// cek_data.role = role;
cek_data = cek_data.dataValues;
// console.log(req.session.user);
// session data
req.session.user = {
username: username,
password: password,
role: role,
nik: cek_data.nik,
};
await req.session.save();
res.send({ status: true, message: "ini login post router", data: cek_data })
} catch (error) {
res.status(500).send({ status: false, message: error.message })
}
})
module.exports = router