Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/src/categories/categories.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 24 additions & 3 deletions backend/src/categories/categories.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,13 +38,29 @@ export class CategoriesService {
}

async create(dto: CreateCategoryDto): Promise<Category> {
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<Category> {
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<void> {
const cat = await this.findOne(id);
await this.repo.remove(cat);
}

private async ensureNameUnique(name: string): Promise<void> {
const existing = await this.repo.findOne({ where: { name } });
if (existing) {
throw new ConflictException('A category with this name already exists');
}
}
}
4 changes: 4 additions & 0 deletions backend/src/categories/dto/update-category.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateCategoryDto } from './create-category.dto';

export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
Loading