backend/source/repositories/admin.ts

171 lines
3.3 KiB
TypeScript

/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend
Copyright (C) 2024 Christian Fraß
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
*/
namespace _espe.repository.admin
{
/**
*/
var _store : (
null
|
lib_plankton.storage.type_store<
_espe.type.admin_id,
Record<string, any>,
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
Record<string, any>
>
) = null;
/**
*/
function get_store(
) : lib_plankton.storage.type_store<
_espe.type.admin_id,
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": _espe.helpers.database_implementation(),
"table_name": "admins",
"key_name": "id",
}
);
}
else {
// do nothing
}
return _store;
}
/**
*/
type type_dispersal = Record<string, any>;
/**
*/
function encode(
object : _espe.type.admin_object
) : type_dispersal
{
return {
"name": object.name,
"email_address": object.email_address,
"password_image": object.password_image,
"password_fail_count": object.password_fail_count,
};
}
/**
*/
function decode(
dispersal : type_dispersal
) : _espe.type.admin_object
{
return {
"name": dispersal["name"],
"email_address": dispersal["email_address"],
"password_image": dispersal["password_image"],
"password_fail_count": dispersal["password_fail_count"],
};
}
/**
*/
export async function list(
search_term : (null | string)
) : Promise<
Array<
{
id : _espe.type.admin_id;
preview : _espe.type.admin_object;
}
>
>
{
return (
(await get_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": decode(preview),
})
)
);
}
/**
*/
export async function read(
id : _espe.type.admin_id
) : Promise<_espe.type.admin_object>
{
const row : Record<string, any> = await get_store().read(id);
const dispersal : type_dispersal = row;
return decode(dispersal);
}
/**
*/
export function create(
value : _espe.type.admin_object
) : Promise<_espe.type.admin_id>
{
const dispersal : type_dispersal = encode(value);
return get_store().create(dispersal);
}
/**
*/
export function update(
id : _espe.type.admin_id,
value : _espe.type.admin_object
) : Promise<void>
{
const dispersal : type_dispersal = encode(value);
return get_store().update(id, dispersal);
}
}