Skip to content

Commit

Permalink
feat: Services CRUD (#35)
Browse files Browse the repository at this point in the history
# Description

* Create service
* Delete service

## 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 12, 2024
1 parent 1bd1240 commit cd943e8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/services/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 { CreateService } from './dtos/create.dto';

@Injectable()
export class CreateServicePipe implements PipeTransform<CreateService, Promise<CreateService>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

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

return value;
}
}
3 changes: 2 additions & 1 deletion src/services/damplab-services.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CreateServicePipe } from './create.pipe';
import { DampLabServicePipe } from './damplab-services.pipe';
import { DampLabServicesResolver } from './damplab-services.resolver';
import { DampLabServices } from './damplab-services.services';
Expand All @@ -8,7 +9,7 @@ import { ServiceUpdatePipe } from './update.pipe';

@Module({
imports: [MongooseModule.forFeature([{ name: DampLabService.name, schema: DampLabServiceSchema }])],
providers: [DampLabServicesResolver, DampLabServices, DampLabServicePipe, ServiceUpdatePipe],
providers: [DampLabServicesResolver, DampLabServices, DampLabServicePipe, ServiceUpdatePipe, CreateServicePipe],
exports: [DampLabServices, DampLabServicePipe]
})
export class DampLabServicesModule {}
13 changes: 13 additions & 0 deletions src/services/damplab-services.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Resolver, Query, ResolveField, Parent, ID, Args, Mutation } from '@nestjs/graphql';
import { CreateServicePipe } from './create.pipe';
import { DampLabServicePipe } from './damplab-services.pipe';
import { DampLabServices } from './damplab-services.services';
import { CreateService } from './dtos/create.dto';
import { ServiceChange } from './dtos/update.dto';
import { DampLabService } from './models/damplab-service.model';
import { ServiceUpdatePipe } from './update.pipe';
Expand All @@ -22,6 +24,17 @@ export class DampLabServicesResolver {
return this.dampLabServices.update(service, changes);
}

@Mutation(() => Boolean)
async deleteService(@Args('service', { type: () => ID }, DampLabServicePipe) service: DampLabService): Promise<boolean> {
await this.dampLabServices.delete(service);
return true;
}

@Mutation(() => DampLabService)
async createService(@Args('service', CreateServicePipe) service: CreateService): Promise<DampLabService> {
return this.dampLabServices.create(service);
}

/**
* Resolver which the `allowedConnections` field of the `DampLabService`
* type. Allows for the recursive search on possible connections.
Expand Down
17 changes: 17 additions & 0 deletions src/services/damplab-services.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import mongoose from 'mongoose';
import { ServiceChange } from './dtos/update.dto';
import { CreateService } from './dtos/create.dto';

@Injectable()
export class DampLabServices {
Expand All @@ -28,4 +29,20 @@ export class DampLabServices {
await this.dampLabServiceModel.updateOne({ _id: service._id }, changes);
return (await this.dampLabServiceModel.findById(service._id))!;
}

async delete(service: DampLabService): Promise<void> {
// Remove all allowed connections first
await this.dampLabServiceModel.updateMany(
{},
{
$pull: { allowedConnections: service._id }
}
);

await this.dampLabServiceModel.deleteOne({ _id: service._id });
}

async create(service: CreateService): Promise<DampLabService> {
return this.dampLabServiceModel.create(service);
}
}
8 changes: 8 additions & 0 deletions src/services/dtos/create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ID, InputType, Field, OmitType } from '@nestjs/graphql';
import { DampLabService } from '../models/damplab-service.model';

@InputType()
export class CreateService extends OmitType(DampLabService, ['_id', 'allowedConnections'] as const, InputType) {
@Field(() => [ID])
allowedConnections: string[];
}

0 comments on commit cd943e8

Please sign in to comment.