import type { key_val } from '$lib/ae_stores'; import { api } from '$lib/api'; import { db_posts } from "$lib/ae_posts/db_posts"; let ae_promises: key_val = {}; // Updated 2024-09-25 export async function load_ae_obj_id__post_comment( { api_cfg, post_comment_id, enabled = 'enabled', hidden = 'not_hidden', limit = 99, offset = 0, try_cache = true, log_lvl = 0 }: { api_cfg: any, post_comment_id: string, enabled?: string, hidden?: string, limit?: number, offset?: number, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_id__post_comment() *** post_comment_id=${post_comment_id}`); } let params = {}; ae_promises.load__post_comment_obj = await api.get_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'post_comment', obj_id: post_comment_id, // NOTE: This is the FQDN, not normally the ID. use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config. use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value params: params, log_lvl: log_lvl }) .then(function (post_comment_obj_get_result) { if (post_comment_obj_get_result) { if (try_cache) { // This is expecting a list db_save_ae_obj_li__post_comment({ obj_type: 'post_comment', obj_li: [post_comment_obj_get_result], log_lvl: log_lvl }); } return post_comment_obj_get_result; } else { console.log('No results returned.'); return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }); return ae_promises.load__post_comment_obj; } // Updated 2024-11-20 export async function load_ae_obj_li__post_comment( { api_cfg, for_obj_type = 'post', for_obj_id, enabled = 'enabled', hidden = 'not_hidden', limit = 99, offset = 0, order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'updated_on': 'DESC', 'created_on': 'DESC', 'title': 'ASC'}, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, for_obj_type: string, for_obj_id: string, enabled?: string, hidden?: string, limit?: number, offset?: number, order_by_li?: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_li__post_comment() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`); } // if (params.qry__enabled) { enabled = params.qry__enabled; } // if (params.qry__hidden) { hidden = params.qry__hidden; } // if (params.qry__limit) { limit = params.qry__limit; } // if (params.qry__offset) { offset = params.qry__offset; } let params_json: key_val = {}; // console('params_json:', params_json); ae_promises.load__post_comment_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({ api_cfg: api_cfg, obj_type: 'post_comment', for_obj_type: for_obj_type, for_obj_id: for_obj_id, use_alt_tbl: false, use_alt_mdl: false, use_alt_exp: false, enabled: enabled, hidden: hidden, order_by_li: order_by_li, limit: limit, offset: offset, params_json: params_json, params: params, log_lvl: log_lvl }) .then(function (post_comment_obj_li_get_result) { if (post_comment_obj_li_get_result) { if (try_cache) { db_save_ae_obj_li__post_comment({ obj_type: 'post_comment', obj_li: post_comment_obj_li_get_result, log_lvl: log_lvl }); } return post_comment_obj_li_get_result; } else { return []; } }) .catch(function (error) { console.log('No results returned or failed.', error); }); if (log_lvl) { console.log('ae_promises.load__post_comment_obj_li:', ae_promises.load__post_comment_obj_li); } return ae_promises.load__post_comment_obj_li; } // Updated 2024-09-25 export async function create_ae_obj__post_comment( { api_cfg, post_id, data_kv, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, post_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** create_ae_obj__post_comment() *** post_id=${post_id}`); } ae_promises.create__post_comment = await api.create_ae_obj_crud({ api_cfg: api_cfg, obj_type: 'post_comment', fields: { post_id_random: post_id, ...data_kv }, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(function (post_comment_obj_create_result) { if (post_comment_obj_create_result) { if (try_cache) { db_save_ae_obj_li__post_comment( { obj_type: 'post_comment', obj_li: [post_comment_obj_create_result], log_lvl: log_lvl }); } return post_comment_obj_create_result; } else { return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { }); if (log_lvl) { console.log('ae_promises.create__post_comment:', ae_promises.create__post_comment); } return ae_promises.create__post_comment; } // Updated 2024-11-08 export async function delete_ae_obj_id__post_comment( { api_cfg, post_comment_id, method = 'delete', // 'delete', 'disable', 'hide' params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, post_comment_id: string, method?: string, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** delete_ae_obj_id__post_comment() *** post_comment_id=${post_comment_id}`); } ae_promises.delete__post_comment_obj = await api.delete_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'post_comment', obj_id: post_comment_id, key: api_cfg.api_crud_super_key, params: params, method: method, log_lvl: log_lvl }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { if (try_cache) { if (log_lvl) { console.log(`Attempting to remove IDB entry for post_comment_id=${post_comment_id}`); } db_posts.comment.delete(post_comment_id); // Delete from the DB no matter what. } }); if (log_lvl) { console.log('ae_promises.delete__post_comment_obj:', ae_promises.delete__post_comment_obj); } return ae_promises.delete__post_comment_obj; } // Updated 2024-09-25 export async function update_ae_obj__post_comment( { api_cfg, post_comment_id, data_kv, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, post_comment_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** update_ae_obj__post_comment() *** post_comment_id=${post_comment_id}`, data_kv); } ae_promises.update__post_comment_obj = await api.update_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'post_comment', obj_id: post_comment_id, fields: data_kv, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(function (post_comment_obj_update_result) { if (post_comment_obj_update_result) { if (try_cache) { db_save_ae_obj_li__post_comment({ obj_type: 'post_comment', obj_li: [post_comment_obj_update_result], log_lvl: log_lvl }); } return post_comment_obj_update_result; } else { return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { }); if (log_lvl) { console.log('ae_promises.update__post_comment_obj:', ae_promises.update__post_comment_obj); } return ae_promises.update__post_comment_obj; } // This function will loop through the post_comment_obj_li and save each one to the DB. // Updated 2024-09-25 export function db_save_ae_obj_li__post_comment( { obj_type, obj_li, log_lvl = 0 }: { obj_type: string, obj_li: any, log_lvl?: number } ) { if (log_lvl) { console.log(`*** db_save_ae_obj_li__post_comment() ***`); } if (obj_li && obj_li.length) { obj_li.forEach(async function (obj: any) { if (log_lvl) { console.log(`ae_obj ${obj_type}:`, obj); } try { const id_random = await db_posts.comment.put({ id: obj.post_comment_id_random, post_comment_id: obj.post_comment_id_random, post_id: obj.post_id_random, external_person_id: obj.external_person_id, name: obj.name, title: obj.title, // Switching to name instead of title // summary: obj.summary, content: obj.content, anonymous: obj.anonymous, full_name: obj.full_name, email: obj.email, notify: obj.notify, linked_li_json: obj.linked_li_json, cfg_json: obj.cfg_json, enable: obj.enable, hide: obj.hide, priority: obj.priority, sort: obj.sort, group: obj.group, notes: obj.notes, created_on: obj.created_on, updated_on: obj.updated_on, // From SQL view }); if (log_lvl) { console.log(`Put obj with ID: ${obj.post_comment_id_random} or ${id_random}`); } } catch (error) { let status = `Failed to put ${obj.post_comment_id_random}: ${error}`; console.log(status); return false; } }); return true; } }