start for tag Module

This commit is contained in:
vahidrezvani 2025-08-23 16:05:21 +03:30
parent e2bfef11f1
commit b1099cc38a
8 changed files with 257 additions and 99 deletions

View File

@ -11,6 +11,7 @@ import { AppService } from './app.service';
import { LanguageController } from './language/language.controller'; import { LanguageController } from './language/language.controller';
import { I18nextModule } from './i18next/i18next.module'; import { I18nextModule } from './i18next/i18next.module';
import { ShopCategoryDocumentModule } from './shop-category-document/shop-category-document.module'; import { ShopCategoryDocumentModule } from './shop-category-document/shop-category-document.module';
import { TagModule } from './tag/tag.module';
@Module({ @Module({
imports: [ imports: [
@ -30,6 +31,7 @@ import { ShopCategoryDocumentModule } from './shop-category-document/shop-catego
LanguageModule, LanguageModule,
I18nextModule, I18nextModule,
ShopCategoryDocumentModule, ShopCategoryDocumentModule,
TagModule,
], ],
controllers: [AppController, LanguageController], controllers: [AppController, LanguageController],
providers: [AppService], providers: [AppService],

View File

@ -1,7 +1,10 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { ShopCategoryDoc } from '../../schemas/shopCategory.schema'; import { ShopCategoryDoc } from '../../schemas/shopCategory.schema';
import { ShopCategoryDocument, ShopCategoryDocumentSchema } from '../../schemas/shopCategoryDocument'; import {
ShopCategoryDocument,
ShopCategoryDocumentSchema,
} from '../../schemas/shopCategoryDocument';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { I18nService } from 'nestjs-i18n'; import { I18nService } from 'nestjs-i18n';
@ -13,11 +16,14 @@ import { ShopCategoryService } from '../shop-category/shop-category.service';
@Injectable() @Injectable()
export class ShopCategoryDocumentService { export class ShopCategoryDocumentService {
constructor(
constructor(@InjectModel(ShopCategoryDocument.name) private shopCategoryDocumentModel: Model<ShopCategoryDoc>, @InjectModel(ShopCategoryDocument.name)
private shopCategoryDocumentModel: Model<ShopCategoryDoc>,
private readonly languageService: LanguageService, private readonly languageService: LanguageService,
@Inject(forwardRef(() => ShopCategoryService)) private readonly shopCategoryService: ShopCategoryService, @Inject(forwardRef(() => ShopCategoryService))
@Inject('I18NEXT') private readonly i18n: typeof i18next) { } private readonly shopCategoryService: ShopCategoryService,
@Inject('I18NEXT') private readonly i18n: typeof i18next
) {}
async create( async create(
createShopCategoryDocumentDto: CreateShopCategoryDocumentDto, createShopCategoryDocumentDto: CreateShopCategoryDocumentDto,
@ -33,17 +39,26 @@ export class ShopCategoryDocumentService {
if (!language.success) { if (!language.success) {
return { return {
success: false, success: false,
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }), message: await this.i18n.t('not_found', {
data: null lng: lang,
ns: 'shopCategoryDocument',
}),
data: null,
}; };
} }
const shopCategory = await this.shopCategoryService.findOne(createShopCategoryDocumentDto.ShopCategory, lang) const shopCategory = await this.shopCategoryService.findOne(
createShopCategoryDocumentDto.ShopCategory,
lang
);
if (!shopCategory.success) { if (!shopCategory.success) {
return { return {
success: false, success: false,
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }), message: await this.i18n.t('not_found', {
data: null lng: lang,
ns: 'shopCategoryDocument',
}),
data: null,
}; };
} }
const createdDoc = new this.shopCategoryDocumentModel({ const createdDoc = new this.shopCategoryDocumentModel({
@ -60,56 +75,84 @@ export class ShopCategoryDocumentService {
const result = await createdDoc.save(); const result = await createdDoc.save();
const message = await this.i18n.t('created', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('created', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: true, message, data: result }; return { success: true, message, data: result };
} catch (error) { } catch (error) {
const message = await this.i18n.t('create_error', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('create_error', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, errors: error.message }; return { success: false, message, errors: error.message };
} }
} }
async update( async update(
id: string, id: string,
updateShopCategoryDocumentDto: UpdateShopCategoryDocumentDto, updateShopCategoryDocumentDto: UpdateShopCategoryDocumentDto,
lang: string lang: string
): Promise<ApiResponse> { ): Promise<ApiResponse> {
try { try {
const existingDoc = await this.shopCategoryDocumentModel
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec(); .findOne({ ID: id })
.exec();
if (!existingDoc) { if (!existingDoc) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('not_found', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, data: null }; return { success: false, message, data: null };
} }
if (updateShopCategoryDocumentDto.Language) { if (updateShopCategoryDocumentDto.Language) {
const language = await this.languageService.findOneWithID(updateShopCategoryDocumentDto.Language, 'en'); const language = await this.languageService.findOneWithID(
updateShopCategoryDocumentDto.Language,
'en'
);
if (!language.success) { if (!language.success) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('not_found', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, data: null }; return { success: false, message, data: null };
} }
updateShopCategoryDocumentDto.Language = language.data._id; updateShopCategoryDocumentDto.Language = language.data._id;
} }
// آپدیت فیلدها // آپدیت فیلدها
Object.assign(existingDoc, updateShopCategoryDocumentDto, { updatedAt: new Date() }); Object.assign(existingDoc, updateShopCategoryDocumentDto, {
updatedAt: new Date(),
});
const updatedDoc = await existingDoc.save(); const updatedDoc = await existingDoc.save();
const message = await this.i18n.t('updated', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('updated', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: true, message, data: updatedDoc }; return { success: true, message, data: updatedDoc };
} catch (error) { } catch (error) {
const message = await this.i18n.t('update_error', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('update_error', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, errors: error.message }; return { success: false, message, errors: error.message };
} }
} }
async toggleStatus(
id: string, async toggleStatus(id: string, lang: string): Promise<ApiResponse> {
lang: string
): Promise<ApiResponse> {
try { try {
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec(); const existingDoc = await this.shopCategoryDocumentModel
.findOne({ ID: id })
.exec();
if (!existingDoc) { if (!existingDoc) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('not_found', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, data: null }; return { success: false, message, data: null };
} }
existingDoc.Status = !existingDoc.Status; existingDoc.Status = !existingDoc.Status;
@ -117,10 +160,70 @@ export class ShopCategoryDocumentService {
const updatedDoc = await existingDoc.save(); const updatedDoc = await existingDoc.save();
const message = await this.i18n.t('status_toggled', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('status_toggled', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: true, message, data: updatedDoc }; return { success: true, message, data: updatedDoc };
} catch (error) { } catch (error) {
const message = await this.i18n.t('update_error', { lng: lang, ns: 'shopCategoryDocument' }); const message = await this.i18n.t('update_error', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, errors: error.message };
}
}
async findAll(lang: string): Promise<ApiResponse> {
try {
const documents = await this.shopCategoryDocumentModel
.find()
.populate('Tags')
.populate('ShopCategory')
.populate('Language')
.exec();
const message = await this.i18n.t('retrieved_all', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: true, message, data: documents };
} catch (error) {
const message = await this.i18n.t('retrieve_error', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, errors: error.message };
}
}
async findOne(id: string, lang: string): Promise<ApiResponse> {
try {
const document = await this.shopCategoryDocumentModel
.findOne({ ID: id })
.populate('Tags')
.populate('ShopCategory')
.populate('Language')
.exec();
if (!document) {
const message = await this.i18n.t('not_found', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, data: null };
}
const message = await this.i18n.t('retrieved', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: true, message, data: document };
} catch (error) {
const message = await this.i18n.t('retrieve_error', {
lng: lang,
ns: 'shopCategoryDocument',
});
return { success: false, message, errors: error.message }; return { success: false, message, errors: error.message };
} }
} }

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TagController } from './tag.controller';
describe('TagController', () => {
let controller: TagController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [TagController],
}).compile();
controller = module.get<TagController>(TagController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';
@Controller('tag')
export class TagController {}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { TagController } from './tag.controller';
import { TagService } from './tag.service';
@Module({
controllers: [TagController],
providers: [TagService],
})
export class TagModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TagService } from './tag.service';
describe('TagService', () => {
let service: TagService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TagService],
}).compile();
service = module.get<TagService>(TagService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class TagService {}