From 2a87164bb5dcf35a72d581bdf8ea15ca1ffae145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Vin=C3=ADcius?= Date: Wed, 24 May 2023 13:53:23 -0300 Subject: [PATCH] #28 Feat: Delete many tutorials --- src/tutorials/tutorials.controller.spec.ts | 38 ++++++++++++++++++++++ src/tutorials/tutorials.controller.ts | 9 +++++ src/tutorials/tutorials.service.ts | 10 ++++++ 3 files changed, 57 insertions(+) diff --git a/src/tutorials/tutorials.controller.spec.ts b/src/tutorials/tutorials.controller.spec.ts index 6084ffc..4667644 100644 --- a/src/tutorials/tutorials.controller.spec.ts +++ b/src/tutorials/tutorials.controller.spec.ts @@ -42,6 +42,7 @@ describe('TutorialsController', () => { .mockResolvedValue(mockTutorialEntityList[0]), updateTutorial: jest.fn().mockResolvedValue(mockUpdateTutorialDto), deleteTutorial: jest.fn().mockResolvedValue('Deletado com sucesso'), + deleteTutorials: jest.fn().mockResolvedValue('Deletados com sucesso'), }, }, ], @@ -123,4 +124,41 @@ describe('TutorialsController', () => { expect(service.deleteTutorial).toHaveBeenCalledWith(id); }); }); + + // Create two mocks for deleteTutorials + const mockTutorials = [ + { + id: '1', + name: 'mockStation', + category_id: mockUuid, + filename: 'mockFile', + data: [37, 80, 68, 70, 45, 49, 46, 52, 10, 37, 226, 227, 207, 211, 10] + }, + { + id: '2', + name: 'mockStation', + category_id: mockUuid, + filename: 'mockFile', + data: [37, 80, 68, 70, 45, 49, 46, 52, 10, 37, 226, 227, 207, 211, 10] + } + ]; + + + describe('deleteManyTutorials', () => { + it('should delete many tutorial entities succesfully', async () => { + + + const ids = mockTutorials.map(tutorial => tutorial.id); + + const result = await controller.deleteTutorials(ids); + + expect(result).toMatch('Deletados com sucesso'); + + expect(service.deleteTutorials).toHaveBeenCalledTimes(1); + + expect(service.deleteTutorials).toHaveBeenCalledWith(ids); + }); + } + ); + }); diff --git a/src/tutorials/tutorials.controller.ts b/src/tutorials/tutorials.controller.ts index 08c9961..de7ab9a 100644 --- a/src/tutorials/tutorials.controller.ts +++ b/src/tutorials/tutorials.controller.ts @@ -9,6 +9,7 @@ import { UseInterceptors, CacheInterceptor, UploadedFile, + ParseArrayPipe, } from '@nestjs/common'; import { TutorialsService } from './tutorials.service'; import { CreateTutorialDto } from './dto/create-tutorial.dto'; @@ -67,4 +68,12 @@ export class TutorialsController { async deleteTutorial(@Param('id') id: string): Promise { return await this.tutorialService.deleteTutorial(id); } + + // Rota para excluir lista de tutoriais cadastrados em lote + @Delete( + 'delete-tutorials/:ids', + ) + async deleteTutorials(@Param('ids', ParseArrayPipe) ids: string[]): Promise { + return await this.tutorialService.deleteTutorials(ids); + } } diff --git a/src/tutorials/tutorials.service.ts b/src/tutorials/tutorials.service.ts index 09fd681..1af9740 100644 --- a/src/tutorials/tutorials.service.ts +++ b/src/tutorials/tutorials.service.ts @@ -104,4 +104,14 @@ export class TutorialsService { throw new InternalServerErrorException(err.message); } } + + // Deleta uma lista de tutoriais + async deleteTutorials(ids: string[]): Promise { + try { + await this.tutorialRepo.delete(ids); + return 'Deletados com sucesso'; + } catch (err) { + throw new InternalServerErrorException(err.message); + } + } }