mirror of
https://github.com/StepanovPlaton/torrent_frontend.git
synced 2026-04-04 04:40:50 +04:00
Refactoring
This commit is contained in:
@@ -85,20 +85,16 @@ import { ItemService } from "./item";
|
||||
export { ItemService };
|
||||
|
||||
import {
|
||||
isSection,
|
||||
TypesOfItems,
|
||||
type IItemService,
|
||||
type ItemType,
|
||||
type ItemCardType,
|
||||
type ItemCreateType,
|
||||
type TypesOfItems,
|
||||
type ItemSectionsType,
|
||||
ItemSections,
|
||||
} from "./types";
|
||||
export {
|
||||
isSection,
|
||||
TypesOfItems,
|
||||
type IItemService,
|
||||
type ItemType,
|
||||
type ItemCardType,
|
||||
type ItemCreateType,
|
||||
type TypesOfItems,
|
||||
type ItemSectionsType,
|
||||
ItemSections,
|
||||
};
|
||||
|
||||
@@ -7,10 +7,8 @@ import { audiobookCreateSchema } from "./audiobook/schemas/audiobook";
|
||||
import { AudiobookService } from "./audiobook/audiobook";
|
||||
import {
|
||||
IItemService,
|
||||
ItemCardType,
|
||||
ItemCreateType,
|
||||
ItemPropertiesDescriptionType,
|
||||
ItemSectionsType,
|
||||
ItemType,
|
||||
TypesOfItems,
|
||||
UnionItemType,
|
||||
@@ -18,9 +16,8 @@ import {
|
||||
import { EraseCacheByTags } from "@/shared/utils/http";
|
||||
|
||||
export abstract class ItemService {
|
||||
private static get itemsConfiguration(): {
|
||||
static get itemsConfiguration(): {
|
||||
[k in TypesOfItems]: {
|
||||
sectionUrl: ItemSectionsType;
|
||||
formResolver: ZodSchema;
|
||||
propertiesDescription: ItemPropertiesDescriptionType<UnionItemType>;
|
||||
service: IItemService;
|
||||
@@ -28,19 +25,16 @@ export abstract class ItemService {
|
||||
} {
|
||||
return {
|
||||
[TypesOfItems.game]: {
|
||||
sectionUrl: "games",
|
||||
formResolver: gameCreateSchema,
|
||||
propertiesDescription: GameService.propertiesDescription,
|
||||
service: GameService,
|
||||
},
|
||||
[TypesOfItems.movie]: {
|
||||
sectionUrl: "movies",
|
||||
formResolver: movieCreateSchema,
|
||||
propertiesDescription: MovieService.propertiesDescription,
|
||||
service: MovieService,
|
||||
},
|
||||
[TypesOfItems.audiobook]: {
|
||||
sectionUrl: "audiobooks",
|
||||
formResolver: audiobookCreateSchema,
|
||||
propertiesDescription: AudiobookService.propertiesDescription,
|
||||
service: AudiobookService,
|
||||
@@ -48,76 +42,12 @@ export abstract class ItemService {
|
||||
};
|
||||
}
|
||||
|
||||
static get itemSections(): {
|
||||
[k in ItemSectionsType]: {
|
||||
sectionName: string;
|
||||
itemType: TypesOfItems;
|
||||
popularSubsectionName: string;
|
||||
sectionInviteText: string;
|
||||
addItemText: string;
|
||||
sectionDescription: string;
|
||||
service: IItemService;
|
||||
};
|
||||
} {
|
||||
return {
|
||||
games: {
|
||||
sectionName: "Игры",
|
||||
itemType: TypesOfItems.game,
|
||||
popularSubsectionName: "Популярные игры",
|
||||
sectionInviteText: 'Перейти в раздел "Игры"',
|
||||
addItemText: "Добавить игру",
|
||||
sectionDescription:
|
||||
"каталог .torrent файлов для обмена актуальными версиями популярных игр",
|
||||
service: GameService,
|
||||
},
|
||||
movies: {
|
||||
sectionName: "Фильмы",
|
||||
itemType: TypesOfItems.movie,
|
||||
popularSubsectionName: "Популярные фильмы",
|
||||
sectionInviteText: 'Перейти в раздел "Фильмы"',
|
||||
addItemText: "Добавить фильм",
|
||||
sectionDescription:
|
||||
"каталог .torrent файлов для обмена популярными фильмами в лучшем качестве",
|
||||
service: MovieService,
|
||||
},
|
||||
audiobooks: {
|
||||
sectionName: "Аудиокниги",
|
||||
itemType: TypesOfItems.audiobook,
|
||||
popularSubsectionName: "Популярные аудиокниги",
|
||||
sectionInviteText: 'Перейти в раздел "Аудиокниги"',
|
||||
addItemText: "Добавить аудиокнигу",
|
||||
sectionDescription:
|
||||
"каталог .torrent файлов для обмена популярными аудиокнигами любимых авторов",
|
||||
service: AudiobookService,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static isExistingItem(
|
||||
item: ItemCreateType | ItemType
|
||||
): item is ItemType {
|
||||
return (item as ItemType).id !== undefined;
|
||||
}
|
||||
|
||||
public static GetFormResolver(
|
||||
item: ItemCardType | ItemCreateType | ItemType
|
||||
) {
|
||||
return this.itemsConfiguration[item.type].formResolver;
|
||||
}
|
||||
|
||||
public static GetSectionUrlByItemType(
|
||||
item: ItemCardType | ItemCreateType | ItemType
|
||||
) {
|
||||
return this.itemsConfiguration[item.type].sectionUrl;
|
||||
}
|
||||
|
||||
public static GetPropertiesDescriptionForItem<
|
||||
T extends ItemType | ItemCreateType
|
||||
>(item: T) {
|
||||
return this.itemsConfiguration[item.type]
|
||||
.propertiesDescription as ItemPropertiesDescriptionType<T>;
|
||||
}
|
||||
|
||||
public static async AddItem(itemInfo: ItemCreateType) {
|
||||
const item = await this.itemsConfiguration[itemInfo.type].service.Add(
|
||||
itemInfo
|
||||
|
||||
@@ -16,30 +16,12 @@ export type ItemCreateType =
|
||||
| AudiobookCreateType;
|
||||
|
||||
export type UnionItemType = GameType & MovieType & AudiobookType;
|
||||
export type UnionItemCardType = GameCardType &
|
||||
MovieCardType &
|
||||
AudiobookCardType;
|
||||
export type UnionItemCreateType = GameCreateType &
|
||||
MovieCreateType &
|
||||
AudiobookCreateType;
|
||||
|
||||
export enum TypesOfItems {
|
||||
game,
|
||||
movie,
|
||||
audiobook,
|
||||
}
|
||||
|
||||
export type ItemSectionsType = "games" | "movies" | "audiobooks";
|
||||
export const ItemSections = [
|
||||
"games",
|
||||
"movies",
|
||||
"audiobooks",
|
||||
] as ItemSectionsType[];
|
||||
|
||||
export const isSection = (a: string): a is ItemSectionsType => {
|
||||
return (ItemSections as string[]).includes(a);
|
||||
};
|
||||
|
||||
export type ItemPropertiesDescriptionType<T extends ItemType | ItemCreateType> =
|
||||
{
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user