Skip to content

Commit

Permalink
Merge pull request #207 from mytlogos/mass-request
Browse files Browse the repository at this point in the history
feat: add /user/load api
  • Loading branch information
mytlogos authored Feb 23, 2022
2 parents e8763a7 + 1736395 commit aa3ae34
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/database/contexts/internalListContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SubContext } from "./subContext";
import { List, Medium, Uuid, MultiSingleNumber, MinList, StorageList, ListMedia } from "../../types";
import { List, Medium, Uuid, MultiSingleNumber, MinList, StorageList, ListMedia, PromiseMultiSingle } from "../../types";
import { Errors, promiseMultiSingle, multiSingle } from "../../tools";
import { storeModifications } from "../sqlTools";

Expand Down Expand Up @@ -51,6 +51,14 @@ export class InternalListContext extends SubContext {
return { list: lists, media: loadedMedia };
}

public async getShallowList<T extends MultiSingleNumber>(listId: T, uuid: Uuid): PromiseMultiSingle<T, List> {
// TODO: 29.06.2019 replace with id IN (...)
return promiseMultiSingle(listId, async (id: number) => {
const result = await this.query("SELECT * FROM reading_list WHERE uuid = ? AND id = ?;", [uuid, id]);
return this.createShallowList(result[0]);
});
}

/**
* Recreates a list from storage.
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/database/contexts/mediumContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ export class MediumContext extends SubContext {
) as Promise<FullMediumToc[]>;
}

public getTocs(tocIds: number[]): Promise<FullMediumToc[]> {
return this.queryInList(
"SELECT id, medium_id as mediumId, link, " +
"countryOfOrigin, languageOfOrigin, author, title," +
"medium, artist, lang, stateOrigin, stateTL, series, universe " +
"FROM medium_toc WHERE id IN (??);",
[tocIds],
) as Promise<FullMediumToc[]>;
}

public async removeMediumToc(mediumId: number, link: string): Promise<boolean> {
const domainRegMatch = /https?:\/\/(.+?)(\/|$)/.exec(link);

Expand Down
26 changes: 17 additions & 9 deletions packages/core/src/database/contexts/partContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,25 @@ export class PartContext extends SubContext {
/**
* Returns all parts of an medium.
*/
public async getParts<T extends MultiSingleNumber>(partId: T, uuid: Uuid): Promise<Part[]> {
public async getParts<T extends MultiSingleNumber>(partId: T, uuid: Uuid, full = true): Promise<Part[]> {
const parts: Optional<any[]> = await this.queryInList("SELECT * FROM part WHERE id IN (??);", [partId]);
if (!parts || !parts.length) {
return [];
}
const partIdMap = new Map<number, any>();
const episodesResult: Optional<any[]> = await this.queryInList("SELECT id FROM episode WHERE part_id IN (??);", [
parts.map((value) => {
partIdMap.set(value.id, value);
return value.id;
}),
]);
const episodesResult: Optional<any[]> = await this.queryInList(
"SELECT id, part_id FROM episode WHERE part_id IN (??);",
[
parts.map((value) => {
partIdMap.set(value.id, value);
return value.id;
}),
],
);

const episodes = episodesResult || [];
const episodes: Array<{ id: number; part_id: number }> = episodesResult || [];

if (episodes) {
if (full) {
const episodeIds = episodes.map((value) => value.id);
const fullEpisodes = await this.parentContext.episodeContext.getEpisode(episodeIds, uuid);
fullEpisodes.forEach((value) => {
Expand All @@ -180,6 +183,11 @@ export class PartContext extends SubContext {
}
part.episodes.push(value);
});
} else {
episodes.forEach((value) => {
const part: Part = partIdMap.get(value.part_id);
(part.episodes as number[]).push(value.id);
});
}
return parts.map((part) => {
return {
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/database/contexts/queryContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Primitive,
DataStats,
NewData,
QueryItems,
QueryItemsResult,
} from "../../types";
import { Errors, getElseSet, getElseSetObj, ignore, multiSingle, promiseMultiSingle, batch } from "../../tools";
import logger from "../../logger";
Expand Down Expand Up @@ -730,6 +732,48 @@ export class QueryContext implements ConnectionContext {
};
}

public async queryItems(uuid: Uuid, query: QueryItems): Promise<QueryItemsResult> {
const [
externalUser,
externalMediaLists,
mediaLists,
mediaTocs,
tocs,
media,
parts,
partReleases,
partEpisodes,
episodes,
episodeReleases,
] = await Promise.all([
this.externalUserContext.getExternalUser(query.externalUser),
Promise.all(query.externalMediaLists.map((id) => this.externalListContext.getExternalList(id))),
this.internalListContext.getShallowList(query.mediaLists, uuid),
this.mediumContext.getMediumTocs(query.mediaTocs),
this.mediumContext.getTocs(query.tocs),
this.mediumContext.getSimpleMedium(query.media),
this.partContext.getParts(query.parts, uuid, false),
this.partContext.getPartReleases(query.partReleases),
this.partContext.getPartItems(query.partEpisodes),
this.episodeContext.getEpisode(query.episodes, uuid),
this.episodeContext.getReleases(query.episodeReleases),
]);

return {
episodeReleases, // by episode id
episodes,
partEpisodes, // by part id
partReleases, // by part id
parts,
media,
tocs, // by toc id
mediaTocs, // by medium id
mediaLists,
externalMediaLists,
externalUser,
};
}

private async _batchFunction<T>(
value: T[],
query: string,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/database/storages/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
Nullable,
DataStats,
NewData,
QueryItems,
QueryItemsResult,
} from "../../types";
import logger from "../../logger";
import { databaseSchema } from "../databaseSchema";
Expand Down Expand Up @@ -341,6 +343,10 @@ export class Storage {
return inContext((context) => context.getNew(uuid, date));
}

public queryItems(uuid: Uuid, query: QueryItems): Promise<QueryItemsResult> {
return inContext((context) => context.queryItems(uuid, query));
}

/**
*
* @param result
Expand Down
28 changes: 28 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,3 +1871,31 @@ export interface CustomHook {
hookState: HookState;
comment: string;
}

export interface QueryItems {
episodeReleases: number[]; // by episode id
episodes: number[];
partEpisodes: number[]; // by part id
partReleases: number[]; // by part id
parts: number[];
media: number[];
tocs: number[]; // by toc id
mediaTocs: number[]; // by medium id
mediaLists: number[];
externalMediaLists: number[];
externalUser: string[];
}

export interface QueryItemsResult {
episodeReleases: EpisodeRelease[]; // by episode id
episodes: Episode[];
partEpisodes: Record<number, number[]>; // by part id
partReleases: Record<number, SimpleRelease[]>; // by part id
parts: Part[];
media: SimpleMedium[];
tocs: FullMediumToc[]; // by toc id
mediaTocs: FullMediumToc[]; // by medium id
mediaLists: List[];
externalMediaLists: ExternalList[];
externalUser: ExternalUser[];
}
9 changes: 9 additions & 0 deletions packages/server/src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {
AppEventProgram,
AppEventType,
AppEvent,
QueryItems,
QueryItemsResult,
Uuid,
} from "enterprise-core/dist/types";
import { Handler, Router } from "express";
import { extractQueryParam, createHandler } from "./apiTools";
Expand Down Expand Up @@ -386,6 +389,11 @@ const getStatus = createHandler(async (): Promise<Status> => {
};
});

const postLoad = createHandler(async (request): Promise<QueryItemsResult> => {
const { items, uuid }: { items: QueryItems; uuid: Uuid } = request.body;
return storage.queryItems(uuid, items);
});

/**
* Creates the User Api Router.
*
Expand Down Expand Up @@ -891,6 +899,7 @@ export function userRouter(): Router {
*/
router.get("/events", getAllAppEvents);
router.get("/status", getStatus);
router.post("/load", postLoad);

router.use("/medium", mediumRouter());
router.use("/jobs", jobsRouter());
Expand Down

0 comments on commit aa3ae34

Please sign in to comment.