add: CRUD tags

This commit is contained in:
Muhammad Afif
2025-10-10 20:06:56 +07:00
parent ee30308112
commit 425b1ed554
8 changed files with 358 additions and 122 deletions

89
services/tags.service.js Normal file
View File

@@ -0,0 +1,89 @@
const {
getAllTagsDb,
getTagsByIdDb,
createTagsDb,
updateTagsDb,
deleteTagsDb
} = require('../db/tags.db');
const { ErrorHandler } = require('../helpers/error');
class TagsService {
// Get all devices
static async getAllTags(param) {
try {
const results = await getAllTagsDb(param);
results.data.map(element => {
});
return results
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get device by ID
static async getTagByID(id) {
try {
const result = await getTagsByIdDb(id);
if (result.length < 1) throw new ErrorHandler(404, 'Tags not found');
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Create device
static async createTags(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await createTagsDb(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Update device
static async updateTags(id, data) {
try {
if (!data || typeof data !== 'object') data = {};
const dataExist = await getTagsByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Tags not found');
}
const result = await updateTagsDb(id, data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Soft delete device
static async deleteTags(id, userId) {
try {
const dataExist = await getTagsByIdDb(id);
if (dataExist.length < 1) {
throw new ErrorHandler(404, 'Tags not found');
}
const result = await deleteTagsDb(id, userId);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
}
module.exports = TagsService;