seperated backend and frontend
This commit is contained in:
11
backend/src/build-parts/build-parts.module.ts
Normal file
11
backend/src/build-parts/build-parts.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BuildPartsService } from './build-parts.service';
|
||||
import { BuildPartsResolver } from './build-parts.resolver';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { BuildParts, BuildPartsSchema } from '@/models/build-parts.model';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: BuildParts.name, schema: BuildPartsSchema }])],
|
||||
providers: [BuildPartsService, BuildPartsResolver]
|
||||
})
|
||||
export class BuildPartsModule { }
|
||||
18
backend/src/build-parts/build-parts.resolver.spec.ts
Normal file
18
backend/src/build-parts/build-parts.resolver.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { BuildPartsResolver } from './build-parts.resolver';
|
||||
|
||||
describe('BuildPartsResolver', () => {
|
||||
let resolver: BuildPartsResolver;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [BuildPartsResolver],
|
||||
}).compile();
|
||||
|
||||
resolver = module.get<BuildPartsResolver>(BuildPartsResolver);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(resolver).toBeDefined();
|
||||
});
|
||||
});
|
||||
28
backend/src/build-parts/build-parts.resolver.ts
Normal file
28
backend/src/build-parts/build-parts.resolver.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
|
||||
import { Types } from 'mongoose';
|
||||
import { BuildParts } from '@/models/build-parts.model';
|
||||
import { CreateBuildPartsInput } from './dto/create-build-part.input';
|
||||
import graphqlFields from 'graphql-fields';
|
||||
import type { GraphQLResolveInfo } from 'graphql';
|
||||
import { BuildPartsService } from './build-parts.service';
|
||||
|
||||
@Resolver()
|
||||
export class BuildPartsResolver {
|
||||
|
||||
constructor(private readonly buildPartsService: BuildPartsService) { }
|
||||
|
||||
@Query(() => [BuildParts], { name: 'BuildParts' })
|
||||
async getBuildParts(@Info() info: GraphQLResolveInfo): Promise<BuildParts[]> {
|
||||
const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findAll(projection);
|
||||
}
|
||||
|
||||
@Query(() => BuildParts, { name: 'BuildParts', nullable: true })
|
||||
async getBuildPart(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<BuildParts | null> {
|
||||
const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findById(new Types.ObjectId(id), projection);
|
||||
}
|
||||
|
||||
@Mutation(() => BuildParts, { name: 'createBuildPart' })
|
||||
async createBuildPart(@Args('input') input: CreateBuildPartsInput): Promise<BuildParts> {
|
||||
return this.buildPartsService.create(input);
|
||||
}
|
||||
}
|
||||
18
backend/src/build-parts/build-parts.service.spec.ts
Normal file
18
backend/src/build-parts/build-parts.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { BuildPartsService } from './build-parts.service';
|
||||
|
||||
describe('BuildPartsService', () => {
|
||||
let service: BuildPartsService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [BuildPartsService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<BuildPartsService>(BuildPartsService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
33
backend/src/build-parts/build-parts.service.ts
Normal file
33
backend/src/build-parts/build-parts.service.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Types, Model } from 'mongoose';
|
||||
import { BuildParts, BuildPartsDocument } from '@/models/build-parts.model';
|
||||
import { CreateBuildPartsInput } from './dto/create-build-part.input';
|
||||
|
||||
@Injectable()
|
||||
export class BuildPartsService {
|
||||
|
||||
constructor(@InjectModel(BuildParts.name) private readonly buildPartsModel: Model<BuildPartsDocument>) { }
|
||||
|
||||
async findAll(projection?: any): Promise<BuildPartsDocument[]> {
|
||||
return this.buildPartsModel.find({}, projection, { lean: false }).populate({ path: 'buildParts', select: projection?.buildParts }).exec();
|
||||
}
|
||||
|
||||
async findById(id: Types.ObjectId, projection?: any): Promise<BuildPartsDocument | null> {
|
||||
return this.buildPartsModel.findById(id, projection, { lean: false }).populate({ path: 'buildParts', select: projection?.buildParts }).exec();
|
||||
}
|
||||
|
||||
async create(input: CreateBuildPartsInput): Promise<BuildPartsDocument> {
|
||||
const buildParts = new this.buildPartsModel(input);
|
||||
return buildParts.save();
|
||||
}
|
||||
|
||||
buildProjection(fields: Record<string, any>): any {
|
||||
const projection: any = {};
|
||||
for (const key in fields) {
|
||||
if (key === 'buildParts' && typeof fields[key] === 'object') { for (const subField of Object.keys(fields[key])) { projection[`buildParts.${subField}`] = 1 } }
|
||||
else { projection[key] = 1 }
|
||||
}
|
||||
return projection;
|
||||
}
|
||||
}
|
||||
7
backend/src/build-parts/dto/create-build-part.input.ts
Normal file
7
backend/src/build-parts/dto/create-build-part.input.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { InputType, Field, ID } from '@nestjs/graphql';
|
||||
|
||||
|
||||
@InputType()
|
||||
export class CreateBuildPartsInput {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user