forked from cyclic-software/express-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cyclic-software#7 from Capstone-Bangkit/CRUD-second
Crud second
- Loading branch information
Showing
10 changed files
with
437 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//POG | ||
import Keuangan from "../models/keuangan.js"; | ||
|
||
export const postKeuangan = async (req, res) => { | ||
const { | ||
tanggal, | ||
kegiatan, | ||
jenis, | ||
catatan, | ||
jumlah, | ||
} = req.body; | ||
|
||
const KeuanganPost = new Keuangan({ | ||
tanggal: tanggal, | ||
kegiatan: kegiatan, | ||
jenis: jenis, | ||
catatan: catatan, | ||
jumlah: jumlah, | ||
}); | ||
|
||
try { | ||
const keuangan = await KeuanganPost.save(); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil membuat Keuangan baru', | ||
data: keuangan | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal membuat Keuangan baru' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getKeuangan = async (req, res) => { | ||
try { | ||
const keuangan = await Keuangan.findAll() | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Keuangan', | ||
data: keuangan | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Keuangan' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getKeuanganById = async (req, res) => { | ||
try { | ||
const keuangan = await Keuangan.findOne({ | ||
where: { | ||
id_keuangan: req.params.id_keuangan, | ||
} | ||
}) | ||
if (keuangan === null) return error | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Keuangan', | ||
data: keuangan | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Keuangan' | ||
}) | ||
}; | ||
} | ||
|
||
export const updateKeuangan = async (req, res) => { | ||
try { | ||
const updateKeuangan = await Keuangan.update({ | ||
tanggal: req.body.tanggal, | ||
kegiatan: req.body.kegiatan, | ||
jenis: req.body.jenis, | ||
catatan: req.body.catatan, | ||
jumlah: req.body.jumlah, | ||
},{ | ||
where:{ | ||
id_keuangan: req.params.id_keuangan | ||
} | ||
}); | ||
if (updateKeuangan == 0) return error | ||
const dataKeuangan = await Keuangan.findOne({ | ||
where: { | ||
id_keuangan: req.params.id_keuangan, | ||
} | ||
}) | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil memperbarui Keuangan', | ||
data: dataKeuangan | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal memperbarui Keuangan' | ||
}) | ||
} | ||
} | ||
|
||
export const deleteKeuangan = async (req, res) => { | ||
try { | ||
const deleteKeuangan = await Keuangan.destroy({ | ||
where: { | ||
id_keuangan: req.params.id_keuangan | ||
} | ||
}); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil menghapus Keuangan' | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal menghapus Keuangan' | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
//POG | ||
import Penyakit from "../models/penyakit.js"; | ||
import path from "path" | ||
import fs from "fs" | ||
|
||
export const postPenyakit = async (req, res) => { | ||
const { | ||
nama, | ||
indikasi, | ||
tanggal, | ||
latitude, | ||
longitude, | ||
deskripsi, | ||
} = req.body; | ||
if (req.files === null) return res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Tidak ada file', | ||
}) | ||
const file = req.files.file | ||
const fileSize = file.data.length | ||
const ext = path.extname(file.name) | ||
const fileName = file.md5 + ext | ||
const url = `${req.protocol}://${req.get("host")}/public/images/${fileName}` | ||
const allowedType = ['.png', '.jpg', '.jpeg'] | ||
|
||
if (!allowedType.includes(ext.toLowerCase())) return res.status(422).json({ | ||
status: res.statusCode, | ||
message: 'invalid images', | ||
}) | ||
if (fileSize > 5000000) return res.status(422).json({ | ||
status: res.statusCode, | ||
message: 'Image must be less than 5 MB', | ||
}) | ||
|
||
file.mv(`./public/images/${fileName}`, async (err) => { | ||
if (err) return res.status(500).json({ | ||
status: res.statusCode, | ||
message: 'invalid images', | ||
}) | ||
try { | ||
const penyakit = await Penyakit.create({ | ||
nama: nama, | ||
image: fileName, | ||
url: url, | ||
indikasi: indikasi, | ||
tanggal: tanggal, | ||
latitude: latitude, | ||
longitude: longitude, | ||
deskripsi: deskripsi, | ||
}) | ||
res.status(201).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil membuat Penyakit', | ||
data: penyakit | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal membuat Penyakit baru' | ||
}) | ||
} | ||
}) | ||
}; | ||
|
||
export const getPenyakit = async (req, res) => { | ||
try { | ||
const penyakit = await Penyakit.findAll() | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Penyakit', | ||
data: penyakit | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Penyakit' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getPenyakitById = async (req, res) => { | ||
try { | ||
const penyakit = await Penyakit.findOne({ | ||
where: { | ||
id_penyakit: req.params.id_penyakit, | ||
} | ||
}) | ||
if (penyakit === null) return error | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Penyakit', | ||
data: penyakit | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Penyakit' | ||
}) | ||
}; | ||
} | ||
|
||
export const updatePenyakit = async (req, res) => { | ||
const searchpenyakit = await Penyakit.findOne({ | ||
where: { | ||
id_penyakit: req.params.id_penyakit | ||
} | ||
}); | ||
if (!searchpenyakit) return res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Penyakit tidak ditemukan' | ||
}) | ||
|
||
let fileName = ""; | ||
if (req.files === null) { | ||
fileName = searchpenyakit.image | ||
} else { | ||
const file = req.files.file | ||
const fileSize = file.data.length | ||
const ext = path.extname(file.name) | ||
fileName = file.md5 + ext | ||
const allowedType = ['.png', '.jpg', '.jpeg'] | ||
|
||
if (!allowedType.includes(ext.toLowerCase())) return res.status(422).json({ | ||
status: res.statusCode, | ||
message: 'invalid images', | ||
}) | ||
if (fileSize > 5000000) return res.status(422).json({ | ||
status: res.statusCode, | ||
message: 'Image must be less than 5 MB', | ||
}) | ||
|
||
const filePath = `./public/images/${searchpenyakit.image}` | ||
fs.unlinkSync(filePath) | ||
|
||
file.mv(`./public/images/${fileName}`, (err) => { | ||
if (err) return res.status(500).json({ | ||
status: res.statusCode, | ||
message: 'invalid images', | ||
}) | ||
}) | ||
} | ||
|
||
const { | ||
nama, | ||
indikasi, | ||
tanggal, | ||
latitude, | ||
longitude, | ||
deskripsi, | ||
} = req.body; | ||
const url = `${req.protocol}://${req.get("host")}/images/${fileName}` | ||
try { | ||
await Penyakit.update({ | ||
nama: nama, | ||
image: fileName, | ||
url: url, | ||
indikasi: indikasi, | ||
tanggal: tanggal, | ||
latitude: latitude, | ||
longitude: longitude, | ||
deskripsi: deskripsi, | ||
}, { | ||
where: { | ||
id_penyakit: req.params.id_penyakit | ||
} | ||
}) | ||
const updatedpenyakit = await Penyakit.findOne({ | ||
where: { | ||
id_penyakit: req.params.id_penyakit, | ||
} | ||
}) | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil memperbarui Penyakit', | ||
data: updatedpenyakit | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal memperbarui Penyakit' | ||
}) | ||
} | ||
} | ||
|
||
export const deletePenyakit = async (req, res) => { | ||
const penyakit = await Penyakit.findOne({ | ||
where: { | ||
id_penyakit: req.params.id_penyakit | ||
} | ||
}); | ||
if (!penyakit) return res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Penyakit tidak ditemukan' | ||
}) | ||
|
||
try { | ||
const filePath = `./public/images/${penyakit.image}` | ||
fs.unlinkSync(filePath) | ||
await Penyakit.destroy({ | ||
where: { | ||
id_penyakit: req.params.id_penyakit | ||
} | ||
}); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil menghapus Penyakit' | ||
}) | ||
} catch (err) { | ||
res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Gagal menghapus Penyakit' | ||
}) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.