feat(backend): Added skeleton for a database learning object/path provider.
This commit is contained in:
parent
cc6947dd3c
commit
2d9f17484c
5 changed files with 103 additions and 6 deletions
|
@ -0,0 +1,42 @@
|
|||
import {LearningObjectProvider} from "./learning-object-provider";
|
||||
import {
|
||||
FilteredLearningObject,
|
||||
LearningObjectIdentifier,
|
||||
LearningPathIdentifier
|
||||
} from "../../interfaces/learning-content";
|
||||
|
||||
/**
|
||||
* Service providing access to data about learning objects from the database
|
||||
*/
|
||||
const databaseLearningObjectProvider: LearningObjectProvider = {
|
||||
/**
|
||||
* Fetches a single learning object by its HRUID
|
||||
*/
|
||||
getLearningObjectById(id: LearningObjectIdentifier): Promise<FilteredLearningObject | null> {
|
||||
return Promise.resolve(null); // TODO
|
||||
},
|
||||
|
||||
/**
|
||||
* Fetch full learning object data (metadata)
|
||||
*/
|
||||
getLearningObjectHTML(id: LearningObjectIdentifier): Promise<string | null> {
|
||||
return Promise.resolve(null); // TODO
|
||||
},
|
||||
|
||||
/**
|
||||
* Fetch only learning object HRUIDs
|
||||
*/
|
||||
getLearningObjectIdsFromPath(id: LearningPathIdentifier): Promise<string[]> {
|
||||
return Promise.resolve([]);// TODO
|
||||
},
|
||||
|
||||
/**
|
||||
* Obtain a HTML-rendering of the learning object with the given identifier (as a string).
|
||||
*/
|
||||
getLearningObjectsFromPath(id: LearningPathIdentifier): Promise<FilteredLearningObject[]> {
|
||||
return Promise.resolve([]); // TODO
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default databaseLearningObjectProvider;
|
|
@ -5,9 +5,15 @@ import {
|
|||
} from "../../interfaces/learning-content";
|
||||
import dwengoApiLearningObjectProvider from "./dwengo-api-learning-object-provider";
|
||||
import {LearningObjectProvider} from "./learning-object-provider";
|
||||
import {EnvVars, getEnvVar} from "../../util/envvars";
|
||||
import databaseLearningObjectProvider from "./database-learning-object-provider";
|
||||
|
||||
function getProvider(id: LearningObjectIdentifier): LearningObjectProvider {
|
||||
return dwengoApiLearningObjectProvider
|
||||
if (id.hruid.startsWith(getEnvVar(EnvVars.UserContentPrefix))) {
|
||||
return databaseLearningObjectProvider;
|
||||
} else {
|
||||
return dwengoApiLearningObjectProvider;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import {LearningPathProvider} from "./learning-path-provider";
|
||||
import {LearningPath, LearningPathResponse} from "../../interfaces/learning-content";
|
||||
|
||||
/**
|
||||
* Service providing access to data about learning paths from the database.
|
||||
*/
|
||||
const databaseLearningPathProvider: LearningPathProvider = {
|
||||
/**
|
||||
* Fetch the learning paths with the given hruids from the database.
|
||||
*/
|
||||
fetchLearningPaths(hruids: string[], language: string, source: string): Promise<LearningPathResponse> {
|
||||
throw new Error("Not yet implemented"); // TODO
|
||||
},
|
||||
|
||||
/**
|
||||
* Search learning paths in the database using the given search string.
|
||||
*/
|
||||
searchLearningPaths(query: string, language: string): Promise<LearningPath[]> {
|
||||
return Promise.resolve([]); // TODO
|
||||
}
|
||||
}
|
||||
|
||||
export default databaseLearningPathProvider;
|
|
@ -1,5 +1,13 @@
|
|||
import {LearningPath, LearningPathResponse} from "../../interfaces/learning-content";
|
||||
import {
|
||||
LearningPath,
|
||||
LearningPathResponse
|
||||
} from "../../interfaces/learning-content";
|
||||
import dwengoApiLearningPathProvider from "./dwengo-api-learning-path-provider";
|
||||
import databaseLearningPathProvider from "./database-learning-path-provider";
|
||||
import {EnvVars, getEnvVar} from "../../util/envvars";
|
||||
|
||||
const userContentPrefix = getEnvVar(EnvVars.UserContentPrefix);
|
||||
const allProviders = [dwengoApiLearningPathProvider, databaseLearningPathProvider]
|
||||
|
||||
/**
|
||||
* Service providing access to data about learning paths from the appropriate data source (database or Dwengo-api)
|
||||
|
@ -8,15 +16,32 @@ const learningPathService = {
|
|||
/**
|
||||
* Fetch the learning paths with the given hruids from the data source.
|
||||
*/
|
||||
fetchLearningPaths(hruids: string[], language: string, source: string): Promise<LearningPathResponse> {
|
||||
return dwengoApiLearningPathProvider.fetchLearningPaths(hruids, language, source);
|
||||
async fetchLearningPaths(hruids: string[], language: string, source: string): Promise<LearningPathResponse> {
|
||||
const userContentHruids = hruids.filter(hruid => hruid.startsWith(userContentPrefix));
|
||||
const nonUserContentHruids = hruids.filter(hruid => !hruid.startsWith(userContentPrefix));
|
||||
|
||||
const userContentLearningPaths =
|
||||
await databaseLearningPathProvider.fetchLearningPaths(userContentHruids, language, source);
|
||||
const nonUserContentLearningPaths
|
||||
= await databaseLearningPathProvider.fetchLearningPaths(nonUserContentHruids, language, source);
|
||||
|
||||
return {
|
||||
data: (userContentLearningPaths.data || []).concat(nonUserContentLearningPaths.data || []),
|
||||
source: source,
|
||||
success: userContentLearningPaths.success || nonUserContentLearningPaths.success
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Search learning paths in the data source using the given search string.
|
||||
*/
|
||||
searchLearningPaths(query: string, language: string): Promise<LearningPath[]> {
|
||||
return dwengoApiLearningPathProvider.searchLearningPaths(query, language);
|
||||
async searchLearningPaths(query: string, language: string): Promise<LearningPath[]> {
|
||||
const providerResponses = await Promise.all(
|
||||
allProviders.map(
|
||||
provider => provider.searchLearningPaths(query, language)
|
||||
)
|
||||
);
|
||||
return providerResponses.flat();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ export const EnvVars: { [key: string]: EnvVar } = {
|
|||
DbUpdate: { key: DB_PREFIX + 'UPDATE', defaultValue: false },
|
||||
LearningContentRepoApiBaseUrl: { key: PREFIX + "LEARNING_CONTENT_REPO_API_BASE_URL", defaultValue: "https://dwengo.org/backend/api"},
|
||||
FallbackLanguage: { key: PREFIX + "FALLBACK_LANGUAGE", defaultValue: "nl" },
|
||||
UserContentPrefix: { key: DB_PREFIX + 'USER_CONTENT_PREFIX', defaultValue: "u_" },
|
||||
} as const;
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue