Refactor code structure for improved readability and maintainability
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import { CategoriesService } from './categories.service';
|
||||
export declare class CategoriesController {
|
||||
private readonly categoriesService;
|
||||
constructor(categoriesService: CategoriesService);
|
||||
findAll(): Promise<{
|
||||
name: string;
|
||||
id: number;
|
||||
parentId: number | null;
|
||||
}[]>;
|
||||
findTree(): Promise<import("./categories.service").CategoryNode[]>;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CategoriesController = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const categories_service_1 = require("./categories.service");
|
||||
const public_decorator_1 = require("../auth/decorators/public.decorator");
|
||||
let CategoriesController = class CategoriesController {
|
||||
constructor(categoriesService) {
|
||||
this.categoriesService = categoriesService;
|
||||
}
|
||||
findAll() {
|
||||
return this.categoriesService.findAll();
|
||||
}
|
||||
findTree() {
|
||||
return this.categoriesService.findTree();
|
||||
}
|
||||
};
|
||||
exports.CategoriesController = CategoriesController;
|
||||
__decorate([
|
||||
(0, public_decorator_1.Public)(),
|
||||
(0, common_1.Get)(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", void 0)
|
||||
], CategoriesController.prototype, "findAll", null);
|
||||
__decorate([
|
||||
(0, public_decorator_1.Public)(),
|
||||
(0, common_1.Get)('tree'),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", void 0)
|
||||
], CategoriesController.prototype, "findTree", null);
|
||||
exports.CategoriesController = CategoriesController = __decorate([
|
||||
(0, common_1.Controller)('categories'),
|
||||
__metadata("design:paramtypes", [categories_service_1.CategoriesService])
|
||||
], CategoriesController);
|
||||
//# sourceMappingURL=categories.controller.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"categories.controller.js","sourceRoot":"","sources":["../../src/categories/categories.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAiD;AACjD,6DAAyD;AACzD,0EAA6D;AAGtD,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,YAA6B,iBAAoC;QAApC,sBAAiB,GAAjB,iBAAiB,CAAmB;IAAG,CAAC;IAIrE,OAAO;QACL,OAAO,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,CAAC;IAC1C,CAAC;IAID,QAAQ;QACN,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,CAAC;IAC3C,CAAC;CACF,CAAA;AAdY,oDAAoB;AAK/B;IAFC,IAAA,yBAAM,GAAE;IACR,IAAA,YAAG,GAAE;;;;mDAGL;AAID;IAFC,IAAA,yBAAM,GAAE;IACR,IAAA,YAAG,EAAC,MAAM,CAAC;;;;oDAGX;+BAbU,oBAAoB;IADhC,IAAA,mBAAU,EAAC,YAAY,CAAC;qCAEyB,sCAAiB;GADtD,oBAAoB,CAchC"}
|
||||
@@ -0,0 +1,2 @@
|
||||
export declare class CategoriesModule {
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CategoriesModule = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const categories_service_1 = require("./categories.service");
|
||||
const categories_controller_1 = require("./categories.controller");
|
||||
const prisma_module_1 = require("../prisma/prisma.module");
|
||||
let CategoriesModule = class CategoriesModule {
|
||||
};
|
||||
exports.CategoriesModule = CategoriesModule;
|
||||
exports.CategoriesModule = CategoriesModule = __decorate([
|
||||
(0, common_1.Module)({
|
||||
imports: [prisma_module_1.PrismaModule],
|
||||
providers: [categories_service_1.CategoriesService],
|
||||
controllers: [categories_controller_1.CategoriesController],
|
||||
exports: [categories_service_1.CategoriesService],
|
||||
})
|
||||
], CategoriesModule);
|
||||
//# sourceMappingURL=categories.module.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"categories.module.js","sourceRoot":"","sources":["../../src/categories/categories.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,6DAAyD;AACzD,mEAA+D;AAC/D,2DAAuD;AAQhD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAN5B,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,4BAAY,CAAC;QACvB,SAAS,EAAE,CAAC,sCAAiB,CAAC;QAC9B,WAAW,EAAE,CAAC,4CAAoB,CAAC;QACnC,OAAO,EAAE,CAAC,sCAAiB,CAAC;KAC7B,CAAC;GACW,gBAAgB,CAAG"}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
export type CategoryNode = {
|
||||
id: number;
|
||||
name: string;
|
||||
parentId: number | null;
|
||||
children: CategoryNode[];
|
||||
};
|
||||
export type FlatCategory = {
|
||||
id: number;
|
||||
name: string;
|
||||
path: string;
|
||||
};
|
||||
export declare class CategoriesService {
|
||||
private readonly prisma;
|
||||
constructor(prisma: PrismaService);
|
||||
findAll(): Promise<{
|
||||
name: string;
|
||||
id: number;
|
||||
parentId: number | null;
|
||||
}[]>;
|
||||
findTree(): Promise<CategoryNode[]>;
|
||||
findFlattened(): Promise<FlatCategory[]>;
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CategoriesService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const prisma_service_1 = require("../prisma/prisma.service");
|
||||
let CategoriesService = class CategoriesService {
|
||||
constructor(prisma) {
|
||||
this.prisma = prisma;
|
||||
}
|
||||
async findAll() {
|
||||
return this.prisma.category.findMany({ orderBy: { name: 'asc' } });
|
||||
}
|
||||
async findTree() {
|
||||
const all = await this.prisma.category.findMany({ orderBy: { name: 'asc' } });
|
||||
const map = new Map();
|
||||
all.forEach((c) => map.set(c.id, { ...c, children: [] }));
|
||||
const roots = [];
|
||||
map.forEach((node) => {
|
||||
if (node.parentId === null) {
|
||||
roots.push(node);
|
||||
}
|
||||
else {
|
||||
map.get(node.parentId)?.children.push(node);
|
||||
}
|
||||
});
|
||||
return roots;
|
||||
}
|
||||
async findFlattened() {
|
||||
const all = await this.prisma.category.findMany({ orderBy: { name: 'asc' } });
|
||||
const byId = new Map();
|
||||
all.forEach((c) => byId.set(c.id, c));
|
||||
const pathCache = new Map();
|
||||
const buildPath = (id) => {
|
||||
const cached = pathCache.get(id);
|
||||
if (cached)
|
||||
return cached;
|
||||
const current = byId.get(id);
|
||||
if (!current)
|
||||
return '';
|
||||
const path = current.parentId == null
|
||||
? current.name
|
||||
: `${buildPath(current.parentId)} > ${current.name}`;
|
||||
pathCache.set(id, path);
|
||||
return path;
|
||||
};
|
||||
return all.map((c) => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
path: buildPath(c.id),
|
||||
}));
|
||||
}
|
||||
};
|
||||
exports.CategoriesService = CategoriesService;
|
||||
exports.CategoriesService = CategoriesService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
||||
], CategoriesService);
|
||||
//# sourceMappingURL=categories.service.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"categories.service.js","sourceRoot":"","sources":["../../src/categories/categories.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,6DAAyD;AAgBlD,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IAC5B,YAA6B,MAAqB;QAArB,WAAM,GAAN,MAAM,CAAe;IAAG,CAAC;IAEtD,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IACrE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