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#6 from Capstone-Bangkit/CRUD
add crud for hasil panen and inventaris
- Loading branch information
Showing
14 changed files
with
492 additions
and
75 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,116 @@ | ||
import HasilPanen from "../models/hasilpanen.js"; | ||
|
||
export const postHasilPanen = async (req, res) => { | ||
const { | ||
tanggal, | ||
jenis, | ||
berat, | ||
jual, | ||
catatan | ||
} = req.body; | ||
|
||
const HasilPanenPost = new HasilPanen({ | ||
tanggal: tanggal, | ||
jenis: jenis, | ||
berat: berat, | ||
jual: jual, | ||
catatan: catatan, | ||
}); | ||
|
||
try { | ||
const hasilpanen = await HasilPanenPost.save(); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil membuat hasil panen baru', | ||
data: hasilpanen | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal membuat hasil panen baru' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getHasilPanen = async (req, res) => { | ||
try { | ||
const hasilpanen = await HasilPanen.findAll() | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan hasil panen', | ||
data: hasilpanen | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan hasil panen' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getHasilPanenById = async (req, res) => { | ||
try { | ||
const hasilpanen = await HasilPanen.findOne({ | ||
where: { | ||
id_hasil: req.params.id_hasil, | ||
} | ||
}) | ||
if (hasilpanen === null) return error | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan hasil panen', | ||
data: hasilpanen | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan hasil panen' | ||
}) | ||
}; | ||
} | ||
|
||
export const updateHasilPanen = async (req, res) => { | ||
const dataHasilPanen = req.body | ||
try { | ||
const updateHasilPanen = await HasilPanen.update({ | ||
tanggal: req.body.tanggal, | ||
jenis: req.body.jenis, | ||
berat: req.body.berat, | ||
jual: req.body.jual, | ||
catatan: req.body.catatan, | ||
},{ | ||
where:{ | ||
id_hasil: req.params.id_hasil | ||
} | ||
}); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil memperbarui hasil panen', | ||
data: dataHasilPanen | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal memperbarui hasil panen' | ||
}) | ||
} | ||
} | ||
|
||
export const deleteHasilPanen = async (req, res) => { | ||
try { | ||
const deleteHasilPanen = await HasilPanen.destroy({ | ||
where: { | ||
id_hasil: req.params.id_hasil | ||
} | ||
}); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil menghapus hasil panen' | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal menghapus hasil panen' | ||
}) | ||
} | ||
} |
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,203 @@ | ||
import Inventaris from "../models/inventaris.js"; | ||
import path from "path" | ||
import fs from "fs" | ||
|
||
export const postInventaris = async (req, res) => { | ||
const { | ||
nama, | ||
jumlah, | ||
catatan | ||
} = 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")}/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 inventaris = await Inventaris.create({ | ||
nama: nama, | ||
image: fileName, | ||
url: url, | ||
jumlah: jumlah, | ||
catatan: catatan, | ||
}) | ||
res.status(201).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil membuat inventaris', | ||
data: inventaris | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal membuat Inventaris baru' | ||
}) | ||
} | ||
}) | ||
}; | ||
|
||
export const getInventaris = async (req, res) => { | ||
try { | ||
const inventaris = await Inventaris.findAll() | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Inventaris', | ||
data: inventaris | ||
}) | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Inventaris' | ||
}) | ||
}; | ||
}; | ||
|
||
export const getInventarisById = async (req, res) => { | ||
try { | ||
const inventaris = await Inventaris.findOne({ | ||
where: { | ||
id_inventaris: req.params.id_inventaris, | ||
} | ||
}) | ||
if (inventaris === null) return error | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil mendapatkan Inventaris', | ||
data: inventaris | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal mendapatkan Inventaris' | ||
}) | ||
}; | ||
} | ||
|
||
export const updateInventaris = async (req, res) => { | ||
const searchinventaris = await Inventaris.findOne({ | ||
where: { | ||
id_inventaris: req.params.id_inventaris | ||
} | ||
}); | ||
if (!searchinventaris) return res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Inventaris tidak ditemukan' | ||
}) | ||
|
||
let fileName = ""; | ||
if (req.files === null) { | ||
fileName = inventaris.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/${searchinventaris.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, | ||
jumlah, | ||
catatan | ||
} = req.body; | ||
const url = `${req.protocol}://${req.get("host")}/images/${fileName}` | ||
try { | ||
await Inventaris.update({ | ||
nama: nama, | ||
image: fileName, | ||
url: url, | ||
jumlah: jumlah, | ||
catatan: catatan, | ||
}, { | ||
where: { | ||
id_inventaris: req.params.id_inventaris | ||
} | ||
}) | ||
const updatedinventaris = await Inventaris.findOne({ | ||
where: { | ||
id_inventaris: req.params.id_inventaris, | ||
} | ||
}) | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil memperbarui inventaris', | ||
data: updatedinventaris | ||
}) | ||
} catch (error) { | ||
res.status(400).json({ | ||
status: res.statusCode, | ||
message: 'Gagal memperbarui Inventaris' | ||
}) | ||
} | ||
} | ||
|
||
export const deleteInventaris = async (req, res) => { | ||
const inventaris = await Inventaris.findOne({ | ||
where: { | ||
id_inventaris: req.params.id_inventaris | ||
} | ||
}); | ||
if (!inventaris) return res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Inventaris tidak ditemukan' | ||
}) | ||
|
||
try { | ||
const filePath = `./public/images/${inventaris.image}` | ||
fs.unlinkSync(filePath) | ||
await Inventaris.destroy({ | ||
where: { | ||
id_inventaris: req.params.id_inventaris | ||
} | ||
}); | ||
res.status(200).json({ | ||
status: res.statusCode, | ||
message: 'Berhasil menghapus inventaris' | ||
}) | ||
} catch (err) { | ||
res.status(404).json({ | ||
status: res.statusCode, | ||
message: 'Gagal menghapus inventaris' | ||
}) | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.