64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""
|
|
Group Model - Abstraktionsschicht für Datenbankzugriffe
|
|
"""
|
|
from cmbnet.db import get_db
|
|
|
|
|
|
class GroupModel:
|
|
@staticmethod
|
|
def get_all_by_tenant(tenant_id):
|
|
db = get_db()
|
|
return db.execute(
|
|
'SELECT * FROM groups WHERE tenant_id = ? ORDER BY id',
|
|
(tenant_id,)
|
|
).fetchall()
|
|
|
|
@staticmethod
|
|
def get_by_id(group_id, tenant_id):
|
|
db = get_db()
|
|
return db.execute(
|
|
'SELECT * FROM groups WHERE id = ? AND tenant_id = ?',
|
|
(group_id, tenant_id)
|
|
).fetchone()
|
|
|
|
@staticmethod
|
|
def get_by_account_id(account_id, tenant_id):
|
|
db = get_db()
|
|
return db.execute(
|
|
'SELECT * FROM groups WHERE account_id = ? AND tenant_id = ?',
|
|
(account_id, tenant_id)
|
|
).fetchone()
|
|
|
|
@staticmethod
|
|
def create(tenant_id, aliasname, account_id=None, state='offen'):
|
|
db = get_db()
|
|
cursor = db.execute(
|
|
'INSERT INTO groups (tenant_id, aliasname, account_id, state) VALUES (?, ?, ?, ?)',
|
|
(tenant_id, aliasname, account_id, state)
|
|
)
|
|
db.commit()
|
|
return cursor.lastrowid
|
|
|
|
@staticmethod
|
|
def update(group_id, tenant_id, **kwargs):
|
|
db = get_db()
|
|
allowed = ['aliasname', 'state', 'account_id', 'last_lat', 'last_lon', 'last_pos_at']
|
|
updates = []
|
|
values = []
|
|
for k, v in kwargs.items():
|
|
if k in allowed:
|
|
updates.append(f'{k} = ?')
|
|
values.append(v)
|
|
if not updates:
|
|
return
|
|
updates.append('updated_at = CURRENT_TIMESTAMP')
|
|
values.extend([group_id, tenant_id])
|
|
db.execute(f'UPDATE groups SET {", ".join(updates)} WHERE id = ? AND tenant_id = ?', values)
|
|
db.commit()
|
|
|
|
@staticmethod
|
|
def delete(group_id, tenant_id):
|
|
db = get_db()
|
|
db.execute('DELETE FROM groups WHERE id = ? AND tenant_id = ?', (group_id, tenant_id))
|
|
db.commit()
|