Skip to content

Commit

Permalink
Complete needed webapp backend (PR #34 )
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp authored Nov 3, 2023
2 parents 9ab702b + 093bbf7 commit 8985a36
Show file tree
Hide file tree
Showing 18 changed files with 361 additions and 198 deletions.
230 changes: 115 additions & 115 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"@types/body-parser": "^1.19.4",
"@types/express": "^4.17.20",
"@types/multer": "^1.4.9",
"@types/node": "^20.8.7",
"@types/node": "^20.8.10",
"@types/swagger-ui-express": "^4.1.5",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.52.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
Expand All @@ -40,11 +40,11 @@
"dependencies": {
"@tsoa/runtime": "^5.0.0",
"api-error-handler": "^1.0.0",
"axios": "^1.5.1",
"axios": "^1.6.0",
"body-parser": "^1.20.2",
"cloudflare": "^2.9.1",
"cors": "^2.8.5",
"discord-api-types": "^0.37.60",
"discord-api-types": "^0.37.62",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"firestorm-db": "^1.10.0",
Expand Down
19 changes: 18 additions & 1 deletion src/v2/controller/path.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Body,
Put,
} from "tsoa";
import { Path, InputPath, Paths } from "../interfaces";
import { Path, InputPath, Paths, PathNewVersionParam } from "../interfaces";
import PathService from "../service/path.service";

@Route("paths")
Expand Down Expand Up @@ -60,6 +60,23 @@ export class PathsController extends Controller {
});
}

@Put("versions/modify/{old_version}/{new_version}")
@Security("bot")
@Security("discord", ["administrator"])
public async modifyVersion(
@URLPath() old_version: string,
@URLPath() new_version: string,
): Promise<void> {
return this.service.modifyVersion(old_version, new_version);
}

@Post("versions/add")
@Security("bot")
@Security("discord", ["administrator"])
public async addVersion(@Body() body: PathNewVersionParam): Promise<void> {
return this.service.addVersion(body);
}

/**
* Delete use by internal id (e.g. 6096bcd96fb8b)
* @param {String} id Internal ID
Expand Down
10 changes: 8 additions & 2 deletions src/v2/controller/texture.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Path,
Post,
Put,
Query,
Request,
Response,
Route,
Expand All @@ -17,13 +18,13 @@ import { Request as ExRequest, Response as ExResponse } from "express";
import { Contributions, Paths, Texture, Textures, Uses } from "../interfaces";
import {
Edition,
CreatedTextures,
KnownPacks,
TextureCreationParam,
TextureMCMETA,
TextureProperty,
TextureAll,
TexturesAll,
EntireTextureToCreate,
} from "../interfaces/textures";
import TextureService from "../service/texture.service";
import { NotFoundError } from "../tools/ApiError";
Expand Down Expand Up @@ -83,6 +84,11 @@ export class TextureController extends Controller {
return this.service.getVersionByEdition(edition);
}

@Get("search")
public searchTexture(@Query() tag?: string, @Query() name?: string): Promise<Texture[]> {
return this.service.searchByNameIdAndTag(tag, name);
}

/**
* Get a texture by ID or name
* @param id_or_name Texture ID or texture name (join by "," if multiple)
Expand Down Expand Up @@ -172,7 +178,7 @@ export class TextureController extends Controller {
@Post("multiple")
@Security("bot")
@Security("discord", ["administrator"])
public async createMultipleTextures(@Body() body: CreatedTextures): Promise<Textures> {
public async createMultipleTextures(@Body() body: EntireTextureToCreate[]): Promise<Textures> {
return this.service.createEntireTextures(body);
}

Expand Down
4 changes: 2 additions & 2 deletions src/v2/firestorm/textures/contributions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import firestorm from "firestorm-db";
import { Texture, User } from "~/v2/interfaces";
import { Texture, Users } from "~/v2/interfaces";
import { users } from "..";
import { textures } from ".";
import config from "../config";

config();

export const contributions = firestorm.collection("contributions", (el) => {
el.getContributors = (): Promise<User> => users.searchKeys(el.contributors || []);
el.getContributors = (): Promise<Users> => users.searchKeys(el.contributors || []);
el.getTexture = (): Promise<Texture> => textures.get(el.textureID);

return el;
Expand Down
5 changes: 3 additions & 2 deletions src/v2/firestorm/textures/paths.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import firestorm from "firestorm-db";
import { Texture, Use } from "~/v2/interfaces";
import { Texture } from "~/v2/interfaces";
import config from "../config";

import { uses } from "./uses";
import { OldUse } from "~/v2/tools/mapping/textures";

config();

export const paths = firestorm.collection("paths", (el) => {
el.use = async (): Promise<Use> => uses.get(el.useID);
el.use = async (): Promise<OldUse> => uses.get(el.useID);

el.texture = (): Promise<Texture> =>
new Promise((resolve, reject) => {
Expand Down
5 changes: 3 additions & 2 deletions src/v2/firestorm/textures/uses.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import firestorm from "firestorm-db";
import { Paths, Texture } from "~/v2/interfaces";
import { Texture } from "~/v2/interfaces";
import config from "../config";

import { textures } from ".";
import { paths } from "./paths";
import { OldPaths } from "~/v2/tools/mapping/textures";

config();

export const uses = firestorm.collection("uses", (el) => {
el.texture = (): Promise<Texture> => textures.get(el.textureID);

el.paths = (): Promise<Paths> =>
el.paths = (): Promise<OldPaths> =>
paths.search([
{
field: "useID",
Expand Down
1 change: 1 addition & 0 deletions src/v2/interfaces/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ContributionCreationParams {
export interface Contribution extends ContributionCreationParams {
id: string; // contribution unique id
}

export interface Contributions extends Array<Contribution> {}
export interface ContributionsAuthor {
id: string; // discord user id
Expand Down
10 changes: 10 additions & 0 deletions src/v2/interfaces/paths.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Edition } from "./textures";

export interface CreationPath {
name: string; // texture path ('textures/block/stone.png')
versions: Array<string>; // MC versions
Expand All @@ -15,12 +17,20 @@ export interface Path extends InputPath {
export interface Paths extends Array<Path> {}

export interface PathRepository {
addNewVersionToVersion(version: string, newVersion: string): void | PromiseLike<void>;
getPathById(path_id: string): Promise<Path>;
getPathUseById(use_id: string): Promise<Paths>;
getPathsByUseIdsAndVersion(use_ids: string[], version: string): Promise<Paths>;
createPath(path: 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>;
removePathsByBulk(path_ids: string[]): Promise<void>;
getRaw(): Promise<Paths>;
}

export interface PathNewVersionParam {
edition: Edition;
version: string;
newVersion: string;
}
13 changes: 8 additions & 5 deletions src/v2/interfaces/textures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreatedUse, Uses } from "./uses";
import { EntireUseToCreate, Uses } from "./uses";
import { Paths } from "./paths";
import { Contributions } from "./contributions";

Expand Down Expand Up @@ -38,19 +38,22 @@ export const OurPacksArr = [
export const DefaultPacksArr = ["default", "progart"] as const;
export const KnownPacksArr = [...DefaultPacksArr, ...OurPacksArr] as const;

export interface CreatedTexture extends TextureCreationParam {
uses: CreatedUse[];
export interface EntireTextureToCreate extends TextureCreationParam {
uses: EntireUseToCreate[];
}

export interface CreatedTextures extends Array<CreatedTexture> {}
export type KnownPacks = (typeof KnownPacksArr)[number];
export type Edition = "java" | "bedrock" | "dungeons";
export type TextureProperty = "uses" | "paths" | "contributions" | "mcmeta" | "all" | null;

export interface TextureRepository {
changeTexture(id: string, body: TextureCreationParam): Promise<Texture>;
getRaw(): Promise<Textures>;
getByNameIdAndTag(tag?: string, search?: string): Promise<Textures>;
getByNameIdAndTag(
tag: string | undefined,
search: string | undefined,
forcePartial?: boolean,
): Promise<Textures>;
getTextureById(id: number, property: TextureProperty): Promise<Texture>;
getVersions(): Promise<Array<string>>;
getEditions(): Promise<Array<string>>;
Expand Down
16 changes: 10 additions & 6 deletions src/v2/interfaces/uses.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { CreationPath } from "./paths";

export interface CreationUse {
export interface BaseUse {
name: string; // use name
texture: number; // texture id
edition: string; // game edition
}

export interface CreationUse extends BaseUse {
texture: number; // texture id
}

export interface EntireUseToCreate extends BaseUse {
paths: CreationPath[]; // all the paths to be created
}

export interface Use extends CreationUse {
id: string; // use unique id
}

export interface Uses extends Array<Use> {}

export interface CreatedUse extends CreationUse {
paths: CreationPath[];
}

export interface UseRepository {
getUsesByIdAndEdition(id_arr: number[], edition: string): Promise<Uses>;
getUsesByEdition(edition: string): Promise<Uses>;
getRaw(): Promise<Uses>;
getUseByIdOrName(id_or_name: string): Promise<Uses | Use>;
deleteUse(id: string): Promise<void>;
Expand Down
41 changes: 40 additions & 1 deletion src/v2/repository/firestorm/path.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InputPath, Path, Paths, PathRepository } from "~/v2/interfaces";
import { mapPath, unmapPath, mapPaths } from "../../tools/mapping/textures";
import { mapPath, unmapPath, mapPaths, OldPath } from "../../tools/mapping/textures";
import { paths } from "../../firestorm/textures/paths";

export default class PathFirestormRepository implements PathRepository {
Expand Down Expand Up @@ -55,6 +55,45 @@ export default class PathFirestormRepository implements PathRepository {
return paths.set(path_id, unmapPath(path)).then(() => this.getPathById(path_id));
}

/**
* Changes all the old version presence in all paths with the new one
* @param old_version old version to remove on paths versions array
* @param new_version new version to replace the old version
*/
modifyVersion(old_version: string, new_version: string): void | PromiseLike<void> {
return this.getRaw()
.then((r) => {
const old: OldPath[] = Object.values(r);
const filtered = old.filter((p) => p.versions.includes(old_version));
const edits = filtered.map((p) => ({
id: p.id,
field: "versions",
operation: "set",
value: p.versions.map((v) => (v === old_version ? new_version : v)),
}));

return paths.editFieldBulk(edits);
})
.then(() => {});
}

addNewVersionToVersion(version: string, newVersion: string): void | PromiseLike<void> {
return this.getRaw()
.then((r) => {
const old: OldPath[] = Object.values(r);
const filtered = old.filter((p) => p.versions.includes(version));
const edits = filtered.map((p) => ({
id: p.id,
field: "versions",
operation: "array-push",
value: newVersion,
}));

return paths.editFieldBulk(edits);
})
.then(() => {});
}

getRaw() {
return paths.read_raw();
}
Expand Down
26 changes: 19 additions & 7 deletions src/v2/repository/firestorm/texture.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ import {
TextureRepository,
Path,
} from "../../interfaces";
import { mapTexture, mapTextures, OldUse, unmapTexture } from "../../tools/mapping/textures";
import {
mapTexture,
mapTextures,
OldUse,
unmapTexture,
unmapTextureCreation,
} from "../../tools/mapping/textures";

export default class TextureFirestormRepository implements TextureRepository {
async getByNameIdAndTag(tag?: string, search?: string): Promise<Textures> {
async getByNameIdAndTag(
tag: string | undefined,
search: string | undefined,
forcePartial: boolean = false,
): Promise<Textures> {
// * none, read raw
if (tag === undefined && search === undefined) {
return this.getRaw();
Expand Down Expand Up @@ -53,9 +63,9 @@ export default class TextureFirestormRepository implements TextureRepository {
}

// with search
let partial = false;
let partial = false || forcePartial;
if (search !== undefined) {
partial = search.startsWith("_") || search.endsWith("_");
partial = search.startsWith("_") || search.endsWith("_") || forcePartial;

criterias.push({
field: "name",
Expand Down Expand Up @@ -182,12 +192,12 @@ export default class TextureFirestormRepository implements TextureRepository {
public getTags(): Promise<Array<string>> {
return textures
.select({
fields: ["type"], // TODO: change with tags
fields: ["type", "tags"], // TODO: change with tags
})
.then((res: any) =>
(
Object.values(res).reduce((acc: Array<string>, cur: any) => {
const types = cur.type;
const types = cur.type || cur.tags;
acc.push(types);
return acc;
}, []) as Array<string>
Expand Down Expand Up @@ -224,7 +234,9 @@ export default class TextureFirestormRepository implements TextureRepository {
}

public createTexture(texture: TextureCreationParam): Promise<Texture> {
return textures.add(texture).then((id: string) => textures.get(id));
return textures
.add(unmapTextureCreation(texture))
.then((id: string) => this.searchTextureByNameOrId(id));
}

public async deleteTexture(id: string): Promise<void> {
Expand Down
12 changes: 12 additions & 0 deletions src/v2/repository/firestorm/use.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { mapUse, mapUses, unmapUse } from "../../tools/mapping/textures";
import { paths as pathsCollection, uses } from "../../firestorm";

export default class UseFirestormRepository implements UseRepository {
getUsesByEdition(edition: string): Promise<Uses> {
return uses
.search([
{
field: "editions",
criteria: "array-contains",
value: edition,
},
])
.then(mapUses); // TODO: remove this after rewrite
}

getUsesByIdAndEdition(id_arr: number[], edition: string): Promise<Uses> {
return uses
.search([
Expand Down
Loading

0 comments on commit 8985a36

Please sign in to comment.