diff --git a/backend/src/categories/categories.controller.ts b/backend/src/categories/categories.controller.ts index ed93b41..9063416 100644 --- a/backend/src/categories/categories.controller.ts +++ b/backend/src/categories/categories.controller.ts @@ -1,7 +1,8 @@ -import { Controller, Get, Post, Delete, Body, Param, UseGuards } from '@nestjs/common'; +import { Controller, Get, Post, Delete, Body, Param, UseGuards, Patch } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; import { CategoriesService } from './categories.service'; import { CreateCategoryDto } from './dto/create-category.dto'; +import { UpdateCategoryDto } from './dto/update-category.dto'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; @ApiTags('Categories') @@ -29,6 +30,12 @@ export class CategoriesController { return this.service.create(dto); } + @Patch(':id') + @ApiOperation({ summary: 'Update a category' }) + update(@Param('id') id: string, @Body() dto: UpdateCategoryDto) { + return this.service.update(id, dto); + } + @Delete(':id') @ApiOperation({ summary: 'Delete a category' }) remove(@Param('id') id: string) { diff --git a/backend/src/categories/categories.service.ts b/backend/src/categories/categories.service.ts index 687b2f4..0f70d73 100644 --- a/backend/src/categories/categories.service.ts +++ b/backend/src/categories/categories.service.ts @@ -1,8 +1,13 @@ -import { Injectable, NotFoundException, ConflictException } from '@nestjs/common'; +import { + ConflictException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Category } from './category.entity'; import { CreateCategoryDto } from './dto/create-category.dto'; +import { UpdateCategoryDto } from './dto/update-category.dto'; export interface CategoryWithCount extends Category { assetCount: number; @@ -33,13 +38,29 @@ export class CategoriesService { } async create(dto: CreateCategoryDto): Promise { - const existing = await this.repo.findOne({ where: { name: dto.name } }); - if (existing) throw new ConflictException('A category with this name already exists'); + await this.ensureNameUnique(dto.name); return this.repo.save(this.repo.create(dto)); } + async update(id: string, dto: UpdateCategoryDto): Promise { + const category = await this.findOne(id); + if (dto.name && dto.name !== category.name) { + await this.ensureNameUnique(dto.name); + } + + Object.assign(category, dto); + return this.repo.save(category); + } + async remove(id: string): Promise { const cat = await this.findOne(id); await this.repo.remove(cat); } + + private async ensureNameUnique(name: string): Promise { + const existing = await this.repo.findOne({ where: { name } }); + if (existing) { + throw new ConflictException('A category with this name already exists'); + } + } } diff --git a/backend/src/categories/dto/update-category.dto.ts b/backend/src/categories/dto/update-category.dto.ts new file mode 100644 index 0000000..1ace9d9 --- /dev/null +++ b/backend/src/categories/dto/update-category.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreateCategoryDto } from './create-category.dto'; + +export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}