from .tool.func import *

def api_bbs_w_comment_one(sub_code = ''):
    with get_db_connect() as conn:
        curs = conn.cursor()
        
        sub_code = sub_code.split('-')
        sub_code_last = ''
        if len(sub_code) > 2:
            sub_code_last = sub_code[len(sub_code) - 1]
            del sub_code[len(sub_code) - 1]
            
        sub_code = '-'.join(sub_code)

        curs.execute(db_change('select set_name, set_data, set_code, set_id from bbs_data where (set_name = "comment" or set_name = "comment_date" or set_name = "comment_user_id") and set_id = ? and set_code = ?'), [sub_code, sub_code_last])
        db_data = curs.fetchall()
        if not db_data:
            return flask.jsonify({})
        else:
            temp_id = ''
            temp_dict = {}

            for for_a in db_data:
                if temp_id != for_a[2]:
                    temp_id = for_a[2]
                    temp_dict['code'] = for_a[2]

                temp_dict[for_a[0]] = for_a[1]

            return flask.jsonify(temp_dict)