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 { 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],
|
||||||
|
|
|
||||||
|
|
@ -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,117 +16,217 @@ import { ShopCategoryService } from '../shop-category/shop-category.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ShopCategoryDocumentService {
|
export class ShopCategoryDocumentService {
|
||||||
|
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
|
||||||
|
) {}
|
||||||
|
|
||||||
constructor(@InjectModel(ShopCategoryDocument.name) private shopCategoryDocumentModel: Model<ShopCategoryDoc>,
|
async create(
|
||||||
private readonly languageService: LanguageService,
|
createShopCategoryDocumentDto: CreateShopCategoryDocumentDto,
|
||||||
@Inject(forwardRef(() => ShopCategoryService)) private readonly shopCategoryService: ShopCategoryService,
|
lang: string
|
||||||
@Inject('I18NEXT') private readonly i18n: typeof i18next) { }
|
): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
// بررسی زبان
|
||||||
|
const language = await this.languageService.findOneWithID(
|
||||||
|
createShopCategoryDocumentDto.Language,
|
||||||
|
lang
|
||||||
|
);
|
||||||
|
|
||||||
async create(
|
if (!language.success) {
|
||||||
createShopCategoryDocumentDto: CreateShopCategoryDocumentDto,
|
return {
|
||||||
lang: string
|
success: false,
|
||||||
): Promise<ApiResponse> {
|
message: await this.i18n.t('not_found', {
|
||||||
try {
|
lng: lang,
|
||||||
// بررسی زبان
|
ns: 'shopCategoryDocument',
|
||||||
const language = await this.languageService.findOneWithID(
|
}),
|
||||||
createShopCategoryDocumentDto.Language,
|
data: null,
|
||||||
lang
|
};
|
||||||
);
|
}
|
||||||
|
|
||||||
if (!language.success) {
|
const shopCategory = await this.shopCategoryService.findOne(
|
||||||
return {
|
createShopCategoryDocumentDto.ShopCategory,
|
||||||
success: false,
|
lang
|
||||||
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
|
);
|
||||||
data: null
|
if (!shopCategory.success) {
|
||||||
};
|
return {
|
||||||
}
|
success: false,
|
||||||
|
message: await this.i18n.t('not_found', {
|
||||||
|
lng: lang,
|
||||||
|
ns: 'shopCategoryDocument',
|
||||||
|
}),
|
||||||
|
data: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const createdDoc = new this.shopCategoryDocumentModel({
|
||||||
|
Name: createShopCategoryDocumentDto.Name,
|
||||||
|
Description: createShopCategoryDocumentDto.Description,
|
||||||
|
Language: language.data._id,
|
||||||
|
ID: uuidv4(),
|
||||||
|
Status: true,
|
||||||
|
Tags: [],
|
||||||
|
ShopCategory: shopCategory.data._id,
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
const shopCategory = await this.shopCategoryService.findOne(createShopCategoryDocumentDto.ShopCategory, lang)
|
const result = await createdDoc.save();
|
||||||
if (!shopCategory.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
|
|
||||||
data: null
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const createdDoc = new this.shopCategoryDocumentModel({
|
|
||||||
Name: createShopCategoryDocumentDto.Name,
|
|
||||||
Description: createShopCategoryDocumentDto.Description,
|
|
||||||
Language: language.data._id,
|
|
||||||
ID: uuidv4(),
|
|
||||||
Status: true,
|
|
||||||
Tags: [],
|
|
||||||
ShopCategory: shopCategory.data._id,
|
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
});
|
|
||||||
|
|
||||||
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) {
|
||||||
return { success: true, message, data: result };
|
const message = await this.i18n.t('create_error', {
|
||||||
} catch (error) {
|
lng: lang,
|
||||||
const message = await this.i18n.t('create_error', { lng: lang, ns: 'shopCategoryDocument' });
|
ns: 'shopCategoryDocument',
|
||||||
return { success: false, message, errors: error.message };
|
});
|
||||||
}
|
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();
|
async update(
|
||||||
if (!existingDoc) {
|
id: string,
|
||||||
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
|
updateShopCategoryDocumentDto: UpdateShopCategoryDocumentDto,
|
||||||
return { success: false, message, data: null };
|
lang: string
|
||||||
}
|
): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
|
const existingDoc = await this.shopCategoryDocumentModel
|
||||||
|
.findOne({ ID: id })
|
||||||
|
.exec();
|
||||||
|
if (!existingDoc) {
|
||||||
|
const message = await this.i18n.t('not_found', {
|
||||||
|
lng: lang,
|
||||||
|
ns: 'shopCategoryDocument',
|
||||||
|
});
|
||||||
|
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(
|
||||||
if (!language.success) {
|
updateShopCategoryDocumentDto.Language,
|
||||||
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
|
'en'
|
||||||
return { success: false, message, data: null };
|
);
|
||||||
}
|
if (!language.success) {
|
||||||
updateShopCategoryDocumentDto.Language = language.data._id;
|
const message = await this.i18n.t('not_found', {
|
||||||
}
|
lng: lang,
|
||||||
|
ns: 'shopCategoryDocument',
|
||||||
// آپدیت فیلدها
|
});
|
||||||
Object.assign(existingDoc, updateShopCategoryDocumentDto, { updatedAt: new Date() });
|
return { success: false, message, data: null };
|
||||||
|
|
||||||
const updatedDoc = await existingDoc.save();
|
|
||||||
|
|
||||||
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' });
|
|
||||||
return { success: false, message, errors: error.message };
|
|
||||||
}
|
}
|
||||||
}
|
updateShopCategoryDocumentDto.Language = language.data._id;
|
||||||
async toggleStatus(
|
}
|
||||||
id: string,
|
|
||||||
lang: string
|
|
||||||
): Promise<ApiResponse> {
|
|
||||||
try {
|
|
||||||
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec();
|
|
||||||
if (!existingDoc) {
|
|
||||||
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
|
|
||||||
return { success: false, message, data: null };
|
|
||||||
}
|
|
||||||
existingDoc.Status = !existingDoc.Status;
|
|
||||||
existingDoc.updatedAt = new Date();
|
|
||||||
|
|
||||||
const updatedDoc = await existingDoc.save();
|
// آپدیت فیلدها
|
||||||
|
Object.assign(existingDoc, updateShopCategoryDocumentDto, {
|
||||||
|
updatedAt: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
const message = await this.i18n.t('status_toggled', { lng: lang, ns: 'shopCategoryDocument' });
|
const updatedDoc = await existingDoc.save();
|
||||||
return { success: true, message, data: updatedDoc };
|
|
||||||
} catch (error) {
|
const message = await this.i18n.t('updated', {
|
||||||
const message = await this.i18n.t('update_error', { lng: lang, ns: 'shopCategoryDocument' });
|
lng: lang,
|
||||||
return { success: false, message, errors: error.message };
|
ns: 'shopCategoryDocument',
|
||||||
}
|
});
|
||||||
|
return { success: true, message, data: updatedDoc };
|
||||||
|
} catch (error) {
|
||||||
|
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> {
|
||||||
|
try {
|
||||||
|
const existingDoc = await this.shopCategoryDocumentModel
|
||||||
|
.findOne({ ID: id })
|
||||||
|
.exec();
|
||||||
|
if (!existingDoc) {
|
||||||
|
const message = await this.i18n.t('not_found', {
|
||||||
|
lng: lang,
|
||||||
|
ns: 'shopCategoryDocument',
|
||||||
|
});
|
||||||
|
return { success: false, message, data: null };
|
||||||
|
}
|
||||||
|
existingDoc.Status = !existingDoc.Status;
|
||||||
|
existingDoc.updatedAt = new Date();
|
||||||
|
|
||||||
|
const updatedDoc = await existingDoc.save();
|
||||||
|
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
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