add: CRUD tags
This commit is contained in:
89
services/tags.service.js
Normal file
89
services/tags.service.js
Normal 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;
|
||||
|
||||
Reference in New Issue
Block a user