From af05b1d9e0d9da39af940088e80dade156ce9bab Mon Sep 17 00:00:00 2001 From: cbolles Date: Fri, 6 Sep 2024 12:38:48 -0400 Subject: [PATCH 1/4] Add deletion onto category --- src/categories/categories.resolver.ts | 6 ++++++ src/categories/categories.service.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/categories/categories.resolver.ts b/src/categories/categories.resolver.ts index 6d2a35a..7cb8fb1 100644 --- a/src/categories/categories.resolver.ts +++ b/src/categories/categories.resolver.ts @@ -24,6 +24,12 @@ 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; + } + /** * 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..9fce698 100644 --- a/src/categories/categories.service.ts +++ b/src/categories/categories.service.ts @@ -20,4 +20,8 @@ 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 }); + } } From 74b8fff16f73c0b12f06cc1ac6f0f0e41b831e5e Mon Sep 17 00:00:00 2001 From: cbolles Date: Mon, 9 Sep 2024 14:21:42 -0400 Subject: [PATCH 2/4] Working creation of new categories --- src/categories/categories.resolver.ts | 9 +++++++++ src/categories/categories.service.ts | 5 +++++ src/categories/create.pipe.ts | 17 +++++++++++++++++ src/categories/dtos/create.dto.ts | 8 ++++++++ 4 files changed, 39 insertions(+) create mode 100644 src/categories/create.pipe.ts create mode 100644 src/categories/dtos/create.dto.ts diff --git a/src/categories/categories.resolver.ts b/src/categories/categories.resolver.ts index 7cb8fb1..495f80d 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 { @@ -30,6 +32,13 @@ export class CategoryResolver { 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 9fce698..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 { @@ -24,4 +25,8 @@ export class CategoryService { 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..99e9014 --- /dev/null +++ b/src/categories/create.pipe.ts @@ -0,0 +1,17 @@ +import { Injectable, PipeTransform } from '@nestjs/common'; +import { DampLabServicePipe } from 'src/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[]; +} From 4e5bd84380dc661c8511a07953a5cf76bac80424 Mon Sep 17 00:00:00 2001 From: cbolles Date: Mon, 9 Sep 2024 14:22:26 -0400 Subject: [PATCH 3/4] Fix formatting --- src/categories/categories.resolver.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/categories/categories.resolver.ts b/src/categories/categories.resolver.ts index 495f80d..fae5872 100644 --- a/src/categories/categories.resolver.ts +++ b/src/categories/categories.resolver.ts @@ -33,9 +33,7 @@ export class CategoryResolver { } @Mutation(() => Category) - async createCategory( - @Args('category', CreateCategoryPipe) category: CreateCategory - ): Promise { + async createCategory(@Args('category', CreateCategoryPipe) category: CreateCategory): Promise { return this.categoryService.create(category); } From 23af6963f27c3cab8f39cce402d6081959a3143a Mon Sep 17 00:00:00 2001 From: cbolles Date: Mon, 9 Sep 2024 14:26:58 -0400 Subject: [PATCH 4/4] Fix import strategy --- src/categories/create.pipe.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/categories/create.pipe.ts b/src/categories/create.pipe.ts index 99e9014..be8a98c 100644 --- a/src/categories/create.pipe.ts +++ b/src/categories/create.pipe.ts @@ -1,5 +1,5 @@ import { Injectable, PipeTransform } from '@nestjs/common'; -import { DampLabServicePipe } from 'src/services/damplab-services.pipe'; +import { DampLabServicePipe } from '../services/damplab-services.pipe'; import { CreateCategory } from './dtos/create.dto'; @Injectable()