Skip to content

Commit

Permalink
Prepare api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Ho committed Aug 14, 2024
1 parent 5e6bba1 commit bd8c9c2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions demo/api/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -1453,11 +1453,13 @@ input ProductUpdateInput {
input ProductCategoryInput {
title: String!
slug: String!
products: [ID!]! = []
}

input ProductCategoryUpdateInput {
title: String
slug: String
products: [ID!]
}

input ProductTagInput {
Expand Down
2 changes: 1 addition & 1 deletion demo/api/src/products/entities/product-category.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ProductCategory extends BaseEntity<ProductCategory, "id"> {
//search: true, //not implemented
//filter: true, //not implemented
//sort: true, //not implemented
input: false, //default is true
input: true, //default is true
})
@OneToMany(() => Product, (products) => products.category)
products = new Collection<Product>(this);
Expand Down
9 changes: 7 additions & 2 deletions demo/api/src/products/generated/dto/product-category.input.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file has been generated by comet api-generator.
// You may choose to use this file as scaffold by moving this file out of generated folder and removing this comment.
import { IsSlug, PartialType } from "@comet/cms-api";
import { Field, InputType } from "@nestjs/graphql";
import { IsNotEmpty, IsString } from "class-validator";
import { Field, ID, InputType } from "@nestjs/graphql";
import { IsArray, IsNotEmpty, IsString, IsUUID } from "class-validator";

@InputType()
export class ProductCategoryInput {
Expand All @@ -16,6 +16,11 @@ export class ProductCategoryInput {
@IsSlug()
@Field()
slug: string;

@Field(() => [ID], { defaultValue: [] })
@IsArray()
@IsUUID(undefined, { each: true })
products: string[];
}

@InputType()
Expand Down
23 changes: 20 additions & 3 deletions demo/api/src/products/generated/product-category.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file has been generated by comet api-generator.
// You may choose to use this file as scaffold by moving this file out of generated folder and removing this comment.
import { AffectedEntity, extractGraphqlFields, gqlArgsToMikroOrmQuery, RequiredPermission } from "@comet/cms-api";
import { FindOptions } from "@mikro-orm/core";
import { FindOptions, Reference } from "@mikro-orm/core";
import { InjectRepository } from "@mikro-orm/nestjs";
import { EntityManager, EntityRepository } from "@mikro-orm/postgresql";
import { Args, ID, Info, Mutation, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql";
Expand All @@ -19,6 +19,7 @@ export class ProductCategoryResolver {
constructor(
private readonly entityManager: EntityManager,
@InjectRepository(ProductCategory) private readonly repository: EntityRepository<ProductCategory>,
@InjectRepository(Product) private readonly productRepository: EntityRepository<Product>,
) {}

@Query(() => ProductCategory)
Expand Down Expand Up @@ -65,10 +66,18 @@ export class ProductCategoryResolver {

@Mutation(() => ProductCategory)
async createProductCategory(@Args("input", { type: () => ProductCategoryInput }) input: ProductCategoryInput): Promise<ProductCategory> {
const { products: productsInput, ...assignInput } = input;
const productCategory = this.repository.create({
...input,
...assignInput,
});

if (productsInput) {
const products = await this.productRepository.find({ id: productsInput });
if (products.length != productsInput.length) throw new Error("Couldn't find all products that were passed as input");
await productCategory.products.loadItems();
productCategory.products.set(products.map((product) => Reference.create(product)));
}

await this.entityManager.flush();

return productCategory;
Expand All @@ -82,10 +91,18 @@ export class ProductCategoryResolver {
): Promise<ProductCategory> {
const productCategory = await this.repository.findOneOrFail(id);

const { products: productsInput, ...assignInput } = input;
productCategory.assign({
...input,
...assignInput,
});

if (productsInput) {
const products = await this.productRepository.find({ id: productsInput });
if (products.length != productsInput.length) throw new Error("Couldn't find all products that were passed as input");
await productCategory.products.loadItems();
productCategory.products.set(products.map((product) => Reference.create(product)));
}

await this.entityManager.flush();

return productCategory;
Expand Down

0 comments on commit bd8c9c2

Please sign in to comment.