Files
noahspan-root/api/src/project/project.controller.ts

99 lines
2.4 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
HttpException,
Param,
Post,
Put,
UseGuards
} from '@nestjs/common';
import { ProjectDto } from './project.dto';
import { Project } from './project.entity';
import { ProjectService } from './project.service';
import { AuthGuard, CustomError, Public } from '@noahspan/noahspan-modules';
@Controller('projects')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}
@Get(':partitionKey/:rowKey')
@UseGuards(AuthGuard)
async find(
@Param('partitionKey') partitionKey: string,
@Param('rowKey') rowKey: string
) {
try {
return await this.projectService.find(partitionKey, rowKey);
} catch (error) {
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode);
}
}
@Get()
async findAll() {
try {
return await this.projectService.findAll();
} catch (error) {
console.log(error)
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode);
}
}
@Post()
@UseGuards(AuthGuard)
async create(@Body() projectDto: ProjectDto) {
try {
const project = new Project();
Object.assign(project, projectDto);
return await this.projectService.create(project);
} catch (error) {
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode);
}
}
@Put(':partitionKey/:rowKey')
@UseGuards(AuthGuard)
async update(
@Param('partitionKey') partitionKey: string,
@Param('rowKey') rowKey: string,
@Body() projectDto: ProjectDto
) {
try {
const project = new Project();
Object.assign(project, projectDto);
return await this.projectService.update(partitionKey, rowKey, project);
} catch (error) {
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode);
}
}
@Delete(':partitionKey/:rowKey')
@UseGuards(AuthGuard)
async delete(
@Param('partitionKey') partitionKey: string,
@Param('rowKey') rowKey: string
) {
try {
return await this.projectService.delete(partitionKey, rowKey);
} catch (error) {
const customError = error as CustomError;
throw new HttpException(customError.message, customError.statusCode);
}
}
}