start for tag Module
This commit is contained in:
parent
e2bfef11f1
commit
b1099cc38a
|
|
@ -11,6 +11,7 @@ import { AppService } from './app.service';
|
|||
import { LanguageController } from './language/language.controller';
|
||||
import { I18nextModule } from './i18next/i18next.module';
|
||||
import { ShopCategoryDocumentModule } from './shop-category-document/shop-category-document.module';
|
||||
import { TagModule } from './tag/tag.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
|
@ -30,6 +31,7 @@ import { ShopCategoryDocumentModule } from './shop-category-document/shop-catego
|
|||
LanguageModule,
|
||||
I18nextModule,
|
||||
ShopCategoryDocumentModule,
|
||||
TagModule,
|
||||
],
|
||||
controllers: [AppController, LanguageController],
|
||||
providers: [AppService],
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { ShopCategoryDoc } from '../../schemas/shopCategory.schema';
|
||||
import { ShopCategoryDocument, ShopCategoryDocumentSchema } from '../../schemas/shopCategoryDocument';
|
||||
import {
|
||||
ShopCategoryDocument,
|
||||
ShopCategoryDocumentSchema,
|
||||
} from '../../schemas/shopCategoryDocument';
|
||||
import { Model } from 'mongoose';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
|
|
@ -13,11 +16,14 @@ import { ShopCategoryService } from '../shop-category/shop-category.service';
|
|||
|
||||
@Injectable()
|
||||
export class ShopCategoryDocumentService {
|
||||
|
||||
constructor(@InjectModel(ShopCategoryDocument.name) private shopCategoryDocumentModel: Model<ShopCategoryDoc>,
|
||||
constructor(
|
||||
@InjectModel(ShopCategoryDocument.name)
|
||||
private shopCategoryDocumentModel: Model<ShopCategoryDoc>,
|
||||
private readonly languageService: LanguageService,
|
||||
@Inject(forwardRef(() => ShopCategoryService)) private readonly shopCategoryService: ShopCategoryService,
|
||||
@Inject('I18NEXT') private readonly i18n: typeof i18next) { }
|
||||
@Inject(forwardRef(() => ShopCategoryService))
|
||||
private readonly shopCategoryService: ShopCategoryService,
|
||||
@Inject('I18NEXT') private readonly i18n: typeof i18next
|
||||
) {}
|
||||
|
||||
async create(
|
||||
createShopCategoryDocumentDto: CreateShopCategoryDocumentDto,
|
||||
|
|
@ -33,17 +39,26 @@ export class ShopCategoryDocumentService {
|
|||
if (!language.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
|
||||
data: null
|
||||
message: await this.i18n.t('not_found', {
|
||||
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) {
|
||||
return {
|
||||
success: false,
|
||||
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
|
||||
data: null
|
||||
message: await this.i18n.t('not_found', {
|
||||
lng: lang,
|
||||
ns: 'shopCategoryDocument',
|
||||
}),
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
const createdDoc = new this.shopCategoryDocumentModel({
|
||||
|
|
@ -60,56 +75,84 @@ export class ShopCategoryDocumentService {
|
|||
|
||||
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 };
|
||||
} 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 };
|
||||
}
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
updateShopCategoryDocumentDto: UpdateShopCategoryDocumentDto,
|
||||
lang: string
|
||||
): Promise<ApiResponse> {
|
||||
try {
|
||||
|
||||
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec();
|
||||
const existingDoc = await this.shopCategoryDocumentModel
|
||||
.findOne({ ID: id })
|
||||
.exec();
|
||||
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 };
|
||||
}
|
||||
|
||||
if (updateShopCategoryDocumentDto.Language) {
|
||||
const language = await this.languageService.findOneWithID(updateShopCategoryDocumentDto.Language, 'en');
|
||||
const language = await this.languageService.findOneWithID(
|
||||
updateShopCategoryDocumentDto.Language,
|
||||
'en'
|
||||
);
|
||||
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 };
|
||||
}
|
||||
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 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 };
|
||||
} 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 toggleStatus(
|
||||
id: string,
|
||||
lang: string
|
||||
): Promise<ApiResponse> {
|
||||
|
||||
async toggleStatus(id: string, lang: string): Promise<ApiResponse> {
|
||||
try {
|
||||
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec();
|
||||
const existingDoc = await this.shopCategoryDocumentModel
|
||||
.findOne({ ID: id })
|
||||
.exec();
|
||||
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 };
|
||||
}
|
||||
existingDoc.Status = !existingDoc.Status;
|
||||
|
|
@ -117,10 +160,70 @@ export class ShopCategoryDocumentService {
|
|||
|
||||
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 };
|
||||
} 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 };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import { Controller } from '@nestjs/common';
|
||||
|
||||
@Controller('tag')
|
||||
export class TagController {}
|
||||
|
|
@ -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 {}
|
||||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class TagService {}
|
||||
Loading…
Reference in New Issue