Skip to content

Commit

Permalink
feat: add get parent content
Browse files Browse the repository at this point in the history
fix #27
  • Loading branch information
Leonardo Ferreira Lima committed Sep 27, 2023
1 parent 1856e9f commit 504da8e
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,33 @@ const contentChildren = await tabNews.contents.getChildren({
});
```

**Buscar Conteúdo Pai**

```js
import { TabNews } from 'tabnews-sdk';

const tabNews = new TabNews();

const parentContent = await tabNews.contents.getParent({
slug: '<slug>',
username: '<username>',
});
```

```js
import { TabNews } from 'tabnews-sdk';

const tabNews = new TabNews();

await tabNews.session.create();

// Não é preciso passar o username pois internamente a bliblioteca ira realizar o fecth do usuario atual

const parentContent = await tabNews.contents.getParent({
slug: '<slug>',
});
```

**Criar Conteúdo**

Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`,
Expand Down
64 changes: 64 additions & 0 deletions src/content/__snapshots__/content.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,68 @@ exports[`Content > get > should get content children 1`] = `
`;

exports[`Content > get > should get content children for current user 1`] = `
[
{
"body": "body",
"children": [
{
"body": "body",
"children": [],
"children_deep_count": 0,
"created_at": 2023-04-02T12:25:34.810Z,
"deleted_at": null,
"id": "id",
"owner_id": "owner_id",
"owner_username": "username",
"parent_id": "parent_id",
"published_at": 2023-04-02T12:25:34.865Z,
"slug": "slug",
"source_url": null,
"status": "published",
"tabcoins": 1,
"title": null,
"updated_at": 2023-04-02T12:25:34.810Z,
},
],
"children_deep_count": 1,
"created_at": 2023-04-02T12:25:34.810Z,
"deleted_at": null,
"id": "id",
"owner_id": "owner_id",
"owner_username": "username",
"parent_id": "parent_id",
"published_at": 2023-04-02T12:25:34.865Z,
"slug": "slug",
"source_url": null,
"status": "published",
"tabcoins": 0,
"title": null,
"updated_at": 2023-04-02T12:25:34.810Z,
},
]
`;

exports[`Content > get > should get parent content 1`] = `
{
"body": "body",
"children_deep_count": 2,
"created_at": 2023-09-19T12:16:04.812Z,
"deleted_at": null,
"id": "id",
"owner_id": "owner_id",
"owner_username": "username",
"parent_id": null,
"published_at": 2023-09-19T12:16:04.837Z,
"slug": "slug",
"source_url": "https://source.url.com/source",
"status": "published",
"tabcoins": 1,
"title": "title",
"updated_at": 2023-09-19T12:16:04.812Z,
}
`;

exports[`Content > get > should get parent content for current user 1`] = `
{
"body": "body",
"children_deep_count": 2,
Expand Down Expand Up @@ -305,6 +367,8 @@ exports[`Content > get > should throw an error when content not found 1`] = `"O

exports[`Content > get > should throw an error when content of content children not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;

exports[`Content > get > should throw an error when content of parent not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;

exports[`Content > get > should throw an error when parameter is invalid 1`] = `"\\"page\\" deve possuir um valor mínimo de 1."`;

exports[`Content > update > should throw a api erro when content not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;
Expand Down
71 changes: 69 additions & 2 deletions src/content/content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ describe('Content', () => {
);
};

const mockParentContent = (slug: string, user: string = username) => {
mockOnceResponse(
`${TABNEWS_ENDPOINTS.content}/${user}/${slug}/parent`,
contentDetail,
);
};

it('should get all contents and pagination', async () => {
mockContents(linkHeader);

Expand Down Expand Up @@ -325,9 +332,9 @@ describe('Content', () => {

mockOnceCurrentUser();

mockContent(slug);
mockContentChildren(slug);

const content = await tabNews.content.getBySlug({
const content = await tabNews.content.getChildren({
slug,
});

Expand Down Expand Up @@ -362,6 +369,66 @@ describe('Content', () => {
}),
).rejects.toThrowErrorMatchingSnapshot();
});

it('should get parent content', async () => {
const slug = 'slug';

mockParentContent(slug);

const content = await tabNews.content.getParent({
slug,
username,
});

expect(content).toMatchSnapshot();

const request = mockedRequest();

expectRequest(request).method.toBeGet();
});

it('should get parent content for current user', async () => {
const slug = 'slug';

mockOnceCurrentUser();

mockParentContent(slug);

const content = await tabNews.content.getParent({
slug,
});

expect(content).toMatchSnapshot();

const request = mockedRequest();

expectRequest(request).method.toBeGet();
});

it('should throw an error when content of parent not found', () => {
const slug = 'slug';

mockOnceApiError(
`${TABNEWS_ENDPOINTS.content}/${username}/${slug}/parent`,
{
name: 'NotFoundError',
message: 'O conteúdo informado não foi encontrado no sistema.',
action: 'Verifique se o "slug" está digitado corretamente.',
status_code: 404,
error_id: '3ea15e67-97c8-4671-916f-0344934c8300',
request_id: '11815650-d56e-4b90-97dd-dcdf23df8412',
error_location_code: 'CONTROLLER:CONTENT:GET_HANDLER:SLUG_NOT_FOUND',
key: 'slug',
},
);

expect(() =>
tabNews.content.getParent({
slug,
username,
}),
).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('create', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ export class Content {
return contentChildren;
}

async getParent(params: GetContentParams) {
const url = await this.getUrlForSlugAndUsername(params);

const { body: parentContent } =
await this.tabNews.get<ContentDetailResponse>({
path: `${url}/parent`,
});

return parentContent;
}

private async getUrlForSlugAndUsername({ slug, username }: GetContentParams) {
if (username) {
return `${TABNEWS_ENDPOINTS.content}/${username}/${slug}`;
Expand Down

0 comments on commit 504da8e

Please sign in to comment.