2025-01-02 15:12:48 +07:00
import mysql . connector as mariadb
from config import database
from scripts import loggorilla
class item :
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/item/list "
response = { }
self . cursor . execute ( " BEGIN; " )
try :
ls = [ ]
self . cursor . execute ( " SELECT * FROM `item`; " )
l1 = self . cursor . fetchall ( )
c1 = 0
for d1 in l1 :
ls . append ( {
2025-02-28 17:47:57 +07:00
" sku " : d1 [ " sku " ] ,
2025-01-02 15:12:48 +07:00
" name " : d1 [ " name " ] ,
" price " : {
" buy " : [ ] ,
" sell " : [ ]
}
} )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, ' % Y- % m- %d % H: %i : % S ' ) AS `periods` from item_price WHERE `sku` = %s AND `type`= ' buy ' ORDER BY periods DESC, id DESC; " , ( d1 [ " sku " ] , ) )
2025-01-02 15:12:48 +07:00
ls [ c1 ] [ " price " ] [ " buy " ] = self . cursor . fetchall ( )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, ' % Y- % m- %d % H: %i : % S ' ) AS `periods` from item_price WHERE `sku` = %s AND `type`= ' sell ' ORDER BY periods DESC, id DESC; " , ( d1 [ " sku " ] , ) )
2025-01-02 15:12:48 +07:00
ls [ c1 ] [ " price " ] [ " sell " ] = self . cursor . fetchall ( )
c1 + = 1
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/item/detail "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
2025-02-28 17:47:57 +07:00
sku = params [ " sku " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " SELECT * FROM `item` WHERE `sku` = %s ; " , ( sku , ) )
2025-01-02 15:12:48 +07:00
r = self . cursor . fetchone ( )
data = {
2025-02-28 17:47:57 +07:00
" sku " : r [ " sku " ] ,
2025-01-02 15:12:48 +07:00
" name " : r [ " name " ] ,
" price " : {
" buy " : [ ] ,
" sell " : [ ]
}
}
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, ' % Y- % m- %d % H: %i : % S ' ) AS `periods` from item_price WHERE `sku` = %s AND `type`= ' buy ' ORDER BY periods DESC, id DESC; " , ( sku , ) )
2025-01-02 15:12:48 +07:00
data [ " price " ] [ " buy " ] = self . cursor . fetchall ( )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " SELECT `id`, `currency`, `value`, DATE_FORMAT(`periods`, ' % Y- % m- %d % H: %i : % S ' ) AS `periods` from item_price WHERE `sku` = %s AND `type`= ' sell ' ORDER BY periods DESC, id DESC; " , ( sku , ) )
2025-01-02 15:12:48 +07:00
data [ " price " ] [ " sell " ] = self . cursor . fetchall ( )
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/item/add "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
2025-02-28 17:47:57 +07:00
sku = params [ " sku " ]
name = params [ " name " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Inserting " )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " INSERT INTO `item` VALUES ( %s , %s ) ; " , ( sku , name ) )
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 17:47:57 +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/item/edit "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
2025-02-28 17:47:57 +07:00
key = params [ " sku " ]
name = params [ " name " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Updating " )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " UPDATE `item` SET `name` = %s WHERE `sku` = %s ; " , ( name , 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/item/remove "
response = { }
loggorilla . prcss ( APIADDR , " Define parameters " )
2025-02-28 17:47:57 +07:00
key = params [ " sku " ]
2025-01-02 15:12:48 +07:00
self . cursor . execute ( " BEGIN; " )
try :
loggorilla . prcss ( APIADDR , " Deleting " )
2025-02-28 17:47:57 +07:00
self . cursor . execute ( " DELETE FROM `item` WHERE `sku` = %s ; " , ( key , ) )
2025-01-02 15:12:48 +07:00
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