feat: implement function management with FunctionEntity, FunctionService, and FunctionController; enhance QueryExecuterService to utilize functions; refactor CommandController and QueryController to extend BaseQueryController; update Vm class to handle functions; remove obsolete log entities
This commit is contained in:
@ -9,6 +9,7 @@ import {
|
||||
PrimaryGeneratedColumn,
|
||||
} from "typeorm";
|
||||
import { Database } from "../../databaseManager/entities/database.entity";
|
||||
import { FunctionEntity } from "../../query/entities/function.entity";
|
||||
|
||||
@Entity("project")
|
||||
export class Project {
|
||||
@ -27,4 +28,7 @@ export class Project {
|
||||
|
||||
@OneToMany(() => Query, (query) => query.project)
|
||||
queries: Query[];
|
||||
|
||||
@OneToMany(() => FunctionEntity, (functionEntity) => functionEntity.project)
|
||||
functions: FunctionEntity[];
|
||||
}
|
||||
|
||||
22
src/project/function/function.controller.ts
Normal file
22
src/project/function/function.controller.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Controller, Inject, Post, UseGuards } from "@nestjs/common";
|
||||
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
|
||||
import { FunctionService } from "./function.service";
|
||||
|
||||
@Controller("functions")
|
||||
@UseGuards(ApiTokenGuard)
|
||||
export class FunctionController {
|
||||
constructor(
|
||||
@Inject(FunctionService)
|
||||
private readonly functionService: FunctionService
|
||||
) {}
|
||||
|
||||
@Post("create")
|
||||
async createFunction(projectId: string, name: string, source: string) {
|
||||
return this.functionService.create(projectId, name, source);
|
||||
}
|
||||
|
||||
@Post("delete")
|
||||
async deleteFunction(projectId: string, name: string) {
|
||||
return this.functionService.deleteFunction(projectId, name);
|
||||
}
|
||||
}
|
||||
65
src/project/function/function.service.ts
Normal file
65
src/project/function/function.service.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { FunctionEntity } from "src/query/entities/function.entity";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { ProjectService } from "../project.service";
|
||||
|
||||
@Injectable()
|
||||
export class FunctionService {
|
||||
constructor(
|
||||
@InjectRepository(FunctionEntity)
|
||||
private readonly functionRepository: Repository<FunctionEntity>,
|
||||
@Inject(ProjectService)
|
||||
private readonly projectService: ProjectService
|
||||
) {}
|
||||
|
||||
async create(
|
||||
projectId: string,
|
||||
name: string,
|
||||
source: string
|
||||
): Promise<FunctionEntity> {
|
||||
const project = await this.projectService.findById(projectId);
|
||||
|
||||
if (!project) {
|
||||
throw new Error("Project not found");
|
||||
}
|
||||
|
||||
const existingFunction = await this.functionRepository.findOne({
|
||||
where: { name, project: { id: projectId } },
|
||||
});
|
||||
|
||||
if (existingFunction) {
|
||||
existingFunction.source = source;
|
||||
return this.functionRepository.save(existingFunction);
|
||||
}
|
||||
|
||||
const functionEntity = this.functionRepository.create({
|
||||
name,
|
||||
source,
|
||||
project,
|
||||
});
|
||||
|
||||
return this.functionRepository.save(functionEntity);
|
||||
}
|
||||
|
||||
async findByNames(
|
||||
projectId: string,
|
||||
names: string[]
|
||||
): Promise<FunctionEntity[]> {
|
||||
return this.functionRepository.find({
|
||||
where: { project: { id: projectId }, name: In(names) },
|
||||
});
|
||||
}
|
||||
|
||||
async deleteFunction(projectId: string, name: string): Promise<void> {
|
||||
const functionEntity = await this.functionRepository.findOne({
|
||||
where: { name, project: { id: projectId } },
|
||||
});
|
||||
|
||||
if (!functionEntity) {
|
||||
throw new Error("Function not found");
|
||||
}
|
||||
|
||||
await this.functionRepository.remove(functionEntity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user