Skip to content

Commit

Permalink
Fixes App#120
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRolfFR committed Dec 21, 2023
1 parent 329fe66 commit 9299ccc
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 53 deletions.
48 changes: 24 additions & 24 deletions src/v2/controller/addon.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,40 @@ export class AddonController extends Controller {

private async getAddonProperty(id: number, property: AddonProperty): Promise<Addon | Files> {
switch (property) {
case "files":
return (await this.service.getFiles(id)).map((f) => {
case "files":
return (await this.service.getFiles(id)).map((f) => {
if ((f.use === "header" || f.use === "screenshot") && f.source.startsWith("/"))
f.source = process.env.DB_IMAGE_ROOT + f.source;

if (
f.use === "download" &&
!f.source.startsWith("https://") &&
!f.source.startsWith("http://")
)
f.source = `http://${f.source}`;

return f;
});

case "all":
default:
return this.service.getAll(id).then((addon: AddonAll) => {
addon.files = addon.files.map((f) => {
if ((f.use === "header" || f.use === "screenshot") && f.source.startsWith("/"))
f.source = process.env.DB_IMAGE_ROOT + f.source;

if (
f.use === "download" &&
!f.source.startsWith("https://") &&
!f.source.startsWith("http://")
!f.source.startsWith("https://") &&
!f.source.startsWith("http://")
)
f.source = `http://${f.source}`;

return f;
});

case "all":
default:
return this.service.getAll(id).then((addon: AddonAll) => {
addon.files = addon.files.map((f) => {
if ((f.use === "header" || f.use === "screenshot") && f.source.startsWith("/"))
f.source = process.env.DB_IMAGE_ROOT + f.source;

if (
f.use === "download" &&
!f.source.startsWith("https://") &&
!f.source.startsWith("http://")
)
f.source = `http://${f.source}`;

return f;
});

return addon;
});
return addon;
});
}
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export class AddonController extends Controller {
@Get("{id_or_slug}/{property}")
public async getAddonPropertyById(
@Path() id_or_slug: string,
property: AddonProperty,
property: AddonProperty,
): Promise<Addon | Files> {
return this.service
.getAddonFromSlugOrId(id_or_slug)
Expand Down
1 change: 1 addition & 0 deletions src/v2/interfaces/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface PathRepository {
getPathUseById(use_id: string): Promise<Paths>;
getPathsByUseIdsAndVersion(use_ids: string[], version: string): Promise<Paths>;
createPath(path: InputPath): Promise<Path>;
createPathBulk(paths: InputPath[]): Promise<Path[]>;
updatePath(path_id: string, path: Path): Promise<Path>;
modifyVersion(old_version: string, new_version: string): void | PromiseLike<void>;
removePathById(path_id: string): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions src/v2/interfaces/uses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface UseRepository {
getUseByIdOrName(id_or_name: string): Promise<Uses | Use>;
deleteUse(id: string): Promise<void>;
set(use: Use): Promise<Use>;
setMultiple(uses: Use[]): Promise<Use[]>;
removeUseById(use_id: string): Promise<void>;
removeUsesByBulk(use_ids: string[]): Promise<void>;
}
6 changes: 5 additions & 1 deletion src/v2/repository/firestorm/path.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default class PathFirestormRepository implements PathRepository {

createPath(path: InputPath): Promise<Path> {
// breaks without structuredClone, not sure why
return paths.add(structuredClone(path)).then((id) => paths.get(id));
return paths.add(structuredClone(path)).then((id) => ({ ...structuredClone(path), id }) );
}

createPathBulk(pathArray: InputPath[]): Promise<Path[]> {
return paths.addBulk(pathArray).then((ids) => paths.searchKeys(ids))
}

removePathById(path_id: string): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion src/v2/repository/firestorm/use.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export default class UseFirestormRepository implements UseRepository {

set(use: Use): Promise<Use> {
// breaks without structuredClone, not sure why
return uses.set(use.id, structuredClone(use)).then((id) => uses.get(id));
return uses.set(use.id, structuredClone(use)).then(() => uses.get(use.id));
}

setMultiple(useArray: Use[]): Promise<Use[]> {
const use_ids = useArray.map(u => u.id);
return uses.setBulk(use_ids, useArray).then(() => uses.searchKeys(use_ids));
}

removeUseById(use_id: string): Promise<void> {
Expand Down
8 changes: 6 additions & 2 deletions src/v2/service/path.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequestError } from "../tools/ApiError";
import UseService from "./use.service";
import { InputPath, Path, PathNewVersionParam, Paths } from "../interfaces";
import { InputPath, Path, PathNewVersionParam, PathRepository, Paths } from "../interfaces";
import PathFirestormRepository from "../repository/firestorm/path.repository";
import TextureService from "./texture.service";
import { settings } from "../firestorm";
Expand All @@ -18,7 +18,7 @@ export default class PathService {
else this.useService = new UseService(this);
}

private readonly repository = new PathFirestormRepository();
private readonly repository: PathRepository = new PathFirestormRepository();

getRaw(): Promise<Record<string, Path>> {
return this.repository.getRaw();
Expand All @@ -35,6 +35,10 @@ export default class PathService {
.then(() => this.repository.createPath(path));
}

async createMultiplePaths(paths: InputPath[]): Promise<Path[]> {
return this.repository.createPathBulk(paths);
}

getPathById(id: string): Promise<Path> {
return this.repository.getPathById(id);
}
Expand Down
47 changes: 22 additions & 25 deletions src/v2/service/texture.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contributions, Paths, Texture, Textures, Uses } from "../interfaces";
import { Contributions, InputPath, Paths, Texture, Textures, Use, Uses } from "../interfaces";
import {
Edition,
EntireTextureToCreate,
Expand Down Expand Up @@ -97,32 +97,29 @@ export default class TextureService {
const texture_id = created_texture.id;

// create uses
const uses_created = await Promise.all(
input.uses.map(async (u, ui) => {
const use_id = String(texture_id) + String.fromCharCode("a".charCodeAt(0) + ui);
return this.useService.createUse({
name: u.name,
edition: u.edition,
texture: Number.parseInt(texture_id, 10),
id: use_id,
});
}),
);
const [use_ids, full_uses_to_create]: [string[], Use[]] = input.uses.reduce((acc,u,ui) => {
const use_id = String(texture_id) + String.fromCharCode("a".charCodeAt(0) + ui);
const use = {
name: u.name,
edition: u.edition,
texture: Number.parseInt(texture_id, 10),
id: use_id,
}
acc[0].push(use_id)
acc[1].push(use)
return acc
}, [[],[]]);
await this.useService.createMultipleUses(full_uses_to_create);

// create paths
await Promise.all(
uses_created.map((u, ui) => {
const use_id_created = u.id;
return Promise.all(
input.uses[ui].paths.map((p) =>
this.pathService.createPath({
...p,
use: use_id_created,
}),
),
);
}),
);
const paths_to_add = input.uses.reduce((acc, u, ui) => {
const paths: InputPath[] = u.paths.map(p => ({
...p,
use: use_ids[ui]
}))
return [ ...acc, ...paths ]
}, [] as InputPath[])
await this.pathService.createMultiplePaths(paths_to_add);

return created_texture;
}
Expand Down
9 changes: 9 additions & 0 deletions src/v2/service/use.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ export default class UseService {
});
});
}

createMultipleUses(uses: Use[]): Promise<Use[]> {
return Promise.all(uses.map(u => new Promise((resolve, reject) => {
this.getUseByIdOrNameAndCatch(u.id)
.then(() => reject())
.catch(() => resolve(undefined))
})))
.then(() => this.useRepo.setMultiple(uses))
}
}

0 comments on commit 9299ccc

Please sign in to comment.