Skip to content

Commit

Permalink
Fix texture use creation with PR #38
Browse files Browse the repository at this point in the history
Fixes App#120
  • Loading branch information
3vorp authored Dec 21, 2023
2 parents 329fe66 + ab7ea9a commit 67175dc
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/v2/interfaces/modpacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Modpack {

export interface Modpacks extends Array<Modpack> {}

export interface FirestormModpack extends Modpack {};
export interface FirestormModpack extends Modpack {}

export interface ModpacksRepository {
getRaw(): Promise<Record<string, Modpack>>;
Expand Down
2 changes: 1 addition & 1 deletion src/v2/interfaces/mods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Mod {

export interface Mods extends Array<Mod> {}

export interface FirestormMod extends Mod {};
export interface FirestormMod extends Mod {}

export interface ModsRepository {
getRaw(): Promise<Record<string, Mod>>;
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
2 changes: 1 addition & 1 deletion src/v2/interfaces/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface WebsitePost extends CreateWebsitePost {
id: string;
}

export interface FirestormPost extends WebsitePost {};
export interface FirestormPost extends WebsitePost {}

export interface WebsitePostRepository {
getRaw(): Promise<Record<string, WebsitePost>>;
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
6 changes: 5 additions & 1 deletion 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 Down Expand Up @@ -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
38 changes: 19 additions & 19 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,32 @@ 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_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);
return this.useService.createUse({
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
13 changes: 13 additions & 0 deletions src/v2/service/use.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ 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 67175dc

Please sign in to comment.