74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Inject,
|
|
NotFoundException,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import type { Brain } from '@mosaic/brain';
|
|
import { BRAIN } from '../brain/brain.tokens.js';
|
|
import { AuthGuard } from '../auth/auth.guard.js';
|
|
import { CurrentUser } from '../auth/current-user.decorator.js';
|
|
import { assertOwner } from '../auth/resource-ownership.js';
|
|
import { CreateProjectDto, UpdateProjectDto } from './projects.dto.js';
|
|
|
|
@Controller('api/projects')
|
|
@UseGuards(AuthGuard)
|
|
export class ProjectsController {
|
|
constructor(@Inject(BRAIN) private readonly brain: Brain) {}
|
|
|
|
@Get()
|
|
async list() {
|
|
return this.brain.projects.findAll();
|
|
}
|
|
|
|
@Get(':id')
|
|
async findOne(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
|
return this.getOwnedProject(id, user.id);
|
|
}
|
|
|
|
@Post()
|
|
async create(@CurrentUser() user: { id: string }, @Body() dto: CreateProjectDto) {
|
|
return this.brain.projects.create({
|
|
name: dto.name,
|
|
description: dto.description,
|
|
status: dto.status,
|
|
ownerId: user.id,
|
|
});
|
|
}
|
|
|
|
@Patch(':id')
|
|
async update(
|
|
@Param('id') id: string,
|
|
@Body() dto: UpdateProjectDto,
|
|
@CurrentUser() user: { id: string },
|
|
) {
|
|
await this.getOwnedProject(id, user.id);
|
|
const project = await this.brain.projects.update(id, dto);
|
|
if (!project) throw new NotFoundException('Project not found');
|
|
return project;
|
|
}
|
|
|
|
@Delete(':id')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async remove(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
|
await this.getOwnedProject(id, user.id);
|
|
const deleted = await this.brain.projects.remove(id);
|
|
if (!deleted) throw new NotFoundException('Project not found');
|
|
}
|
|
|
|
private async getOwnedProject(id: string, userId: string) {
|
|
const project = await this.brain.projects.findById(id);
|
|
if (!project) throw new NotFoundException('Project not found');
|
|
assertOwner(project.ownerId, userId, 'Project');
|
|
return project;
|
|
}
|
|
}
|