Skip to content

Commit

Permalink
Merge pull request #32 from leoferreiralima/feat/get-content-children
Browse files Browse the repository at this point in the history
feat: add content children integration
  • Loading branch information
leoferreiralima authored Sep 27, 2023
2 parents 44b36ec + 0837d32 commit 1856e9f
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 22 deletions.
71 changes: 49 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,16 @@ const { pagination, contents } = await tabNews.contents.getMy({
});
```

**Criar Conteúdo**

Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`,
que é o seu post ou comentario, e o `title` que é opcional apenas quando há um `parent_id`
**Buscar Detalhe do Conteúdo**

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

const tabNews = new TabNews();

await tabNews.session.create();

const response = await tabNews.contents.create({
parent_id: undefined,
slug: 'e-opcional',
title: 'test',
body: 'test',
status: 'published',
source_url: 'https://google.com',
const content = await tabNews.contents.getBySlug({
slug: '<slug>',
username: '<username>',
});
```

Expand All @@ -240,25 +231,23 @@ import { TabNews } from 'tabnews-sdk';

const tabNews = new TabNews();

const { contents } = await tabNews.contents.getAll();

await tabNews.session.create();

const response = await tabNews.contents.create({
parent_id: contents[0].id,
body: 'comentando em um conteúdo',
status: 'published',
// Não é preciso passar o username pois internamente a bliblioteca ira realizar o fecth do usuario atual

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

**Buscar Detalhe do Conteúdo**
**Buscar Comentarios do Conteúdo**

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

const tabNews = new TabNews();

const content = await tabNews.contents.getBySlug({
const contentChildren = await tabNews.contents.getChildren({
slug: '<slug>',
username: '<username>',
});
Expand All @@ -273,11 +262,49 @@ await tabNews.session.create();

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

const content = await tabNews.contents.getBySlug({
const contentChildren = await tabNews.contents.getChildren({
slug: '<slug>',
});
```

**Criar Conteúdo**

Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`,
que é o seu post ou comentario, e o `title` que é opcional apenas quando há um `parent_id`

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

const tabNews = new TabNews();

await tabNews.session.create();

const response = await tabNews.contents.create({
parent_id: undefined,
slug: 'e-opcional',
title: 'test',
body: 'test',
status: 'published',
source_url: 'https://google.com',
});
```

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

const tabNews = new TabNews();

const { contents } = await tabNews.contents.getAll();

await tabNews.session.create();

const response = await tabNews.contents.create({
parent_id: contents[0].id,
body: 'comentando em um conteúdo',
status: 'published',
});
```

**Atualizar Conteúdo**

```js
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 @@ -205,6 +205,68 @@ exports[`Content > get > should get content by slug for current user 1`] = `
}
`;

exports[`Content > get > should get content children 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 content children for current user 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 return correct page when is last page 1`] = `
{
"first_page": 1,
Expand Down Expand Up @@ -241,6 +303,8 @@ exports[`Content > get > should return correct page when is last page 2`] = `

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

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 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
107 changes: 107 additions & 0 deletions src/content/content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,46 @@ const contentDetail = {
body: 'body',
};

const contentChildren = [
{
id: 'id',
parent_id: 'parent_id',
owner_id: 'owner_id',
slug: 'slug',
title: null,
body: 'body',
status: 'published',
source_url: null,
published_at: '2023-04-02T12:25:34.865Z',
created_at: '2023-04-02T12:25:34.810Z',
updated_at: '2023-04-02T12:25:34.810Z',
deleted_at: null,
owner_username: 'username',
tabcoins: 0,
children: [
{
id: 'id',
parent_id: 'parent_id',
owner_id: 'owner_id',
slug: 'slug',
title: null,
body: 'body',
status: 'published',
source_url: null,
published_at: '2023-04-02T12:25:34.865Z',
created_at: '2023-04-02T12:25:34.810Z',
updated_at: '2023-04-02T12:25:34.810Z',
deleted_at: null,
owner_username: 'username',
tabcoins: 1,
children: [],
children_deep_count: 0,
},
],
children_deep_count: 1,
},
];

describe('Content', () => {
const mockContent = (slug: string, user: string = username) => {
mockOnceResponse(
Expand Down Expand Up @@ -85,6 +125,13 @@ describe('Content', () => {
);
};

const mockContentChildren = (slug: string, user: string = username) => {
mockOnceResponse(
`${TABNEWS_ENDPOINTS.content}/${user}/${slug}/children`,
contentChildren,
);
};

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

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

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

mockContentChildren(slug);

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

expect(content).toMatchSnapshot();

const request = mockedRequest();

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

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

mockOnceCurrentUser();

mockContent(slug);

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

expect(content).toMatchSnapshot();

const request = mockedRequest();

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

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

mockOnceApiError(
`${TABNEWS_ENDPOINTS.content}/${username}/${slug}/children`,
{
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.getChildren({
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 @@ -104,6 +104,17 @@ export class Content {
return content;
}

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

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

return contentChildren;
}

private async getUrlForSlugAndUsername({ slug, username }: GetContentParams) {
if (username) {
return `${TABNEWS_ENDPOINTS.content}/${username}/${slug}`;
Expand Down
4 changes: 4 additions & 0 deletions src/content/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ export type UpdateContent = Partial<
status?: ContentStatus;
username?: string;
};

export interface ContentChildrenResponse extends ContentDetailResponse {
children: ContentChildrenResponse[];
}

0 comments on commit 1856e9f

Please sign in to comment.