2025-01-02 15:12:48 +07:00
import mysql . connector as mariadb
from config import database
from scripts import loggorilla
class price :
def __init__ ( self ) :
self . db_main = mariadb . connect ( * * database . db_main )
self . cursor = self . db_main . cursor ( dictionary = True )
def list ( self , params ) :
APIADDR = " /api/invlab/price/list "
response = { }
self . cursor . execute ( " BEGIN; " )
try :
2025-06-01 21:01:26 +07:00
self . cursor . execute ( " SELECT `id`, `sku`, `type`, `currency`, `value`, DATE_FORMAT(`period`, ' % Y- % m- %d % H: %i : % S ' ) AS `period` FROM `item_price` ORDER BY period DESC, id DESC; " )
2025-01-02 15:12:48 +07:00
ls = self . cursor . fetchall ( )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " success "
response [ " desc " ] = " data collected "
response [ " data " ] = ls
except Exception as e :
self . cursor . execute ( " ROLLBACK; " )
loggorilla . error ( APIADDR , str ( e ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " failed "
response [ " desc " ] = " Internal Server Error. Please contact us if you still have an error. "
finally :
self . cursor . execute ( " COMMIT; " )
self . cursor . close ( )
self . db_main . close ( )
return response
def detail ( self , params ) :
APIADDR = " /api/invlab/price/detail "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
id = params [ " id " ]
self . cursor . execute ( " BEGIN; " )
try :
2025-06-01 21:01:26 +07:00
self . cursor . execute ( " SELECT `id`, `sku`, `type`, `currency`, `value`, DATE_FORMAT(`period`, ' % Y- % m- %d % H: %i : % S ' ) AS `period` FROM `item_price` WHERE id= %s ORDER BY period DESC, id DESC; " , ( id , ) )
2025-01-02 15:12:48 +07:00
data = self . cursor . fetchone ( )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " success "
response [ " desc " ] = " data collected "
response [ " data " ] = data
except Exception as e :
self . cursor . execute ( " ROLLBACK; " )
loggorilla . error ( APIADDR , str ( e ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " failed "
response [ " desc " ] = " Internal Server Error. Please contact us if you still have an error. "
finally :
self . cursor . execute ( " COMMIT; " )
self . cursor . close ( )
self . db_main . close ( )
return response
def add ( self , params ) :
APIADDR = " /api/invlab/price/add "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
2025-02-28 20:44:20 +07:00
sku = params [ " sku " ]
2025-01-02 15:12:48 +07:00
type = params [ " type " ]
currency = params [ " currency " ]
value = params [ " value " ]
2025-06-01 21:01:26 +07:00
period = params [ " period " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Inserting " )
2025-06-01 21:01:26 +07:00
self . cursor . execute ( " INSERT INTO `item_price` VALUES (DEFAULT, %s , %s , %s , %s , %s ) ; " , ( sku , type , currency , value , period ) )
2025-01-02 15:12:48 +07:00
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " success "
response [ " desc " ] = " data added "
except Exception as e :
self . cursor . execute ( " ROLLBACK; " )
loggorilla . error ( APIADDR , str ( e ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " failed "
2025-02-28 20:44:20 +07:00
response [ " desc " ] = " Internal Server Error. Please contact us if you still have an error. "
2025-01-02 15:12:48 +07:00
finally :
self . cursor . execute ( " COMMIT; " )
self . cursor . close ( )
self . db_main . close ( )
return response
def edit ( self , params ) :
APIADDR = " /api/invlab/price/edit "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
key = params [ " id " ]
2025-02-28 20:44:20 +07:00
sku = params [ " sku " ]
2025-01-02 15:12:48 +07:00
type = params [ " type " ]
currency = params [ " currency " ]
value = params [ " value " ]
2025-06-01 21:01:26 +07:00
period = params [ " period " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Updating " )
2025-06-01 21:01:26 +07:00
self . cursor . execute ( " UPDATE `item_price` SET `sku` = %s ,`type` = %s ,`currency` = %s ,`value` = %s ,`period` = %s WHERE `id` = %s ; " , ( sku , type , currency , value , period , key ) )
2025-01-02 15:12:48 +07:00
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " success "
response [ " desc " ] = " data change "
except Exception as e :
self . cursor . execute ( " ROLLBACK; " )
loggorilla . error ( APIADDR , str ( e ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " failed "
response [ " desc " ] = " Internal Server Error. Please contact us if you still have an error. "
finally :
self . cursor . execute ( " COMMIT; " )
self . cursor . close ( )
self . db_main . close ( )
return response
def remove ( self , params ) :
APIADDR = " /api/invlab/price/remove "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
key = params [ " id " ]
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Deleting " )
self . cursor . execute ( " DELETE FROM `item_price` WHERE `id` = %s ; " , ( key , ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " success "
response [ " desc " ] = " data removed "
except Exception as e :
self . cursor . execute ( " ROLLBACK; " )
loggorilla . error ( APIADDR , str ( e ) )
loggorilla . prcss ( APIADDR , " Giving response " )
response [ " status " ] = " failed "
response [ " desc " ] = " Internal Server Error. Please contact us if you still have an error. "
finally :
self . cursor . execute ( " COMMIT; " )
self . cursor . close ( )
self . db_main . close ( )
return response