コード例 #1
0
    def post(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            _json = request.json
            _client_name = _json['client_name']
            _client_status = _json['client_status']
            _mobile_phone = _json['mobile_phone']
            _email_address = _json['email_address']
            _city = _json['city']
            if not isinstance(conn, str):
                query = "INSERT INTO client(client_name, client_status, mobile_phone, email_address, city) VALUES(%s, %s, %s, %s, %s)"
                bindData = (_client_name, _client_status, _mobile_phone,
                            _email_address, _city)
                cursor = conn.cursor()
                cursor.execute(query, bindData)
                conn.commit()
                close_connection(conn, cursor)

                return api_success(None, "Client saved successfully")
            else:
                return api_failure(str(conn))
        except Exception as error:
            return api_failure(str(error))
コード例 #2
0
    def put(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            _json = request.json
            _client_id = _json['client_id']
            _client_name = _json['client_name']
            _client_status = _json['client_status']
            _mobile_phone = _json['mobile_phone']
            _email_address = _json['email_address']
            _city = _json['city']

            cursor.execute("SELECT count(*) FROM client WHERE client_id=%s",
                           (_client_id, ))
            check_record_exist = cursor.fetchone().get('count(*)')
            if check_record_exist:
                if not isinstance(conn, str):
                    query = "UPDATE client set client_name=%s, client_status=%s, mobile_phone=%s, email_address=%s, city=%s where client_id = %s"
                    bindData = (_client_name, _client_status, _mobile_phone,
                                _email_address, _city, _client_id)
                    cursor = conn.cursor()
                    cursor.execute(query, bindData)
                    conn.commit()
                    close_connection(conn, cursor)

                    return api_success(None, "Client updated successfully")
                else:
                    return api_failure(str(conn))
            else:
                return {"message": "Record not found"}, 404
        except Exception as error:
            return api_failure(str(error))
コード例 #3
0
    def post(self, *args, **kwargs):
        _json = request.json
        _username = _json.get('username', None)
        _password = _json.get('password', None)

        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            cursor.execute("SELECT id, username, password FROM user")
            db_users = cursor.fetchall()

            username_table = {u.get('username', None): u for u in db_users}
            userid_table = {u.get('id', None): u for u in db_users}

            user = username_table.get(_username, None)
            if user and safe_str_cmp(user.get('password'), _password):
                access_token = create_access_token(
                    identity=user.get('username'), fresh=True)
                refresh_token = create_refresh_token(
                    identity=user.get('username'))
                return {
                    "access_token": access_token,
                    "refresh_token": refresh_token
                }, 200

            return {"message": "Invalid Credentials!"}, 401
        except Exception as e:
            return {"message": str(e)}, 400
コード例 #4
0
    def get(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            client_id = int(request.headers.get('client_id'))
            cursor.execute("SELECT count(*) FROM client WHERE client_id=%s",
                           (client_id, ))
            check_record_exist = cursor.fetchone().get('count(*)')
            if check_record_exist:
                if not isinstance(conn, str):
                    query = "SELECT client_id, client_name, client_status, mobile_phone, email_address, city FROM client \
                             WHERE client_id = {client_id}".format(
                        client_id=client_id)
                    cursor.execute(query)
                    clientRow = cursor.fetchone()
                    close_connection(conn, cursor)

                    return api_success(clientRow,
                                       "Client details fetched successfully")
                else:
                    return api_failure(str(conn))
            else:
                return {"message": "Record not found"}, 404
        except Exception as error:
            return api_failure(str(error))
コード例 #5
0
    def put(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            _json = request.json
            _product_id = _json['product_id']
            _product_name = _json['product_name']
            _product_status = _json['product_status']
            cursor.execute("SELECT count(*) FROM product WHERE product_id=%s", (_product_id,))
            check_record_exist = cursor.fetchone().get('count(*)')
            if check_record_exist:
                if not isinstance(conn, str):
                    query = "UPDATE product set product_name=%s, product_status=%s where product_id = %s"
                    bindData = (_product_name, _product_status, _product_id)
                    cursor.execute(query, bindData)
                    conn.commit()
                    close_connection(conn, cursor)

                    return api_success(None, "Product updated successfully")
                else:
                    return api_failure(str(conn))
            else:
                return { "message": "Record not found" }, 404
        except Exception as error:
            return api_failure(str(error))
コード例 #6
0
    def get(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            if not isinstance(conn, str):
                cursor.execute("SELECT product_id, product_name, product_status FROM product")
                productRows = cursor.fetchall()
                close_connection(conn, cursor)
                return api_success(productRows, "Products lists fetched successfully")
            else:
                return api_failure(str(conn))
        except Exception as error:
            return api_failure(str(error))
コード例 #7
0
    def get(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            if not isinstance(conn, str):
                cursor.execute("SELECT client_id, client_name, mobile_phone, email_address, city FROM client")
                clientRows = cursor.fetchall()
                close_connection(conn, cursor)
                return api_success(clientRows, "Clients lists fetched successfully")
            else:
                return api_failure(str(conn))
        except Exception as error:
            return api_failure(str(error))
コード例 #8
0
    def post(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            _json = request.json
            _product_name = _json['product_name']
            _product_status = _json['product_status']
            if not isinstance(conn, str):
                query = "INSERT INTO product(product_name, product_status) VALUES(%s, %s)"
                bindData = (_product_name, _product_status)
                cursor = conn.cursor()
                cursor.execute(query, bindData)
                conn.commit()
                close_connection(conn, cursor)

                return api_success(None, "Product saved successfully")
            else:
                return api_failure(str(conn))
        except Exception as error:
            return api_failure(str(error))
コード例 #9
0
    def delete(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            _product_id = int(request.headers.get('product_id'))
            cursor.execute("SELECT count(*) FROM product WHERE product_id=%s", (_product_id,))
            check_record_exist = cursor.fetchone().get('count(*)')
            if check_record_exist:
                if not isinstance(conn, str):
                    query = "DELETE FROM product WHERE product_id =%s"
                    bindData = (_product_id, )
                    cursor.execute(query, bindData)
                    conn.commit()
                    close_connection(conn, cursor)

                    return api_success(None, "Products deleted successfully")
                else:
                    return api_failure(str(conn))
            else:
                return { "message": "Record not found" }, 404
        except Exception as error:
            return api_failure(str(error))
コード例 #10
0
    def get(self):
        try:
            conn = mysql.connect()
            cursor = conn.cursor()

            product_id = int(request.headers.get('product_id'))
            cursor.execute("SELECT count(*) FROM product WHERE product_id=%s", (product_id,))
            check_record_exist = cursor.fetchone().get('count(*)')
            if check_record_exist:
                if not isinstance(conn, str):
                    query = "SELECT product_id, product_name, product_status FROM product \
                             WHERE product_id = {product_id}".format(product_id = product_id)
                    cursor.execute(query)
                    productRow = cursor.fetchone()
                    close_connection(conn, cursor)

                    return api_success(productRow, "Products details fetched successfully")
                else:
                    return api_failure(str(conn))
            else:
                return { "message": "Record not found" }, 404
        except Exception as error:
            return api_failure(str(error))