225 lines
3.8 KiB
TypeScript
225 lines
3.8 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.group
|
||
|
{
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
var _store : (
|
||
|
null
|
||
|
|
|
||
|
lib_plankton.storage.type_store<
|
||
|
_espe.type.group_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.group_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": "groups",
|
||
|
"key_name": "id",
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// do nothing
|
||
|
}
|
||
|
return _store;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
type type_dispersal = Record<string, any>;
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function encode(
|
||
|
object : _espe.type.group_object
|
||
|
) : type_dispersal
|
||
|
{
|
||
|
return {
|
||
|
"name": object.name,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function decode(
|
||
|
dispersal : type_dispersal
|
||
|
) : _espe.type.group_object
|
||
|
{
|
||
|
return {
|
||
|
"name": dispersal.core_row["name"],
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @todo optimize
|
||
|
*/
|
||
|
export async function list(
|
||
|
search_term : (null | string)
|
||
|
) : Promise<
|
||
|
Array<
|
||
|
{
|
||
|
id : _espe.type.group_id;
|
||
|
preview : {
|
||
|
name : string;
|
||
|
};
|
||
|
}
|
||
|
>
|
||
|
>
|
||
|
{
|
||
|
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": {
|
||
|
"name": preview["name"],
|
||
|
}
|
||
|
})
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function read(
|
||
|
id : _espe.type.group_id
|
||
|
) : Promise<_espe.type.group_object>
|
||
|
{
|
||
|
const core_row : Record<string, any> = await get_store().read(id);
|
||
|
|
||
|
const dispersal : type_dispersal = {
|
||
|
"core_row": core_row,
|
||
|
};
|
||
|
|
||
|
return decode(dispersal);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function create(
|
||
|
value : _espe.type.group_object
|
||
|
) : Promise<_espe.type.group_id>
|
||
|
{
|
||
|
const dispersal : type_dispersal = encode(value);
|
||
|
|
||
|
// core
|
||
|
const id : _espe.type.group_id = await get_store().create(dispersal.core_row);
|
||
|
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @todo replace groups smartly
|
||
|
*/
|
||
|
export async function update(
|
||
|
id : _espe.type.group_id,
|
||
|
value : _espe.type.group_object
|
||
|
) : Promise<void>
|
||
|
{
|
||
|
const dispersal : type_dispersal = encode(value);
|
||
|
|
||
|
// core
|
||
|
await get_store().update(id, dispersal.core_row);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function delete_(
|
||
|
id : _espe.type.group_id
|
||
|
) : Promise<void>
|
||
|
{
|
||
|
// core
|
||
|
await get_store().delete(id);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function dump(
|
||
|
) : Promise<
|
||
|
Array<
|
||
|
{
|
||
|
id : _espe.type.group_id;
|
||
|
object : _espe.type.group_object;
|
||
|
}
|
||
|
>
|
||
|
>
|
||
|
{
|
||
|
return (
|
||
|
Promise.all(
|
||
|
(await get_store().search(null))
|
||
|
.map(hit => hit.key)
|
||
|
.map(
|
||
|
id => (
|
||
|
read(id)
|
||
|
.then(
|
||
|
(object) => ({
|
||
|
"id": id,
|
||
|
"object": object
|
||
|
})
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|