commit
32a36cceff
444 files changed
+67186
No files matched your search
@@ -0,0 +1,103 @@
|
||||
const { query } = require('../database/db');
|
||||
|
||||
class Kandang {
|
||||
static async getAll() {
|
||||
const result = await query('SELECT * FROM kandangs ORDER BY id');
|
||||
return result.rows.map(this.formatKandang);
|
||||
}
|
||||
|
||||
static async getById(id) {
|
||||
const result = await query('SELECT * FROM kandangs WHERE id = $1', [id]);
|
||||
if (result.rows.length === 0) return null;
|
||||
return this.formatKandang(result.rows[0]);
|
||||
}
|
||||
|
||||
static async create(name) {
|
||||
const result = await query(
|
||||
'INSERT INTO kandangs (name) VALUES ($1) RETURNING *',
|
||||
[name]
|
||||
);
|
||||
return this.formatKandang(result.rows[0]);
|
||||
}
|
||||
|
||||
static async update(id, name) {
|
||||
const result = await query(
|
||||
'UPDATE kandangs SET name = $1 WHERE id = $2 RETURNING *',
|
||||
[name, id]
|
||||
);
|
||||
if (result.rows.length === 0) return null;
|
||||
return this.formatKandang(result.rows[0]);
|
||||
}
|
||||
|
||||
static async delete(id) {
|
||||
const result = await query('DELETE FROM kandangs WHERE id = $1', [id]);
|
||||
return result.rowCount > 0;
|
||||
}
|
||||
|
||||
// Get kandangs with their doc_in_count for a specific cycle
|
||||
static async getByCycle(cycleId) {
|
||||
const result = await query(`
|
||||
SELECT k.*, kc.doc_in_count
|
||||
FROM kandangs k
|
||||
LEFT JOIN kandang_cycles kc ON k.id = kc.kandang_id AND kc.cycle_id = $1
|
||||
ORDER BY k.id
|
||||
`, [cycleId]);
|
||||
return result.rows.map(row => ({
|
||||
...this.formatKandang(row),
|
||||
docInCount: parseInt(row.doc_in_count) || 0
|
||||
}));
|
||||
}
|
||||
|
||||
// Set doc_in_count for a kandang in a specific cycle (upsert)
|
||||
static async setDocInCount(kandangId, cycleId, docInCount) {
|
||||
await query(`
|
||||
INSERT INTO kandang_cycles (kandang_id, cycle_id, doc_in_count)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (kandang_id, cycle_id)
|
||||
DO UPDATE SET doc_in_count = EXCLUDED.doc_in_count
|
||||
`, [kandangId, cycleId, docInCount]);
|
||||
}
|
||||
|
||||
// Copy DOC counts from one cycle to another (useful when starting new cycle)
|
||||
static async copyDocCountsToCycle(fromCycleId, toCycleId) {
|
||||
await query(`
|
||||
INSERT INTO kandang_cycles (kandang_id, cycle_id, doc_in_count)
|
||||
SELECT kandang_id, $2, doc_in_count
|
||||
FROM kandang_cycles
|
||||
WHERE cycle_id = $1
|
||||
ON CONFLICT (kandang_id, cycle_id) DO NOTHING
|
||||
`, [fromCycleId, toCycleId]);
|
||||
}
|
||||
|
||||
// Get kandang by cage UUID
|
||||
static async getByCageUuid(cageUuid) {
|
||||
const result = await query(
|
||||
'SELECT * FROM kandangs WHERE cage_uuid = $1',
|
||||
[cageUuid]
|
||||
);
|
||||
if (result.rows.length === 0) return null;
|
||||
return this.formatKandang(result.rows[0]);
|
||||
}
|
||||
|
||||
// Update cage UUID for a kandang
|
||||
static async updateCageUuid(id, cageUuid) {
|
||||
const result = await query(
|
||||
'UPDATE kandangs SET cage_uuid = $1, updated_at = CURRENT_TIMESTAMP WHERE id = $2 RETURNING *',
|
||||
[cageUuid, id]
|
||||
);
|
||||
if (result.rows.length === 0) return null;
|
||||
return this.formatKandang(result.rows[0]);
|
||||
}
|
||||
|
||||
static formatKandang(record) {
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
cageUuid: record.cage_uuid || null,
|
||||
createdAt: record.created_at,
|
||||
updatedAt: record.updated_at
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Kandang;
|
||||
Reference in new issue
Block a user