diff --git a/src/categories/categories.resolver.ts b/src/categories/categories.resolver.ts index 6d2a35a..fae5872 100644 --- a/src/categories/categories.resolver.ts +++ b/src/categories/categories.resolver.ts @@ -6,6 +6,8 @@ import { DampLabService } from '../services/models/damplab-service.model'; import { CategoryPipe } from './categories.pipe'; import { CategoryChange } from './dtos/update.dto'; import { CategoryUpdatePipe } from './update.pipe'; +import { CreateCategory } from './dtos/create.dto'; +import { CreateCategoryPipe } from './create.pipe'; @Resolver(() => Category) export class CategoryResolver { @@ -24,6 +26,17 @@ export class CategoryResolver { return this.categoryService.update(category, changes); } + @Mutation(() => Boolean) + async deleteCategory(@Args('category', { type: () => ID }, CategoryPipe) category: Category): Promise { + await this.categoryService.delete(category); + return true; + } + + @Mutation(() => Category) + async createCategory(@Args('category', CreateCategoryPipe) category: CreateCategory): Promise { + return this.categoryService.create(category); + } + /** * Resolver for the services field of the Category type */ diff --git a/src/categories/categories.service.ts b/src/categories/categories.service.ts index 6f65c24..e39caf8 100644 --- a/src/categories/categories.service.ts +++ b/src/categories/categories.service.ts @@ -3,6 +3,7 @@ import { InjectModel } from '@nestjs/mongoose'; import { Category, CategoryDocument } from './category.model'; import { Model } from 'mongoose'; import { CategoryChange } from './dtos/update.dto'; +import { CreateCategory } from './dtos/create.dto'; @Injectable() export class CategoryService { @@ -20,4 +21,12 @@ export class CategoryService { await this.categoryModel.updateOne({ _id: category._id }, change); return (await this.find(category._id))!; } + + async delete(category: Category): Promise { + await this.categoryModel.deleteOne({ _id: category._id }); + } + + async create(category: CreateCategory): Promise { + return this.categoryModel.create(category); + } } diff --git a/src/categories/create.pipe.ts b/src/categories/create.pipe.ts new file mode 100644 index 0000000..be8a98c --- /dev/null +++ b/src/categories/create.pipe.ts @@ -0,0 +1,17 @@ +import { Injectable, PipeTransform } from '@nestjs/common'; +import { DampLabServicePipe } from '../services/damplab-services.pipe'; +import { CreateCategory } from './dtos/create.dto'; + +@Injectable() +export class CreateCategoryPipe implements PipeTransform> { + constructor(private readonly damplabServicePipe: DampLabServicePipe) {} + + async transform(value: CreateCategory): Promise { + // Ensure the services are valid + for (const service of value.services) { + await this.damplabServicePipe.transform(service); + } + + return value; + } +} diff --git a/src/categories/dtos/create.dto.ts b/src/categories/dtos/create.dto.ts new file mode 100644 index 0000000..fe7a033 --- /dev/null +++ b/src/categories/dtos/create.dto.ts @@ -0,0 +1,8 @@ +import { ID, InputType, OmitType, Field } from '@nestjs/graphql'; +import { Category } from '../category.model'; + +@InputType() +export class CreateCategory extends OmitType(Category, ['_id', 'services'] as const, InputType) { + @Field(() => [ID]) + services: string[]; +}