From 465d0e90c30dafd59aacb049b17d6c0f38b08352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Tue, 21 Nov 2023 23:48:25 -0300 Subject: [PATCH 1/2] For review --- src/rate-plans/services/delete.service.ts | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/rate-plans/services/delete.service.ts diff --git a/src/rate-plans/services/delete.service.ts b/src/rate-plans/services/delete.service.ts new file mode 100644 index 0000000..484eab0 --- /dev/null +++ b/src/rate-plans/services/delete.service.ts @@ -0,0 +1,49 @@ +/* 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, + ) {} + + /** + * @description function to delete a rate plan + * @param {number} inputId number type + * @returns an object with the ratePlan + */ + async delete(inputId: number): Promise { + try { + msgResponse = null; + msgLog = null; + + ratePlanOld = this.ratePlansRepository.findOne(inputId); + + deletedRatePlan = this.ratePlansRepository.remove(ratePlanOld); + + //https://github.com/typeorm/typeorm/issues/7024 + + return deletedRatePlan; + + } catch (error) { + msgResponse = 'ERROR in delete function for DeleteRatePlansService class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} From b1589e9cf9e8192bac16d3fd876df9862418a03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Mon, 27 Nov 2023 23:29:48 -0300 Subject: [PATCH 2/2] Updated service and controller --- src/rate-plans/rate-plans.module.ts | 5 +++ src/rate-plans/resolvers/delete.resolver.ts | 50 +++++++++++++++++++++ src/rate-plans/services/delete.service.ts | 10 +++-- src/schema.gql | 1 + 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/rate-plans/resolvers/delete.resolver.ts diff --git a/src/rate-plans/rate-plans.module.ts b/src/rate-plans/rate-plans.module.ts index f0fb71b..3840705 100644 --- a/src/rate-plans/rate-plans.module.ts +++ b/src/rate-plans/rate-plans.module.ts @@ -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], @@ -30,6 +32,9 @@ import { GetByIdRatePlansResolver } from './resolvers/get-by-id.resolver'; //GetByIdRatePlan GetByIdRatePlansService, GetByIdRatePlansResolver, + //DeleteRatePlan + DeleteRatePlansService, + DeleteRatePlansResolver ], }) export class RatePlansModule {} diff --git a/src/rate-plans/resolvers/delete.resolver.ts b/src/rate-plans/resolvers/delete.resolver.ts new file mode 100644 index 0000000..03f49a3 --- /dev/null +++ b/src/rate-plans/resolvers/delete.resolver.ts @@ -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 { + 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; + } + } +} diff --git a/src/rate-plans/services/delete.service.ts b/src/rate-plans/services/delete.service.ts index 484eab0..7cd82cb 100644 --- a/src/rate-plans/services/delete.service.ts +++ b/src/rate-plans/services/delete.service.ts @@ -26,17 +26,19 @@ export class DeleteRatePlansService { * @param {number} inputId number type * @returns an object with the ratePlan */ - async delete(inputId: number): Promise { + async delete(inputId: number): Promise { try { msgResponse = null; msgLog = null; - ratePlanOld = this.ratePlansRepository.findOne(inputId); + ratePlanOld = this.ratePlansRepository.findOne(({ + where: { + id: inputId, + }, + })); deletedRatePlan = this.ratePlansRepository.remove(ratePlanOld); - //https://github.com/typeorm/typeorm/issues/7024 - return deletedRatePlan; } catch (error) { diff --git a/src/schema.gql b/src/schema.gql index 2636264..437b8d2 100644 --- a/src/schema.gql +++ b/src/schema.gql @@ -29,6 +29,7 @@ type Query { type Mutation { createRatePlan(ratePlanObj: RatePlansDTO!): RatePlans! updateRatePlan(id: Float!, ratePlanObj: RatePlansDTO!): RatePlans! + deleteRatePlan(inputId: Float!): RatePlans! } input RatePlansDTO {