backend/source/repositories/calendar.ts

255 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-09-12 00:03:29 +02:00
namespace _zeitbild.repository.calendar
{
/**
*/
var _core_store : (
null
|
lib_plankton.storage.type_core_store<
_zeitbild.type.calendar_id,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
) = null;
/**
*/
var _event_store : (
null
|
lib_plankton.storage.type_core_store<
_zeitbild.type.event_id,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
) = null;
/**
*/
function get_core_store(
) : lib_plankton.storage.type_core_store<
_zeitbild.type.calendar_id,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
{
if (_core_store === null) {
_core_store = lib_plankton.storage.sql_table_autokey_core_store(
{
"database_implementation": _zeitbild.database.get_implementation(),
"table_name": "calendars",
"key_name": "id",
}
);
}
else {
// do nothing
}
return _core_store;
}
/**
*/
function get_event_store(
) : lib_plankton.storage.type_core_store<
_zeitbild.type.event_id,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
{
if (_event_store === null) {
_event_store = lib_plankton.storage.sql_table_autokey_core_store(
{
"database_implementation": _zeitbild.database.get_implementation(),
"table_name": "events",
"key_name": "id",
}
);
}
else {
// do nothing
}
return _event_store;
}
/**
* @todo use events table
*/
function encode(
object : _zeitbild.type.calendar_object
) : Record<string, any>
{
switch (object.kind) {
/*
case "concrete": {
const data_raw : any = lib_plankton.json.encode(object.data);
data_raw["users"]
return {
"name": object.name,
"private": object.private,
"kind": object.kind,
"data": {
"users": data_raw["users"],
"events": [] // TODO
},
};
}
*/
default: {
return {
"name": object.name,
"private": object.private,
"kind": object.kind,
"data": lib_plankton.json.encode(object.data),
};
}
}
}
/**
*/
function decode(
row : Record<string, any>
) : _zeitbild.type.calendar_object
{
return {
"name": row["name"],
"private": row["private"],
"kind": row["kind"],
"data": lib_plankton.json.decode(row["data"]),
};
}
/**
*/
export async function dump(
) : Promise<
Array<
{
id : _zeitbild.type.calendar_id;
object : _zeitbild.type.calendar_object;
}
>
>
{
return (
(await get_core_store().search(null))
.map(
({"key": key, "preview": preview}) => ({
"id": key,
"object": (preview as _zeitbild.type.calendar_object),
})
)
);
}
/**
* @todo optimize
*/
export async function list(
search_term : (null | string)
) : Promise<
Array<
{
id : _zeitbild.type.calendar_id;
preview : {
name : string;
};
}
>
>
{
return (
(await get_core_store().search(null))
.filter(
({"key": key, "preview": preview}) => (
(
(search_term === null)
||
(search_term.length <= 1)
)
? true
: (
preview["name"].toLowerCase().includes(search_term.toLowerCase())
)
)
)
.map(
({"key": key, "preview": preview}) => ({
"id": key,
"preview": {
"name": preview["name"],
}
})
)
);
}
/**
*/
export async function read(
id : _zeitbild.type.calendar_id
) : Promise<_zeitbild.type.calendar_object>
{
const row : Record<string, any> = await get_core_store().read(id);
return decode(row);
}
/**
*/
export async function create(
value : _zeitbild.type.calendar_object
) : Promise<_zeitbild.type.calendar_id>
{
const row : Record<string, any> = encode(value);
const id : _zeitbild.type.calendar_id = await get_core_store().create(row);
return id;
}
/**
*/
export async function update(
id : _zeitbild.type.calendar_id,
value : _zeitbild.type.calendar_object
) : Promise<void>
{
const row : Record<string, any> = encode(value);
await get_core_store().update(id, row);
}
/**
*/
export async function delete_(
id : _zeitbild.type.calendar_id
) : Promise<void>
{
await get_core_store().delete(id);
}
}