backend/source/repositories/local_resource_event.ts
Fenris Wolf a17b7dd785 [fix]
2024-09-25 15:28:25 +02:00

180 lines
3.7 KiB
TypeScript

namespace _zeitbild.repository.local_resource_event
{
/**
*/
var _store : (
null
|
lib_plankton.storage.type_store<
int,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
) = null;
/**
*/
function get_store(
) : lib_plankton.storage.type_store<
int,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
{
if (_store === null) {
_store = lib_plankton.storage.sql_table_autokey_store(
{
"database_implementation": _zeitbild.database.get_implementation(),
"table_name": "events",
"key_name": "id",
}
);
}
else {
// do nothing
}
return _store;
}
/**
*/
function encode(
event : _zeitbild.type_event_object
) : Record<string, any>
{
const encode_datetime : ((datetime : _zeitbild.helpers.type_datetime) => string) = ((datetime) => {
return lib_plankton.string.coin(
"{{timezone_shift}}|{{date}}{{macro_time}}",
{
"timezone_shift": datetime.timezone_shift.toFixed(0).padStart(2, "0"),
"date": lib_plankton.string.coin(
"{{year}}-{{month}}-{{day}}",
{
"year": datetime.date.year.toFixed(0).padStart(4, "0"),
"month": datetime.date.month.toFixed(0).padStart(2, "0"),
"day": datetime.date.day.toFixed(0).padStart(2, "0"),
}
),
"macro_time": (
(datetime.time === null)
?
""
:
lib_plankton.string.coin(
"T{{hour}}:{{minute}}:{{second}}",
{
"hour": datetime.time.hour.toFixed(0).padStart(2, "0"),
"minute": datetime.time.minute.toFixed(0).padStart(2, "0"),
"second": datetime.time.second.toFixed(0).padStart(2, "0"),
}
)
),
}
);
});
return {
"name": event.name,
"begin": encode_datetime(event.begin),
"end": (
(event.end === null)
?
null
:
encode_datetime(event.end)
),
"location": event.location,
"description": event.description,
}
}
/**
*/
function decode(
row : Record<string, any>
) : _zeitbild.type_event_object
{
const decode_datetime : ((datetime_raw : string) => _zeitbild.helpers.type_datetime) = ((datetime_raw) => {
const parts : Array<string> = datetime_raw.split("|");
const timezone_shift : int = parseInt(parts[0]);
if (parts[1].length <= 10) {
return {
"timezone_shift": timezone_shift,
"date": {
"year": parseInt(parts[1].slice(0, 4)),
"month": parseInt(parts[1].slice(5, 7)),
"day": parseInt(parts[1].slice(8, 10)),
},
"time": null
};
}
else {
return {
"timezone_shift": timezone_shift,
"date": {
"year": parseInt(parts[1].slice(0, 4)),
"month": parseInt(parts[1].slice(5, 7)),
"day": parseInt(parts[1].slice(8, 10)),
},
"time": {
"hour": parseInt(parts[1].slice(11, 13)),
"minute": parseInt(parts[1].slice(14, 16)),
"second": parseInt(parts[1].slice(17, 19)),
}
};
}
});
return {
"name": row["name"],
"begin": decode_datetime(row["begin"]),
"end": (
(row["end"] === null)
?
null
:
decode_datetime(row["end"])
),
"location": row["location"],
"description": row["description"],
};
}
/**
*/
export function read(
event_id : _zeitbild.type_event_id
) : Promise<_zeitbild.type_event_object>
{
return (
get_store().read(event_id)
.then(
row => Promise.resolve<_zeitbild.type_event_object>(decode(row))
)
);
}
/**
*/
export function create(
event_object : _zeitbild.type_event_object
) : Promise<_zeitbild.type_event_id>
{
return (
Promise.resolve(encode(event_object))
.then(
(row) => get_store().create(row)
)
);
}
}