Skip to content

Commit

Permalink
Merge pull request #9 from andresWeitzel/feature-04-add-rate-plans-de…
Browse files Browse the repository at this point in the history
…lete

feature-04-add-rate-plans-delete
  • Loading branch information
andresWeitzel authored Nov 28, 2023
2 parents 74c0428 + b1589e9 commit bf35c81
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/rate-plans/rate-plans.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { GetAllRatePlansService } from './services/get-all.service';
import { GetAllRatePlansResolver } from './resolvers/get-all.resolver';
import { GetByIdRatePlansService } from './services/get-by-id.service';
import { GetByIdRatePlansResolver } from './resolvers/get-by-id.resolver';
import { DeleteRatePlansService } from './services/delete.service';
import { DeleteRatePlansResolver } from './resolvers/delete.resolver';

@Module({
imports: [DatabaseModule],
Expand All @@ -30,6 +32,9 @@ import { GetByIdRatePlansResolver } from './resolvers/get-by-id.resolver';
//GetByIdRatePlan
GetByIdRatePlansService,
GetByIdRatePlansResolver,
//DeleteRatePlan
DeleteRatePlansService,
DeleteRatePlansResolver
],
})
export class RatePlansModule {}
50 changes: 50 additions & 0 deletions src/rate-plans/resolvers/delete.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable prettier/prettier */
//External
import { Resolver, Args, Mutation } from '@nestjs/graphql';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Delete } from '@nestjs/common';
//Models
import { RatePlans } from '.././models/rate-plans.entity';
//Service
import { DeleteRatePlansService } from '.././services/delete.service';
//const-vars
let ratePlanResult: any;
let msgResponse: string;
let msgLog: string;

//Check
@Resolver()
@ApiTags('DeleteRatePlansResolver')
export class DeleteRatePlansResolver {
constructor(private deletePlansService: DeleteRatePlansService) {}

/**
* @description function for delete a rate plan to database
* @param {number} inputId number type
* @returns a response with the rate plan object and status code
*/
@Delete('/id/:inputId')
@ApiOperation({ summary: 'Delete a rate plans' })
@Mutation(() => RatePlans, { name: 'deleteRatePlan' })
async deleteRatePlan(
@Args({ name: 'inputId' }) inputId: number,
): Promise<string | any> {
try {
msgResponse = null;
msgLog = null;

// --- start with database operations ---
ratePlanResult = await this.deletePlansService.delete(inputId);
if (ratePlanResult != null || ratePlanResult != undefined) {
return `The Rate plan with the id ${inputId} has been successfully deleted`;
}
// --- end with database operations ---
} catch (error) {
msgResponse =
'ERROR in deleteRatePlan function for DeleteRatePlansResolver class';
msgLog = msgResponse + `Caused by ${error}`;
console.log(msgLog);
return msgResponse;
}
}
}
51 changes: 51 additions & 0 deletions src/rate-plans/services/delete.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable prettier/prettier */
//External
import { Injectable, Inject } from '@nestjs/common';
import { Repository } from 'typeorm';
//Models
import { RatePlans } from '../models/rate-plans.entity';
//Const-vars
let deletedRatePlan: any;
let ratePlanOld:any;
let msgResponse: string;
let msgLog: string;

/**
* @description Class service for delete a rate plan
* @param {Object} event Object type
*/
@Injectable()
export class DeleteRatePlansService {
constructor(
@Inject('RATE_PLANS_REPOSITORY')
private ratePlansRepository: Repository<RatePlans>,
) {}

/**
* @description function to delete a rate plan
* @param {number} inputId number type
* @returns an object with the ratePlan
*/
async delete(inputId: number): Promise<string | any> {
try {
msgResponse = null;
msgLog = null;

ratePlanOld = this.ratePlansRepository.findOne(({
where: {
id: inputId,
},
}));

deletedRatePlan = this.ratePlansRepository.remove(ratePlanOld);

return deletedRatePlan;

} catch (error) {
msgResponse = 'ERROR in delete function for DeleteRatePlansService class';
msgLog = msgResponse + `Caused by ${error}`;
console.log(msgLog);
return msgResponse;
}
}
}
1 change: 1 addition & 0 deletions src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Query {
type Mutation {
createRatePlan(ratePlanObj: RatePlansDTO!): RatePlans!
updateRatePlan(id: Float!, ratePlanObj: RatePlansDTO!): RatePlans!
deleteRatePlan(inputId: Float!): RatePlans!
}

input RatePlansDTO {
Expand Down

0 comments on commit bf35c81

Please sign in to comment.