Skip to content

Commit

Permalink
Merge pull request cyclic-software#7 from Capstone-Bangkit/CRUD-second
Browse files Browse the repository at this point in the history
Crud second
  • Loading branch information
royyan005 authored Jun 2, 2022
2 parents 9de5bbf + d6f26da commit 27bf2d2
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 4 deletions.
1 change: 1 addition & 0 deletions controller/hasilpanen.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const updateHasilPanen = async (req, res) => {
id_hasil: req.params.id_hasil
}
});
if (updateHasilPanen == 0) return error
res.status(200).json({
status: res.statusCode,
message: 'Berhasil memperbarui hasil panen',
Expand Down
2 changes: 1 addition & 1 deletion controller/inventaris.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const postInventaris = async (req, res) => {
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 url = `${req.protocol}://${req.get("host")}/public/images/${fileName}`
const allowedType = ['.png', '.jpg', '.jpeg']

if (!allowedType.includes(ext.toLowerCase())) return res.status(422).json({
Expand Down
122 changes: 122 additions & 0 deletions controller/keuangan.js
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'
})
}
}
216 changes: 216 additions & 0 deletions controller/penyakit.js
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'
})
}


}
4 changes: 2 additions & 2 deletions models/hasilpanen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const HasilPanen = db.define('hasilpanen',{
},
tanggal: {
type: Date,
default: Date.now()
default: Date.now(),
},
jenis: {
type: String,
Expand All @@ -23,7 +23,7 @@ const HasilPanen = db.define('hasilpanen',{
},
createdAt: {
type: String,
default: Date.now()
default: Date.now(),
},
updatedAt: {
type: Date,
Expand Down
2 changes: 1 addition & 1 deletion models/inventaris.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Inventaris = db.define('inventaris',{
},
createdAt: {
type: String,
default: Date.now()
default: Date.now(),
},
updatedAt: {
type: Date,
Expand Down
Loading

0 comments on commit 27bf2d2

Please sign in to comment.