from BackendApi.function import QueryFunc restfunc = QueryFunc() def generate_return_data(data, code): return {"data":data, "httpcode":code} class SharixQuery: def auth_query(self, request_data): try: result = restfunc.query_select(''' SELECT * FROM rest_user WHERE (rest_user.name=%s OR rest_user.email=%s) AND rest_user.password=%s; ''', (request_data['login_email'], request_data['login_email'], request_data['password'],), True) return generate_return_data(result, 200) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def add_query(self, request_data): try: check_email_login = restfunc.query_check_count(''' SELECT * FROM rest_user WHERE rest_user.name=%s OR rest_user.email=%s; ''', (request_data['name'], request_data['email'])) if(check_email_login): return generate_return_data({"msg":"User exist!"}, 400) else: result = restfunc.query_insert(''' INSERT INTO rest_user (name, birthday, lastlogintime, insys, idrole, email, password) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING id; ''', \ (request_data['name'], request_data['reg_date'],\ request_data['log_time'], request_data['in_sys'],\ request_data['role_id'], request_data['email'], request_data['password'])) return generate_return_data(result, 201) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def get_user_query(self, id): try: result = restfunc.query_select(''' SELECT * FROM rest_user WHERE id = %s; ''', (id,), True) return generate_return_data(result, 200) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def get_all_user_query(self): try: result = restfunc.query_select(''' SELECT rest_user.id, rest_user.name, rest_user.birthday, rest_user.insys, rest_role.name AS role_name, rest_user.lastlogintime, rest_user.email, rest_user.password FROM rest_user, rest_role WHERE rest_user.idrole = rest_role.id ORDER BY id ASC; ''', (), False) return generate_return_data(result, 200) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def delete_user_query(self, request_data): try: result = restfunc.query_delete_update(''' DELETE FROM rest_user WHERE id = %s; ''', (request_data['id'],)) if(result): return generate_return_data({"msg":"Successful delete!"}, 200) else: return generate_return_data({"msg":"User does not exist!"}, 400) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def update_user_query(self, request_data, id): try: result = restfunc.query_delete_update(''' UPDATE rest_user SET name = %s WHERE id = %s; ''', (request_data['name'], id)) if(result): return {"data": {"msg":"Successful update!"}, "httpcode": 200} else: return generate_return_data({"msg":"User does not exist!"}, 400) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500) def add_car_log(self, request_data): try: result = restfunc.query_insert(''' INSERT INTO cars_log (id, resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s, %s) RETURNING id; ''', (8, 3, request_data['operation'], request_data['date_cur'], 1)) return generate_return_data(result, 201) except Exception as ex: return generate_return_data({"msg":str(ex)}, 500)