Skip to content

Commit

Permalink
feat: Categories Create and Delete (#34)
Browse files Browse the repository at this point in the history
# Description

* Deletion of categories
* Creation of new categories

## Checklist

- [x] This PR can be reviewed in under 30 minutes
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] I have assigned reviewers to this PR.
  • Loading branch information
cbolles authored Sep 11, 2024
1 parent 2da2367 commit 1bd1240
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/categories/categories.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<boolean> {
await this.categoryService.delete(category);
return true;
}

@Mutation(() => Category)
async createCategory(@Args('category', CreateCategoryPipe) category: CreateCategory): Promise<Category> {
return this.categoryService.create(category);
}

/**
* Resolver for the services field of the Category type
*/
Expand Down
9 changes: 9 additions & 0 deletions src/categories/categories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<void> {
await this.categoryModel.deleteOne({ _id: category._id });
}

async create(category: CreateCategory): Promise<Category> {
return this.categoryModel.create(category);
}
}
17 changes: 17 additions & 0 deletions src/categories/create.pipe.ts
Original file line number Diff line number Diff line change
@@ -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<CreateCategory, Promise<CreateCategory>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

async transform(value: CreateCategory): Promise<CreateCategory> {
// Ensure the services are valid
for (const service of value.services) {
await this.damplabServicePipe.transform(service);
}

return value;
}
}
8 changes: 8 additions & 0 deletions src/categories/dtos/create.dto.ts
Original file line number Diff line number Diff line change
@@ -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[];
}

0 comments on commit 1bd1240

Please sign in to comment.