131 lines
2.3 KiB
JavaScript
131 lines
2.3 KiB
JavaScript
const db = require('../../db/database')
|
|
|
|
// const Op = db.Sequelize.Op
|
|
|
|
class ControllerModel {
|
|
async create (req) {
|
|
console.log(req)
|
|
return db.controllers.create(req)
|
|
}
|
|
|
|
async update (req, id) {
|
|
req.updated_at = db.Sequelize.literal('CURRENT_TIMESTAMP')
|
|
if (id != null) {
|
|
const ret = await db.controllers.update(
|
|
req,
|
|
{
|
|
where: {
|
|
id: id
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
} else {
|
|
const ret = await db.controllers.update(
|
|
req,
|
|
{
|
|
where: {}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
}
|
|
|
|
async updateByID (req, id) {
|
|
req.updated_at = db.Sequelize.literal('CURRENT_TIMESTAMP')
|
|
const ret = await db.controllers.update(
|
|
req,
|
|
{
|
|
where: {
|
|
id: id
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async updateByAttr (req, attr, value) {
|
|
const whereObj = {}
|
|
const attrList = attr.split('-')
|
|
const valueList = value.split('-')
|
|
attrList.forEach((key, idx) => {
|
|
whereObj[key] = valueList[idx]
|
|
})
|
|
req.updated_at = db.Sequelize.literal('CURRENT_TIMESTAMP')
|
|
const ret = await db.controllers.update(
|
|
req,
|
|
{
|
|
where: whereObj
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async clearDeleted () {
|
|
const ret = await db.controllers.delete(
|
|
{
|
|
where: {
|
|
deleted: true
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByID (id) {
|
|
const ret = await db.controllers.findAll(
|
|
{
|
|
where: {
|
|
id: id
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByName (name) {
|
|
const ret = await db.controllers.findAll(
|
|
{
|
|
where: {
|
|
name: name
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByMac (mac) {
|
|
const ret = await db.controllers.findAll(
|
|
{
|
|
where: {
|
|
mac_address: mac
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByAttr (attr, value) {
|
|
const whereObj = {}
|
|
const attrList = attr.split('-')
|
|
const valueList = value.split('-')
|
|
attrList.forEach((key, idx) => {
|
|
whereObj[key] = valueList[idx]
|
|
})
|
|
|
|
const ret = await db.controllers.findAll(
|
|
{
|
|
where: whereObj
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getAll () {
|
|
const ret = await db.controllers.findAll()
|
|
return ret
|
|
}
|
|
}
|
|
|
|
export default ControllerModel
|