Skip to content

Commit a33f030

Browse files
committed
add crud for hasil panen and inventaris
1 parent 856451d commit a33f030

File tree

14 files changed

+492
-75
lines changed

14 files changed

+492
-75
lines changed

controller/article.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

controller/hasilpanen.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import HasilPanen from "../models/hasilpanen.js";
2+
3+
export const postHasilPanen = async (req, res) => {
4+
const {
5+
tanggal,
6+
jenis,
7+
berat,
8+
jual,
9+
catatan
10+
} = req.body;
11+
12+
const HasilPanenPost = new HasilPanen({
13+
tanggal: tanggal,
14+
jenis: jenis,
15+
berat: berat,
16+
jual: jual,
17+
catatan: catatan,
18+
});
19+
20+
try {
21+
const hasilpanen = await HasilPanenPost.save();
22+
res.status(200).json({
23+
status: res.statusCode,
24+
message: 'Berhasil membuat hasil panen baru',
25+
data: hasilpanen
26+
})
27+
} catch (error) {
28+
res.status(400).json({
29+
status: res.statusCode,
30+
message: 'Gagal membuat hasil panen baru'
31+
})
32+
};
33+
};
34+
35+
export const getHasilPanen = async (req, res) => {
36+
try {
37+
const hasilpanen = await HasilPanen.findAll()
38+
res.status(200).json({
39+
status: res.statusCode,
40+
message: 'Berhasil mendapatkan hasil panen',
41+
data: hasilpanen
42+
})
43+
} catch (err) {
44+
res.status(400).json({
45+
status: res.statusCode,
46+
message: 'Gagal mendapatkan hasil panen'
47+
})
48+
};
49+
};
50+
51+
export const getHasilPanenById = async (req, res) => {
52+
try {
53+
const hasilpanen = await HasilPanen.findOne({
54+
where: {
55+
id_hasil: req.params.id_hasil,
56+
}
57+
})
58+
if (hasilpanen === null) return error
59+
res.status(200).json({
60+
status: res.statusCode,
61+
message: 'Berhasil mendapatkan hasil panen',
62+
data: hasilpanen
63+
})
64+
} catch (error) {
65+
res.status(400).json({
66+
status: res.statusCode,
67+
message: 'Gagal mendapatkan hasil panen'
68+
})
69+
};
70+
}
71+
72+
export const updateHasilPanen = async (req, res) => {
73+
const dataHasilPanen = req.body
74+
try {
75+
const updateHasilPanen = await HasilPanen.update({
76+
tanggal: req.body.tanggal,
77+
jenis: req.body.jenis,
78+
berat: req.body.berat,
79+
jual: req.body.jual,
80+
catatan: req.body.catatan,
81+
},{
82+
where:{
83+
id_hasil: req.params.id_hasil
84+
}
85+
});
86+
res.status(200).json({
87+
status: res.statusCode,
88+
message: 'Berhasil memperbarui hasil panen',
89+
data: dataHasilPanen
90+
})
91+
} catch (err) {
92+
res.status(400).json({
93+
status: res.statusCode,
94+
message: 'Gagal memperbarui hasil panen'
95+
})
96+
}
97+
}
98+
99+
export const deleteHasilPanen = async (req, res) => {
100+
try {
101+
const deleteHasilPanen = await HasilPanen.destroy({
102+
where: {
103+
id_hasil: req.params.id_hasil
104+
}
105+
});
106+
res.status(200).json({
107+
status: res.statusCode,
108+
message: 'Berhasil menghapus hasil panen'
109+
})
110+
} catch (err) {
111+
res.status(400).json({
112+
status: res.statusCode,
113+
message: 'Gagal menghapus hasil panen'
114+
})
115+
}
116+
}

controller/inventaris.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import Inventaris from "../models/inventaris.js";
2+
import path from "path"
3+
import fs from "fs"
4+
5+
export const postInventaris = async (req, res) => {
6+
const {
7+
nama,
8+
jumlah,
9+
catatan
10+
} = req.body;
11+
if (req.files === null) return res.status(400).json({
12+
status: res.statusCode,
13+
message: 'Tidak ada file',
14+
})
15+
const file = req.files.file
16+
const fileSize = file.data.length
17+
const ext = path.extname(file.name)
18+
const fileName = file.md5 + ext
19+
const url = `${req.protocol}://${req.get("host")}/images/${fileName}`
20+
const allowedType = ['.png', '.jpg', '.jpeg']
21+
22+
if (!allowedType.includes(ext.toLowerCase())) return res.status(422).json({
23+
status: res.statusCode,
24+
message: 'invalid images',
25+
})
26+
if (fileSize > 5000000) return res.status(422).json({
27+
status: res.statusCode,
28+
message: 'Image must be less than 5 MB',
29+
})
30+
31+
file.mv(`./public/images/${fileName}`, async (err) => {
32+
if (err) return res.status(500).json({
33+
status: res.statusCode,
34+
message: 'invalid images',
35+
})
36+
try {
37+
const inventaris = await Inventaris.create({
38+
nama: nama,
39+
image: fileName,
40+
url: url,
41+
jumlah: jumlah,
42+
catatan: catatan,
43+
})
44+
res.status(201).json({
45+
status: res.statusCode,
46+
message: 'Berhasil membuat inventaris',
47+
data: inventaris
48+
})
49+
} catch (error) {
50+
res.status(400).json({
51+
status: res.statusCode,
52+
message: 'Gagal membuat Inventaris baru'
53+
})
54+
}
55+
})
56+
};
57+
58+
export const getInventaris = async (req, res) => {
59+
try {
60+
const inventaris = await Inventaris.findAll()
61+
res.status(200).json({
62+
status: res.statusCode,
63+
message: 'Berhasil mendapatkan Inventaris',
64+
data: inventaris
65+
})
66+
} catch (err) {
67+
res.status(400).json({
68+
status: res.statusCode,
69+
message: 'Gagal mendapatkan Inventaris'
70+
})
71+
};
72+
};
73+
74+
export const getInventarisById = async (req, res) => {
75+
try {
76+
const inventaris = await Inventaris.findOne({
77+
where: {
78+
id_inventaris: req.params.id_inventaris,
79+
}
80+
})
81+
if (inventaris === null) return error
82+
res.status(200).json({
83+
status: res.statusCode,
84+
message: 'Berhasil mendapatkan Inventaris',
85+
data: inventaris
86+
})
87+
} catch (error) {
88+
res.status(400).json({
89+
status: res.statusCode,
90+
message: 'Gagal mendapatkan Inventaris'
91+
})
92+
};
93+
}
94+
95+
export const updateInventaris = async (req, res) => {
96+
const searchinventaris = await Inventaris.findOne({
97+
where: {
98+
id_inventaris: req.params.id_inventaris
99+
}
100+
});
101+
if (!searchinventaris) return res.status(404).json({
102+
status: res.statusCode,
103+
message: 'Inventaris tidak ditemukan'
104+
})
105+
106+
let fileName = "";
107+
if (req.files === null) {
108+
fileName = inventaris.image
109+
} else {
110+
const file = req.files.file
111+
const fileSize = file.data.length
112+
const ext = path.extname(file.name)
113+
fileName = file.md5 + ext
114+
const allowedType = ['.png', '.jpg', '.jpeg']
115+
116+
if (!allowedType.includes(ext.toLowerCase())) return res.status(422).json({
117+
status: res.statusCode,
118+
message: 'invalid images',
119+
})
120+
if (fileSize > 5000000) return res.status(422).json({
121+
status: res.statusCode,
122+
message: 'Image must be less than 5 MB',
123+
})
124+
125+
const filePath = `./public/images/${searchinventaris.image}`
126+
fs.unlinkSync(filePath)
127+
128+
file.mv(`./public/images/${fileName}`, (err) => {
129+
if (err) return res.status(500).json({
130+
status: res.statusCode,
131+
message: 'invalid images',
132+
})
133+
})
134+
}
135+
136+
const {
137+
nama,
138+
jumlah,
139+
catatan
140+
} = req.body;
141+
const url = `${req.protocol}://${req.get("host")}/images/${fileName}`
142+
try {
143+
await Inventaris.update({
144+
nama: nama,
145+
image: fileName,
146+
url: url,
147+
jumlah: jumlah,
148+
catatan: catatan,
149+
}, {
150+
where: {
151+
id_inventaris: req.params.id_inventaris
152+
}
153+
})
154+
const updatedinventaris = await Inventaris.findOne({
155+
where: {
156+
id_inventaris: req.params.id_inventaris,
157+
}
158+
})
159+
res.status(200).json({
160+
status: res.statusCode,
161+
message: 'Berhasil memperbarui inventaris',
162+
data: updatedinventaris
163+
})
164+
} catch (error) {
165+
res.status(400).json({
166+
status: res.statusCode,
167+
message: 'Gagal memperbarui Inventaris'
168+
})
169+
}
170+
}
171+
172+
export const deleteInventaris = async (req, res) => {
173+
const inventaris = await Inventaris.findOne({
174+
where: {
175+
id_inventaris: req.params.id_inventaris
176+
}
177+
});
178+
if (!inventaris) return res.status(404).json({
179+
status: res.statusCode,
180+
message: 'Inventaris tidak ditemukan'
181+
})
182+
183+
try {
184+
const filePath = `./public/images/${inventaris.image}`
185+
fs.unlinkSync(filePath)
186+
await Inventaris.destroy({
187+
where: {
188+
id_inventaris: req.params.id_inventaris
189+
}
190+
});
191+
res.status(200).json({
192+
status: res.statusCode,
193+
message: 'Berhasil menghapus inventaris'
194+
})
195+
} catch (err) {
196+
res.status(404).json({
197+
status: res.statusCode,
198+
message: 'Gagal menghapus inventaris'
199+
})
200+
}
201+
202+
203+
}

0 commit comments

Comments
 (0)