-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Заготовка магазина и рабочие фильтры
- Loading branch information
Showing
30 changed files
with
675 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,5 @@ package-lock.json | |
# IDE files | ||
/.idea | ||
|
||
#uploads | ||
src/uploads/avatars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import {AvatarModel} from "../models/Avatar"; | ||
import {readdir} from "node:fs/promises"; | ||
import cliProgress from "cli-progress" | ||
import {AvatarTagModel} from "../models/AvatarTag"; | ||
import {TagModel} from "../models/Tag"; | ||
|
||
const pathToDecorationsFile = 'src/uploads/decorations.json' | ||
const pathToAvatars = 'src/uploads/avatars' | ||
|
||
interface Content { | ||
avatars: AvatarContent[] | ||
} | ||
|
||
interface AvatarContent { | ||
isAnimated: boolean | ||
filename: string | ||
tags: string[] | ||
price: number | ||
} | ||
|
||
export const syncDatabaseForDecorations = async () => { | ||
console.log('Обновляю декорации в базе данных...') | ||
|
||
let progress = 0 | ||
const progressBar = new cliProgress.SingleBar({ | ||
format: ' {bar} | {filename} | {value}/{total}' | ||
}, cliProgress.Presets.shades_classic) | ||
|
||
const file = Bun.file(pathToDecorationsFile) | ||
|
||
if (!await file.exists()) { | ||
console.log('Файл декораций не найден. Пропускаю ...') | ||
return | ||
} | ||
|
||
const contents: Content = await file.json() | ||
const avatarFiles = await readdir(pathToAvatars) | ||
|
||
progressBar.start(avatarFiles.length, progress) | ||
|
||
for (const avatarFile of avatarFiles) { | ||
progressBar.increment(1, {filename: avatarFile}) | ||
const avatarToSave = { | ||
filename: avatarFile, | ||
isAnimated: avatarFile.endsWith('.gif'), | ||
price: 1000 | ||
} | ||
|
||
let tags: string[] = [] | ||
|
||
// Пробуем найти, переопределена ли цена и статус анимации | ||
for (const avatarInfo of contents.avatars) { | ||
if (avatarInfo.filename !== avatarFile) | ||
continue | ||
avatarToSave.isAnimated = avatarInfo.isAnimated | ||
avatarToSave.price = avatarInfo.price | ||
tags = avatarInfo.tags | ||
break | ||
} | ||
|
||
// Находим или создаём модель | ||
const avatarModel = await AvatarModel.findOrCreate({ | ||
where: { | ||
filename: avatarFile, | ||
}, | ||
defaults: avatarToSave | ||
}) | ||
|
||
// Проверяем наличие тегов | ||
if (!tags) | ||
await AvatarTagModel.destroy({ | ||
where: { | ||
avatarId: avatarModel[0].id | ||
} | ||
}) | ||
else | ||
for (const tag of tags) { | ||
const tagModel = await TagModel.findOrCreate({ | ||
where: { | ||
value: tag | ||
}, | ||
defaults: { | ||
value: tag | ||
} | ||
}) | ||
await AvatarTagModel.findOrCreate({ | ||
where: { | ||
avatarId: avatarModel[0].id, | ||
tagId: tagModel[0].id | ||
}, | ||
defaults: { | ||
avatarId: avatarModel[0].id, | ||
tagId: tagModel[0].id | ||
} | ||
}) | ||
} | ||
|
||
// Если создана - то завершаем цикл | ||
if (avatarModel[1]) | ||
continue | ||
|
||
await avatarModel[0].update(avatarToSave) | ||
} | ||
|
||
progressBar.stop() | ||
|
||
console.log('Обновил все декорации') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DataTypes } from 'sequelize' | ||
|
||
import { cache, enableCache, sequelize } from '@db' | ||
|
||
import { SPOModel } from '../SPO' | ||
import type { IModelPrototype } from '../types' | ||
|
||
// REMOVE IT | ||
// ? | ||
export type AvatarModelType = { | ||
id: bigint | ||
filename: string | ||
price: number | ||
isAnimated: boolean | ||
} | ||
|
||
export type IAvatarModelType = IModelPrototype<AvatarModelType, 'id'> | ||
|
||
const avatarModel = sequelize.define<IAvatarModelType>('avatar', { | ||
id: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
filename: { | ||
type: DataTypes.STRING(35), | ||
allowNull: false | ||
}, | ||
price: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
defaultValue: 1000 | ||
}, | ||
isAnimated: { | ||
type: DataTypes.BOOLEAN, | ||
allowNull: false | ||
} | ||
}) | ||
|
||
export const AvatarModel = enableCache | ||
? cache.init<IAvatarModelType>(avatarModel) | ||
: avatarModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DataTypes } from 'sequelize' | ||
|
||
import { cache, enableCache, sequelize } from '@db' | ||
|
||
import { SPOModel } from '../SPO' | ||
import type {IModelPrototype, IModelPrototypeNoId} from '../types' | ||
import {AvatarModel} from "../Avatar"; | ||
import {TagModel} from "../Tag"; | ||
|
||
// REMOVE IT | ||
// ? | ||
export type AvatarTagModelType = { | ||
avatarId: bigint | ||
tagId: bigint | ||
} | ||
|
||
export type IAvatarTagModelType = IModelPrototypeNoId<AvatarTagModelType> | ||
|
||
const avatarTagModel = sequelize.define<IAvatarTagModelType>('avatarTag', { | ||
avatarId: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
allowNull: false, | ||
references: { | ||
model: AvatarModel | ||
} | ||
}, | ||
tagId: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
allowNull: false, | ||
references: { | ||
model: TagModel | ||
} | ||
} | ||
}) | ||
|
||
export const AvatarTagModel = enableCache | ||
? cache.init<IAvatarTagModelType>(avatarTagModel) | ||
: avatarTagModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DataTypes } from 'sequelize' | ||
|
||
import { cache, enableCache, sequelize } from '@db' | ||
|
||
import { SPOModel } from '../SPO' | ||
import type { IModelPrototype } from '../types' | ||
import {AvatarModel} from "../Avatar"; | ||
|
||
// REMOVE IT | ||
// ? | ||
export type TagModelType = { | ||
id: bigint | ||
value: string | ||
} | ||
|
||
export type ITagModelType = IModelPrototype<TagModelType, 'id'> | ||
|
||
const tagModel = sequelize.define<ITagModelType>('tag', { | ||
id: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
allowNull: false | ||
}, | ||
value: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
} | ||
}) | ||
|
||
export const TagModel = enableCache | ||
? cache.init<ITagModelType>(tagModel) | ||
: tagModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { DataTypes } from 'sequelize' | ||
|
||
import { cache, enableCache, sequelize } from '@db' | ||
|
||
import { SPOModel } from '../SPO' | ||
import type {IModelPrototype, IModelPrototypeNoId} from '../types' | ||
import {AvatarModel} from "../Avatar"; | ||
import {DiaryUserModel} from "../DiaryUser"; | ||
|
||
// REMOVE IT | ||
// ? | ||
export type UserAvatarModelType = { | ||
avatarId: bigint | ||
diaryUserId: bigint | ||
} | ||
|
||
export type IUserAvatarModelType = IModelPrototypeNoId<UserAvatarModelType> | ||
|
||
const userAvatarModel = sequelize.define<IUserAvatarModelType>('userAvatar', { | ||
avatarId: { | ||
type: DataTypes.BIGINT, | ||
references: { | ||
model: AvatarModel | ||
}, | ||
allowNull: false | ||
}, | ||
diaryUserId: { | ||
type: DataTypes.BIGINT, | ||
references: { | ||
model: DiaryUserModel | ||
}, | ||
allowNull: false | ||
} | ||
}) | ||
|
||
export const UserAvatarModel = enableCache | ||
? cache.init<IUserAvatarModelType>(userAvatarModel) | ||
: userAvatarModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.