adding validated services? patching forcad_local.py
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { Controller, HttpCode, Post, Request, UseGuards } from "@nestjs/common";
|
||||
import type { Request as ExpressRequest } from "express";
|
||||
import { LocalAuthGuard } from "../auth/local-auth.guard.js";
|
||||
import { AuthService } from "../services/auth.service.js";
|
||||
|
||||
@Controller("auth")
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@HttpCode(200)
|
||||
@Post("login")
|
||||
async login(@Request() req: ExpressRequest): Promise<{ auth_token: string }> {
|
||||
return this.authService.login(req.user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
HttpCode,
|
||||
NotFoundException,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import type { Request } from "express";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard.js";
|
||||
import { CreateFunctionDto, UpdateFunctionDto } from "@sigma/common";
|
||||
import type { FunctionDocument } from "../schemas/function.schema.js";
|
||||
import { ProjectFunctionsService } from "../services/project-functions.service.js";
|
||||
|
||||
@Controller("projects/:projectId/functions")
|
||||
export class FunctionsController {
|
||||
constructor(
|
||||
private readonly projectFunctionsService: ProjectFunctionsService
|
||||
) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
create(
|
||||
@Req() request: Request,
|
||||
@Param("projectId") projectId: string,
|
||||
@Body() payload: CreateFunctionDto
|
||||
): Promise<FunctionDocument> {
|
||||
return this.projectFunctionsService.create(
|
||||
request.user.userId,
|
||||
projectId,
|
||||
payload
|
||||
);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put(":functionId")
|
||||
async update(
|
||||
@Req() request: Request,
|
||||
@Param("projectId") projectId: string,
|
||||
@Param("functionId") functionId: string,
|
||||
@Body() payload: UpdateFunctionDto
|
||||
): Promise<FunctionDocument> {
|
||||
const func = await this.projectFunctionsService.update(
|
||||
request.user.userId,
|
||||
projectId,
|
||||
functionId,
|
||||
payload
|
||||
);
|
||||
|
||||
if (!func) {
|
||||
throw new NotFoundException("Function not found");
|
||||
}
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(":functionId")
|
||||
@HttpCode(204)
|
||||
async delete(
|
||||
@Req() request: Request,
|
||||
@Param("projectId") projectId: string,
|
||||
@Param("functionId") functionId: string
|
||||
): Promise<FunctionDocument | null> {
|
||||
const func = await this.projectFunctionsService.delete(
|
||||
request.user.userId,
|
||||
projectId,
|
||||
functionId
|
||||
);
|
||||
|
||||
if (!func) {
|
||||
throw new NotFoundException("Function not found");
|
||||
}
|
||||
|
||||
return func;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { HealthService } from "../services/health.service.js";
|
||||
import { CpuInfoDto } from "@sigma/common";
|
||||
|
||||
@Controller("health")
|
||||
export class HealthController {
|
||||
constructor(private healthService: HealthService) {}
|
||||
|
||||
@Get("/cpu")
|
||||
getCpuInfo(): CpuInfoDto {
|
||||
return { usage: this.healthService.getServerCpuUsage() };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import type { Request } from "express";
|
||||
import { ProjectsService } from "../services/projects.service.js";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard.js";
|
||||
import {
|
||||
CreateProjectDto,
|
||||
ProjectDto,
|
||||
QueryProjectDto,
|
||||
UpdateProjectDto,
|
||||
} from "@sigma/common";
|
||||
import type { ProjectDocument } from "../schemas/project.schema.js";
|
||||
|
||||
@Controller("projects")
|
||||
export class ProjectsController {
|
||||
constructor(private readonly projectsService: ProjectsService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
create(
|
||||
@Req() request: Request,
|
||||
@Body() createProjectDto: CreateProjectDto
|
||||
): Promise<ProjectDocument> {
|
||||
return this.projectsService.create(request.user.userId, createProjectDto);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
findAll(
|
||||
@Req() request: Request,
|
||||
@Query() query: QueryProjectDto
|
||||
): Promise<ProjectDocument[]> {
|
||||
return this.projectsService.findAll(request.user.userId, query);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(":id")
|
||||
findOne(
|
||||
@Req() request: Request,
|
||||
@Param("id") id: string
|
||||
): Promise<ProjectDto | null> {
|
||||
return this.projectsService.findOne(request.user.userId, id);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch(":id")
|
||||
update(
|
||||
@Req() request: Request,
|
||||
@Param("id") id: string,
|
||||
@Body() updateProjectDto: UpdateProjectDto
|
||||
): Promise<ProjectDocument | null> {
|
||||
return this.projectsService.update(
|
||||
request.user.userId,
|
||||
id,
|
||||
updateProjectDto
|
||||
);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(":id")
|
||||
@HttpCode(204)
|
||||
remove(
|
||||
@Req() request: Request,
|
||||
@Param("id") id: string
|
||||
): Promise<ProjectDocument | null> {
|
||||
return this.projectsService.remove(request.user.userId, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
HttpCode,
|
||||
Post,
|
||||
Put,
|
||||
UseGuards,
|
||||
Request,
|
||||
Get,
|
||||
ConflictException,
|
||||
} from "@nestjs/common";
|
||||
import type { Request as ExpressRequest } from "express";
|
||||
import { UsersService } from "../services/users.service.js";
|
||||
import { BcryptService } from "../services/bcrypt.service.js";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard.js";
|
||||
import { RegisterUserDto, UpdateUserDto, UserDto } from "@sigma/common";
|
||||
|
||||
@Controller("users")
|
||||
export class UsersController {
|
||||
constructor(
|
||||
private usersService: UsersService,
|
||||
private bcryptService: BcryptService
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async register(@Body() registerDto: RegisterUserDto): Promise<UserDto> {
|
||||
try {
|
||||
const user = await this.usersService.create({
|
||||
username: registerDto.username,
|
||||
passwordHash: await this.bcryptService.hashPassword(
|
||||
registerDto.password
|
||||
),
|
||||
projects: [],
|
||||
});
|
||||
|
||||
return {
|
||||
username: user.username,
|
||||
};
|
||||
} catch (error: any) {
|
||||
if ("code" in error && error.code === 11000) {
|
||||
throw new ConflictException("Username already exists");
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put()
|
||||
async update(
|
||||
@Request() req: ExpressRequest,
|
||||
@Body() updateDto: UpdateUserDto
|
||||
) {
|
||||
this.usersService.update(req.user?._id, {
|
||||
passwordHash: await this.bcryptService.hashPassword(updateDto.password),
|
||||
});
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get("me")
|
||||
async getProfile(@Request() req: ExpressRequest): Promise<UserDto> {
|
||||
const user = await this.usersService.findOne(req.user?.username);
|
||||
|
||||
return {
|
||||
username: user!.username,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user