Work on game edit

This commit is contained in:
2024-05-26 13:55:34 +04:00
parent 2572c43733
commit 8b6246a38c
16 changed files with 405 additions and 217 deletions

View File

@@ -1,15 +1,16 @@
import { HTTPService } from "@/shared/utils/http";
import { gameCardsSchema, GameCardType } from "./schemas/gameCard";
import { gameSchema, GameType } from "./schemas/game";
import { GameCreateType, gameSchema } from "./schemas/game";
import { z } from "zod";
export abstract class GameService {
public static async getGameCards() {
return await HTTPService.get<GameCardType[]>(
"/games/cards",
gameCardsSchema
);
return await HTTPService.get("/games/cards", gameCardsSchema);
}
public static async getGame(id: number) {
return await HTTPService.get<GameType>(`/games/${id}`, gameSchema);
return await HTTPService.get(`/games/${id}`, gameSchema);
}
public static async changeGame(id: number, gameInfo: GameCreateType) {
return await HTTPService.put(`/games/${id}`, gameSchema, gameInfo);
}
}

View File

@@ -1,5 +1,11 @@
import { GameCardType } from "./schemas/gameCard";
import { GameType } from "./schemas/game";
import { GameType, GameCreateType, gameCreateSchema } from "./schemas/game";
import { GameService } from "./game";
export { GameService, type GameType, type GameCardType };
export {
GameService,
gameCreateSchema,
type GameType,
type GameCreateType,
type GameCardType,
};

View File

@@ -1,46 +1,56 @@
import { z } from "zod";
import { gameCardSchema } from "./gameCard";
import { gameCardBaseSchema } from "./gameCard";
export const gameSchema = gameCardSchema.and(
z.object({
torrent_file: z.string().min(1),
trailer: z.string().optional(),
export const gameBaseSchema = gameCardBaseSchema.and(
z.object({
torrent_file: z.string().min(1),
trailer: z.string().optional(),
system: z.string().optional(),
processor: z.string().optional(),
memory: z.string().optional(),
graphics: z.string().optional(),
storage: z.string().optional(),
system: z.string().optional(),
processor: z.string().optional(),
memory: z.string().optional(),
graphics: z.string().optional(),
storage: z.string().optional(),
developer: z.string().optional(),
language: z.string().optional(),
download_size: z.string().optional(),
developer: z.string().optional(),
language: z.string().optional(),
download_size: z.string().optional(),
update_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
upload_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
release_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
})
release_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
})
);
export const gameCreateSchema = gameBaseSchema.and(z.object({}));
export type GameCreateType = z.infer<typeof gameCreateSchema>;
export const gameSchema = gameBaseSchema.and(
z.object({
id: z.number().positive(),
owner_id: z.number().positive(),
update_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
upload_date: z
.string()
.min(1)
.transform((d) => new Date(d)),
})
);
export type GameType = z.infer<typeof gameSchema>;
export const isGame = (a: any): a is GameType => {
return gameSchema.safeParse(a).success;
return gameSchema.safeParse(a).success;
};
export const gamesSchema = z.array(z.any()).transform((a) => {
const games: GameType[] = [];
a.forEach((e) => {
if (isGame(e)) games.push(gameSchema.parse(e));
else console.error("Game parse error - ", e);
});
return games;
const games: GameType[] = [];
a.forEach((e) => {
if (isGame(e)) games.push(gameSchema.parse(e));
else console.error("Game parse error - ", e);
});
return games;
});

View File

@@ -1,8 +1,7 @@
import { z } from "zod";
export const gameCardSchema = z
export const gameCardBaseSchema = z
.object({
id: z.number().positive(),
title: z.string().min(3),
cover: z.string().optional(),
description: z.string().optional(),
@@ -19,6 +18,12 @@ export const gameCardSchema = z
: undefined,
};
});
export const gameCardSchema = gameCardBaseSchema.and(
z.object({
id: z.number().positive(),
})
);
export type GameCardType = z.infer<typeof gameCardSchema>;
export const isGameCard = (a: any): a is GameCardType => {

View File

@@ -11,10 +11,10 @@ import Cookies from "js-cookie";
export abstract class UserService {
public static async Login(loginForm: LoginForm) {
const accessToken = await HTTPService.post<TokenResponse>(
const accessToken = await HTTPService.post(
"/auth",
new URLSearchParams(Object.entries(loginForm)),
tokenResponseSchema,
new URLSearchParams(Object.entries(loginForm)),
{
"Content-Type": "application/x-www-form-urlencoded",
}
@@ -31,6 +31,10 @@ export abstract class UserService {
}
}
public static GetToken(): string | undefined {
return Cookies.get("access-token");
}
public static IdentifyYourself(): TokenData | undefined {
const token = Cookies.get("access-token");
if (token) {