More moving of files around. Hopefully nothing is broken again.
This commit is contained in:
306
src/lib/ae_posts/ae_posts__post_comment.ts
Normal file
306
src/lib/ae_posts/ae_posts__post_comment.ts
Normal file
@@ -0,0 +1,306 @@
|
||||
import type { key_val } from '$lib/ae_stores';
|
||||
import { api } from '$lib/api';
|
||||
|
||||
import { db_posts } from "$lib/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,
|
||||
try_cache = true,
|
||||
log_lvl = 0
|
||||
}: {
|
||||
api_cfg: any,
|
||||
post_comment_id: string,
|
||||
try_cache?: boolean,
|
||||
log_lvl?: number
|
||||
}
|
||||
) {
|
||||
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]
|
||||
|
||||
});
|
||||
}
|
||||
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-09-25
|
||||
export async function load_ae_obj_li__post_comment(
|
||||
{
|
||||
api_cfg,
|
||||
for_obj_type = 'post',
|
||||
for_obj_id,
|
||||
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,
|
||||
order_by_li?: key_val,
|
||||
params?: key_val,
|
||||
try_cache?: boolean,
|
||||
log_lvl?: number
|
||||
}
|
||||
) {
|
||||
console.log(`*** load_ae_obj_li__post_comment() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
|
||||
|
||||
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
|
||||
let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden
|
||||
let limit: number = (params.qry__limit ?? 99); // 99
|
||||
let offset: number = (params.qry__offset ?? 0); // 0
|
||||
|
||||
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({
|
||||
api_cfg: api_cfg,
|
||||
obj_type: 'post_comment',
|
||||
for_obj_type: for_obj_type,
|
||||
for_obj_id: for_obj_id,
|
||||
use_alt_table: true, // 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
|
||||
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
|
||||
});
|
||||
}
|
||||
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={},
|
||||
log_lvl=0
|
||||
}: {
|
||||
api_cfg: any,
|
||||
post_id: string,
|
||||
data_kv: key_val,
|
||||
params?: key_val,
|
||||
log_lvl?: number
|
||||
}
|
||||
) {
|
||||
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) {
|
||||
db_save_ae_obj_li__post_comment(
|
||||
{
|
||||
obj_type: 'post_comment',
|
||||
obj_li: [post_comment_obj_create_result]
|
||||
});
|
||||
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-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]
|
||||
});
|
||||
}
|
||||
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,
|
||||
|
||||
// name: obj.name,
|
||||
// summary: obj.summary,
|
||||
title: obj.title,
|
||||
content: obj.content,
|
||||
|
||||
anonymous: obj.anonymous,
|
||||
full_name: obj.full_name,
|
||||
email: obj.email,
|
||||
|
||||
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
|
||||
});
|
||||
// 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);
|
||||
}
|
||||
|
||||
// const id_random = await db_posts.comment.put(obj);
|
||||
// console.log(`Put obj with ID: ${obj.post_comment_id_random}`);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user