|
@@ -1,10 +1,11 @@
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
-import { Like, Repository } from 'typeorm';
|
|
|
+import { In, Like, Repository } from 'typeorm';
|
|
|
import { Menu } from '@/modules/menu/menu.entity';
|
|
|
import { SharedService } from '@/shared/shared.service';
|
|
|
import { Article } from '@/modules/article/article.entity';
|
|
|
import { Category } from '@/modules/category/category.entity';
|
|
|
+import { getTopMenuFragment } from '@/common/utils';
|
|
|
|
|
|
@Injectable()
|
|
|
export class WebService {
|
|
@@ -73,11 +74,58 @@ export class WebService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- async getCategory(id: number) {
|
|
|
- const category = await this.categoryRepo.findOne({
|
|
|
+ async getTopCategory1(id: number) {
|
|
|
+ let currentCategory = await this.categoryRepo.findOne({
|
|
|
where: { id },
|
|
|
+ relations: { parent: true },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!currentCategory) {
|
|
|
+ return null; // 如果分类不存在,返回 null
|
|
|
+ }
|
|
|
+ // 递归查找顶层父分类
|
|
|
+ while (currentCategory.parent) {
|
|
|
+ currentCategory = await this.categoryRepo.findOne({
|
|
|
+ where: { id: currentCategory.parent.id },
|
|
|
+ relations: { parent: true },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ console.log('currentCategory', currentCategory);
|
|
|
+ return currentCategory.id
|
|
|
+ ? this.categoryRepo.findOne({
|
|
|
+ where: { id: currentCategory.id },
|
|
|
+ relations: { children: true },
|
|
|
+ })
|
|
|
+ : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ async getCategoryTreeAndPath(id: number) {
|
|
|
+ const categories = await this.categoryRepo.find({
|
|
|
relations: { children: true },
|
|
|
});
|
|
|
- return category;
|
|
|
+ const data = this.sharedService.handleTree(categories);
|
|
|
+ return getTopMenuFragment(data, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ async searchArticles(key: string, locale?: string) {
|
|
|
+ const lang = this.sharedService.handleValidLang(locale);
|
|
|
+ const articles = await this.articleRepo.find({
|
|
|
+ where: [
|
|
|
+ {
|
|
|
+ translations: {
|
|
|
+ locale: lang,
|
|
|
+ title: Like(`%${key || ''}%`),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ translations: {
|
|
|
+ locale: lang,
|
|
|
+ content: Like(`%${key || ''}%`),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ relations: { translations: true },
|
|
|
+ });
|
|
|
+ return articles.map((article) => article.translate(lang));
|
|
|
}
|
|
|
}
|