feat: implement ApiTokenGuard for authentication and apply it to relevant controllers
This commit is contained in:
67
src/query/handler/query.controller.ts
Normal file
67
src/query/handler/query.controller.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Headers,
|
||||
Inject,
|
||||
Param,
|
||||
Post,
|
||||
Res,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { Response } from "express";
|
||||
import { QueryHandlerService } from "./query.handler.service";
|
||||
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
|
||||
import { QueryExecuterService } from "../executer/query.executer.service";
|
||||
|
||||
@Controller("query")
|
||||
@UseGuards(ApiTokenGuard)
|
||||
export class QueryController {
|
||||
constructor(
|
||||
@Inject(QueryHandlerService)
|
||||
private readonly queryHandlerService: QueryHandlerService,
|
||||
@Inject(QueryExecuterService)
|
||||
private readonly queryExecuterService: QueryExecuterService
|
||||
) {}
|
||||
|
||||
@Post("create")
|
||||
async createQuery(
|
||||
@Body() queryData: { projectToken: string; source: string }
|
||||
) {
|
||||
return this.queryHandlerService.createQuery(queryData);
|
||||
}
|
||||
|
||||
@Post("update/:id")
|
||||
async updateQuery(
|
||||
@Body() updateData: Partial<{ source: string }>,
|
||||
@Inject("id") id: string
|
||||
) {
|
||||
return this.queryHandlerService.updateQuery(id, updateData);
|
||||
}
|
||||
|
||||
@Post("/run/:token")
|
||||
async runQuery(
|
||||
@Param("token") token: string,
|
||||
@Body() query: Record<string, any>,
|
||||
@Headers() headers: Record<string, any>,
|
||||
@Res() res: Response
|
||||
) {
|
||||
const queryResult = await this.queryExecuterService.runQuery(
|
||||
token,
|
||||
query,
|
||||
headers
|
||||
);
|
||||
|
||||
res.status(queryResult.statusCode);
|
||||
|
||||
if (queryResult.statusCode === 302 && queryResult.headers["Location"]) {
|
||||
res.redirect(queryResult.headers["Location"]);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(queryResult.headers)) {
|
||||
res.setHeader(key, value);
|
||||
}
|
||||
|
||||
res.send(queryResult.response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user