backend/source/services/resource.ts

161 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-09-25 17:18:16 +02:00
/*
This file is part of »zeitbild«.
Copyright 2025 'kcf' <fenris@folksprak.org>
»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»zeitbild« 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
*/
2024-09-18 18:17:25 +02:00
namespace _zeitbild.service.resource
{
/**
*/
export function add(
2024-09-21 11:05:24 +02:00
resource_object : _zeitbild.type_resource_object
) : Promise<_zeitbild.type_resource_id>
2024-09-18 18:17:25 +02:00
{
return _zeitbild.repository.resource.create(resource_object);
}
2024-09-25 14:50:32 +02:00
/**
*/
export async function event_get(
resource_id : _zeitbild.type_resource_id,
local_resource_event_id : _zeitbild.type_local_resource_event_id
) : Promise<_zeitbild.type_event_object>
{
const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
resource_id
);
switch (resource_object.kind) {
case "local": {
const event_object : _zeitbild.type_event_object = await _zeitbild.repository.resource.local_resource_event_read(
resource_id,
local_resource_event_id
);
return Promise.resolve<_zeitbild.type_event_object>(event_object);
break;
}
case "ics_feed": {
// TODO
return Promise.reject(new Error("not implemented"));
break;
}
default: {
// @ts-ignore
throw (new Error("unhandled resource kind: " + resource_object.kind));
}
}
}
2024-09-25 14:50:32 +02:00
/**
*/
export async function event_add(
resource_id : _zeitbild.type_resource_id,
event_object : _zeitbild.type_event_object
) : Promise<_zeitbild.type_local_resource_event_id>
2024-09-25 14:50:32 +02:00
{
2024-09-25 15:28:25 +02:00
const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
2024-09-25 14:50:32 +02:00
resource_id
);
switch (resource_object.kind) {
case "local": {
const local_resource_event_id : _zeitbild.type_local_resource_event_id = await _zeitbild.repository.resource.local_resource_event_create(
2024-09-25 14:50:32 +02:00
resource_id,
event_object
2024-09-25 14:50:32 +02:00
);
return Promise.resolve<_zeitbild.type_local_resource_event_id>(local_resource_event_id);
2024-09-25 14:50:32 +02:00
break;
}
case "ics_feed": {
return Promise.reject(new Error("unavailable"));
2024-09-25 14:50:32 +02:00
break;
}
default: {
2024-09-25 15:28:25 +02:00
// @ts-ignore
throw (new Error("unhandled resource kind: " + resource_object.kind));
}
}
}
/**
*/
export async function event_change(
resource_id : _zeitbild.type_resource_id,
local_resource_event_id : _zeitbild.type_local_resource_event_id,
event_object : _zeitbild.type_event_object
) : Promise<void>
{
const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
resource_id
);
switch (resource_object.kind) {
case "local": {
await _zeitbild.repository.resource.local_resource_event_update(
resource_id,
local_resource_event_id,
event_object
);
return Promise.resolve<void>(undefined);
break;
}
case "ics_feed": {
return Promise.reject(new Error("unavailable"));
break;
}
default: {
// @ts-ignore
2024-09-25 14:50:32 +02:00
throw (new Error("unhandled resource kind: " + resource_object.kind));
}
}
}
/**
*/
export async function event_remove(
resource_id : _zeitbild.type_resource_id,
local_resource_event_id : _zeitbild.type_local_resource_event_id
2024-09-25 14:50:32 +02:00
) : Promise<void>
{
2024-09-25 15:28:25 +02:00
const resource_object : _zeitbild.type_resource_object = await _zeitbild.repository.resource.read(
2024-09-25 14:50:32 +02:00
resource_id
);
switch (resource_object.kind) {
case "local": {
await _zeitbild.repository.resource.local_resource_event_delete(
resource_id,
local_resource_event_id
2024-09-25 14:50:32 +02:00
);
return Promise.resolve<void>(undefined);
break;
}
case "ics_feed": {
return Promise.reject(new Error("unavailable"));
2024-09-25 14:50:32 +02:00
break;
}
default: {
2024-09-25 15:28:25 +02:00
// @ts-ignore
2024-09-25 14:50:32 +02:00
throw (new Error("unhandled resource kind: " + resource_object.kind));
}
}
}
2024-09-18 18:17:25 +02:00
}