complete language service
This commit is contained in:
parent
f52cd5263e
commit
9eaf8ddfc5
|
|
@ -1,19 +1,28 @@
|
||||||
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
|
import { Inject, Injectable, NotFoundException, InternalServerErrorException } from '@nestjs/common';
|
||||||
import { InjectModel } from '@nestjs/mongoose';
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
import { Model } from 'mongoose';
|
import { Model } from 'mongoose';
|
||||||
import { Language } from '../../schemas/language.schema';
|
import { Language } from '../../schemas/language.schema';
|
||||||
import { CreateLanguageDto, UpdateLanguageDto } from './dto/language.dto';
|
import { CreateLanguageDto, UpdateLanguageDto } from './dto/language.dto';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { I18nContext, I18nService } from 'nestjs-i18n';
|
|
||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
|
|
||||||
|
export interface ApiResponse<T = any> {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
data?: T | null;
|
||||||
|
errors?: any;
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LanguageService {
|
export class LanguageService {
|
||||||
constructor(@InjectModel(Language.name) private languageModel: Model<Language>,
|
constructor(
|
||||||
@Inject('I18NEXT') private readonly i18n: typeof i18next) {}
|
@InjectModel(Language.name) private languageModel: Model<Language>,
|
||||||
|
@Inject('I18NEXT') private readonly i18n: typeof i18next
|
||||||
|
) {}
|
||||||
|
|
||||||
async create(
|
/** Create Language */
|
||||||
createLanguageDto: CreateLanguageDto,lang:string): Promise<any> {
|
async create(createLanguageDto: CreateLanguageDto, lang: string): Promise<ApiResponse> {
|
||||||
|
try {
|
||||||
const createdLanguage = new this.languageModel({
|
const createdLanguage = new this.languageModel({
|
||||||
...createLanguageDto,
|
...createLanguageDto,
|
||||||
ID: uuidv4(),
|
ID: uuidv4(),
|
||||||
|
|
@ -22,46 +31,115 @@ async create(
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
});
|
});
|
||||||
const result = await createdLanguage.save();
|
const result = await createdLanguage.save();
|
||||||
console.log("language -----------------", await this.i18n.t('created', { lng: lang ,ns: 'language' }))
|
const message = await this.i18n.t('created', { lng: lang, ns: 'language' });
|
||||||
console.log("language -------sdadasd----------", lang)
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
message: await this.i18n.t('created', { lng:lang }),
|
|
||||||
data: result,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async findAll(): Promise<Language[]> {
|
return { success: true, message, data: result };
|
||||||
return this.languageModel.find().exec();
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('create_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string): Promise<Language> {
|
/** Find All Languages */
|
||||||
|
async findAll(lang: string): Promise<ApiResponse<Language[]>> {
|
||||||
|
try {
|
||||||
|
const result = await this.languageModel.find().exec();
|
||||||
|
const message = await this.i18n.t('retrieved_all', { lng: lang, ns: 'language' });
|
||||||
|
return { success: true, message, data: result };
|
||||||
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Find Active Languages */
|
||||||
|
async findActive(lang: string): Promise<ApiResponse<Language[]>> {
|
||||||
|
try {
|
||||||
|
const result = await this.languageModel.find({ Status: true }).exec();
|
||||||
|
const message = await this.i18n.t('retrieved_active', { lng: lang, ns: 'language' });
|
||||||
|
return { success: true, message, data: result };
|
||||||
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Toggle Status */
|
||||||
|
async toggleStatus(id: string, lang: string): Promise<ApiResponse<Language>> {
|
||||||
|
try {
|
||||||
const language = await this.languageModel.findOne({ ID: id }).exec();
|
const language = await this.languageModel.findOne({ ID: id }).exec();
|
||||||
if (!language) {
|
if (!language) {
|
||||||
throw new NotFoundException(`Language with ID ${id} not found`);
|
const message = await this.i18n.t('not_found', { lng: lang, ns: 'language' });
|
||||||
}
|
return { success: false, message, data: null };
|
||||||
return language;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, updateLanguageDto: UpdateLanguageDto): Promise<Language> {
|
language.Status = !language.Status;
|
||||||
const language = await this.languageModel
|
const result = await language.save();
|
||||||
|
const message = await this.i18n.t('status_toggled', { lng: lang, ns: 'language' });
|
||||||
|
return { success: true, message, data: result };
|
||||||
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('update_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Find One by ID */
|
||||||
|
async findOneWithID(id: string, lang: string): Promise<ApiResponse<Language>> {
|
||||||
|
try {
|
||||||
|
const language = await this.languageModel.findOne({ ID: id }).exec();
|
||||||
|
if (!language) {
|
||||||
|
const message = await this.i18n.t('not_found', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, data: null };
|
||||||
|
}
|
||||||
|
const message = await this.i18n.t('retrieved', { lng: lang, ns: 'language' });
|
||||||
|
return { success: true, message, data: language };
|
||||||
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Find One by _id */
|
||||||
|
async findOneWithid(id: string, lang: string): Promise<ApiResponse<Language>> {
|
||||||
|
try {
|
||||||
|
const language = await this.languageModel.findOne({ _id: id }).exec();
|
||||||
|
if (!language) {
|
||||||
|
const message = await this.i18n.t('not_found', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, data: null };
|
||||||
|
}
|
||||||
|
const message = await this.i18n.t('retrieved', { lng: lang, ns: 'language' });
|
||||||
|
return { success: true, message, data: language };
|
||||||
|
} catch (error) {
|
||||||
|
const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Update Language */
|
||||||
|
async updateLanguage(
|
||||||
|
id: string,
|
||||||
|
updateLanguageDto: UpdateLanguageDto,
|
||||||
|
lang: string
|
||||||
|
): Promise<ApiResponse<Language>> {
|
||||||
|
try {
|
||||||
|
const updatedLanguage = await this.languageModel
|
||||||
.findOneAndUpdate(
|
.findOneAndUpdate(
|
||||||
{ ID: id },
|
{ ID: id },
|
||||||
{ ...updateLanguageDto, updatedAt: new Date() },
|
{ ...updateLanguageDto, updatedAt: new Date() },
|
||||||
{ new: true },
|
{ new: true }
|
||||||
)
|
)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
if (!language) {
|
if (!updatedLanguage) {
|
||||||
throw new NotFoundException(`Language with ID ${id} not found`);
|
const message = await this.i18n.t('not_found', { lng: lang, ns: 'language' });
|
||||||
}
|
return { success: false, message, data: null };
|
||||||
return language;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<void> {
|
const message = await this.i18n.t('updated', { lng: lang, ns: 'language' });
|
||||||
const result = await this.languageModel.deleteOne({ ID: id }).exec();
|
return { success: true, message, data: updatedLanguage };
|
||||||
if (result.deletedCount === 0) {
|
} catch (error) {
|
||||||
throw new NotFoundException(`Language with ID ${id} not found`);
|
const message = await this.i18n.t('update_error', { lng: lang, ns: 'language' });
|
||||||
|
return { success: false, message, errors: error.message };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import { IsString, IsOptional, IsNumber, IsNotEmpty } from 'class-validator';
|
import { IsString, IsOptional, IsNumber, Min, Max, IsNotEmpty } from 'class-validator';
|
||||||
import { Types } from 'mongoose';
|
import { Types } from 'mongoose';
|
||||||
import { i18nValidationMessage } from 'nestjs-i18n';
|
import { i18nValidationMessage } from 'nestjs-i18n';
|
||||||
import { IsMongoId } from 'class-validator';
|
import { IsMongoId } from 'class-validator';
|
||||||
|
|
||||||
export class CreateShopCategoryDto {
|
export class CreateShopCategoryDto {
|
||||||
@IsString({ message: i18nValidationMessage('validation.isString') })
|
@IsString({ message: i18nValidationMessage('validation.isString') })
|
||||||
@IsOptional()
|
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
|
||||||
|
@IsOptional() // اگر اجباری است، این را حذف کنید
|
||||||
Name?: string;
|
Name?: string;
|
||||||
|
|
||||||
@IsString({ message: i18nValidationMessage('validation.isString') })
|
@IsString({ message: i18nValidationMessage('validation.isString') })
|
||||||
|
|
@ -16,10 +17,6 @@ export class CreateShopCategoryDto {
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
Icon?: string;
|
Icon?: string;
|
||||||
|
|
||||||
@IsNumber({}, { message: i18nValidationMessage('validation.isNumber') })
|
|
||||||
@IsOptional()
|
|
||||||
Level?: number;
|
|
||||||
|
|
||||||
@IsMongoId({ message: i18nValidationMessage('validation.isMongoId') })
|
@IsMongoId({ message: i18nValidationMessage('validation.isMongoId') })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
ParentCategory?: Types.ObjectId;
|
ParentCategory?: Types.ObjectId;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
export interface ApiResponse<T = any> {
|
||||||
|
success: boolean; // آیا عملیات موفق بوده یا نه
|
||||||
|
message: string; // پیام قابل نمایش به کاربر (ترجمه شده)
|
||||||
|
data?: T | null; // داده بازگشتی (در GET یا CREATE)
|
||||||
|
errors?: any; // جزئیات خطا (در صورت وجود)
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,14 @@
|
||||||
{
|
{
|
||||||
"created": "Language created successfully"
|
"created": "Language created successfully",
|
||||||
|
"create_error": "Error occurred while creating language",
|
||||||
|
"retrieved_all": "Languages retrieved successfully",
|
||||||
|
"retrieved_active": "Active languages retrieved successfully",
|
||||||
|
"retrieved": "Language retrieved successfully",
|
||||||
|
"retrieve_error": "Error occurred while retrieving language(s)",
|
||||||
|
"not_found": "Language not found",
|
||||||
|
"updated": "Language updated successfully",
|
||||||
|
"update_error": "Error occurred while updating language",
|
||||||
|
"deleted": "Language deleted successfully",
|
||||||
|
"delete_error": "Error occurred while deleting language",
|
||||||
|
"status_toggled": "Language status updated successfully"
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +1,14 @@
|
||||||
{
|
{
|
||||||
"created": "زبان با موفقیت ایجاد شد"
|
"created": "زبان با موفقیت ایجاد شد",
|
||||||
}
|
"create_error": "خطا در ایجاد زبان رخ داد",
|
||||||
|
"retrieved_all": "تمام زبانها با موفقیت دریافت شدند",
|
||||||
|
"retrieved_active": "زبانهای فعال با موفقیت دریافت شدند",
|
||||||
|
"retrieved": "زبان با موفقیت دریافت شد",
|
||||||
|
"retrieve_error": "خطا در دریافت زبانها رخ داد",
|
||||||
|
"not_found": "زبان مورد نظر پیدا نشد",
|
||||||
|
"updated": "زبان با موفقیت بروزرسانی شد",
|
||||||
|
"update_error": "خطا در بروزرسانی زبان رخ داد",
|
||||||
|
"deleted": "زبان با موفقیت حذف شد",
|
||||||
|
"delete_error": "خطا در حذف زبان رخ داد",
|
||||||
|
"status_toggled": "وضعیت زبان با موفقیت تغییر کرد"
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,6 @@ export class ShopCategory {
|
||||||
@Prop()
|
@Prop()
|
||||||
Icon: string;
|
Icon: string;
|
||||||
|
|
||||||
@Prop()
|
|
||||||
Level: number;
|
|
||||||
|
|
||||||
@Prop({ type: [{ type: Types.ObjectId, ref: 'Tag' }] })
|
@Prop({ type: [{ type: Types.ObjectId, ref: 'Tag' }] })
|
||||||
Tags: Types.ObjectId[];
|
Tags: Types.ObjectId[];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue