Skip to content

Commit

Permalink
Merge pull request #8 from fga-eps-mds/feat#80/visualizar-conteudo
Browse files Browse the repository at this point in the history
feat(#80):Visualizar-conteúdo
  • Loading branch information
fernandes-natanael authored Aug 31, 2024
2 parents cd9586c + 1a6dc53 commit 1b68101
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/content/content.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/content/content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ export class ContentService {
throw new NotFoundException(`Content with ID ${id} not found`);
}
}

async findContentsByTrailId(trailId: string): Promise<Content[]> {
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();
}

}
1 change: 1 addition & 0 deletions src/trail/trail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class TrailService {
}
return await this.trailModel.find({ journey: journeyId }).exec();
}

async updateTrail(id: string, updateData: Partial<Trail>): Promise<Trail> {
const trail = await this.trailModel
.findByIdAndUpdate(id, updateData, { new: true })
Expand Down
39 changes: 38 additions & 1 deletion test/content.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
});

});

0 comments on commit 1b68101

Please sign in to comment.