Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Categories Create and Delete #34

Merged
merged 4 commits into from
Sep 11, 2024
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
13 changes: 13 additions & 0 deletions src/categories/categories.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,37 @@
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 {
constructor(private readonly categoryService: CategoryService, private readonly damplabServices: DampLabServices) {}

@Query(() => [Category])
async categories(): Promise<Category[]> {
return this.categoryService.findAll();
}

@Mutation(() => Category)
async updateCategory(
@Args('category', { type: () => ID }, CategoryPipe) category: Category,
@Args('changes', { type: () => CategoryChange }, CategoryUpdatePipe) changes: CategoryChange
): Promise<Category> {
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> {

Check warning on line 36 in src/categories/categories.resolver.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return this.categoryService.create(category);
}

Check warning on line 38 in src/categories/categories.resolver.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

/**
* 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,21 +3,30 @@
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 {
constructor(@InjectModel(Category.name) private readonly categoryModel: Model<CategoryDocument>) {}

async findAll(): Promise<Category[]> {
return this.categoryModel.find().exec();
}

async find(id: string): Promise<Category | null> {
return this.categoryModel.findById(id);
}

async update(category: Category, change: CategoryChange): Promise<Category> {
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> {

Check warning on line 29 in src/categories/categories.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return this.categoryModel.create(category);

Check warning on line 30 in src/categories/categories.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}

Check warning on line 32 in src/categories/categories.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 32 in src/categories/categories.service.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
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';

Check warning on line 2 in src/categories/create.pipe.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
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';

Check warning on line 1 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
import { Category } from '../category.model';

Check warning on line 2 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

@InputType()
export class CreateCategory extends OmitType(Category, ['_id', 'services'] as const, InputType) {

Check warning on line 5 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
@Field(() => [ID])

Check warning on line 6 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 6 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
services: string[];

Check warning on line 7 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 8 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 8 in src/categories/dtos/create.dto.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
Loading