diff --git a/src/content/content.controller.ts b/src/content/content.controller.ts index a5a7c44..e54b125 100644 --- a/src/content/content.controller.ts +++ b/src/content/content.controller.ts @@ -38,6 +38,11 @@ export class ContentController { return this.contentService.findAllContents(); } + @Get('trail/:id') + async findContentsByTrailId(@Param('id') id: string) { + return this.contentService.findContentsByTrailId(id); + } + @Patch(':id') async updateContent( @Param('id') id: string, diff --git a/src/content/content.service.ts b/src/content/content.service.ts index ed9aea4..b854285 100644 --- a/src/content/content.service.ts +++ b/src/content/content.service.ts @@ -68,4 +68,13 @@ export class ContentService { throw new NotFoundException(`Content with ID ${id} not found`); } } + + async findContentsByTrailId(trailId: string): Promise { + const trail = await this.trailModel.findById(trailId).exec(); + if (!trail) { + throw new NotFoundException(`Trail with ID ${trailId} not found`); + } + return await this.contentModel.find({ trail: trailId }).exec(); + } + } diff --git a/src/trail/trail.service.ts b/src/trail/trail.service.ts index 9953146..8f75f1c 100644 --- a/src/trail/trail.service.ts +++ b/src/trail/trail.service.ts @@ -84,6 +84,7 @@ export class TrailService { } return await this.trailModel.find({ journey: journeyId }).exec(); } + async updateTrail(id: string, updateData: Partial): Promise { const trail = await this.trailModel .findByIdAndUpdate(id, updateData, { new: true }) diff --git a/test/content.service.spec.ts b/test/content.service.spec.ts index fcee0b6..b3e327b 100644 --- a/test/content.service.spec.ts +++ b/test/content.service.spec.ts @@ -171,4 +171,41 @@ describe('ContentService', () => { ); }); }); -}); + + describe('findContentsByTrailId', () => { + it('deve retornar uma lista de conteúdos quando a trilha for encontrada', async () => { + const trailId = 'some-trail-id'; + const mockTrail = { _id: trailId, name: 'Test Trail' }; + const mockContents = [ + { _id: 'content1', title: 'Content 1', trail: trailId }, + { _id: 'content2', title: 'Content 2', trail: trailId }, + ]; + + mockTrailModel.findById.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockTrail), + }); + + mockContentModel.find.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(mockContents), + }); + + const result = await service.findContentsByTrailId(trailId); + expect(result).toEqual(mockContents); + expect(mockTrailModel.findById).toHaveBeenCalledWith(trailId); + expect(mockContentModel.find).toHaveBeenCalledWith({ trail: trailId }); + }); + + it('deve lançar uma exceção NotFoundException quando a trilha não for encontrada', async () => { + const trailId = 'some-trail-id'; + + mockTrailModel.findById.mockReturnValueOnce({ + exec: jest.fn().mockResolvedValue(null), + }); + + await expect(service.findContentsByTrailId(trailId)).rejects.toThrow( + NotFoundException, + ); + }); + }); + +}); \ No newline at end of file