Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 175 - post twitter #182

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/features/twitter/controllers/twitter-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import type { TokenPayload } from '@/shared/infra/jwt/jwt';
import type { Controller } from '@/shared/protocols/controller';
import type { AsyncRequestHandler } from '@/shared/protocols/handlers';
import { HttpStatusCode } from '@/shared/protocols/http-client';

import { generateAuthURL } from '../helpers/generate-auth-url';
import type { AuthorizeTwitterService } from '../services/authorize-twitter-service';
import type { PostTwitterService } from '../services/post-twitter-service';
Comment on lines 8 to +10
Copy link
Contributor

@melo-zip melo-zip Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { generateAuthURL } from '../helpers/generate-auth-url';
import type { AuthorizeTwitterService } from '../services/authorize-twitter-service';
import type { PostTwitterService } from '../services/post-twitter-service';
import { generateAuthURL } from '@/features/twitter/helpers/generate-auth-url';
import type { AuthorizeTwitterService } from '@/features/twitter/services/authorize-twitter-service';
import type { PostTwitterService } from '@/features/twitter/services/post-twitter-service';


export class TwitterController implements Controller {
callback: AsyncRequestHandler = async (req, res) => {
Expand Down Expand Up @@ -35,5 +37,18 @@
return res.json(url);
};

constructor(private readonly authorizeTwitter: AuthorizeTwitterService) {}
tweet: AsyncRequestHandler = (req, res) => {
const authorization = req.headers.authorization; // token bearer user

Check warning on line 41 in src/features/twitter/controllers/twitter-controller.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/features/twitter/controllers/twitter-controller.ts#L41

[@typescript-eslint/no-unused-vars] 'authorization' is assigned a value but never used. Allowed unused vars must match /^_/u.

const { file, text } = req.body; // talvez tenha a localização também

const tweet = this.postTwitterService.execute({ file, text });

Check warning on line 45 in src/features/twitter/controllers/twitter-controller.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/features/twitter/controllers/twitter-controller.ts#L45

[@typescript-eslint/no-unused-vars] 'tweet' is assigned a value but never used. Allowed unused vars must match /^_/u.

return res.status(HttpStatusCode.ok).json();
};

constructor(
private readonly authorizeTwitter: AuthorizeTwitterService,
private readonly postTwitterService: PostTwitterService
) {}
}
5 changes: 5 additions & 0 deletions src/features/twitter/models/twitter-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export type TwitterTokenResponse = {
token_type: 'bearer';
};

export type TwitterPostResponse = {
file?: File;
text?: string;
};

export type TwitterUser = {
id: string;
name: string;
Expand Down
11 changes: 10 additions & 1 deletion src/features/twitter/routes/twitter-controller.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Logger } from '@/shared/infra/logger/logger';

import { TwitterController } from '../controllers/twitter-controller';
import { AuthorizeTwitterService } from '../services/authorize-twitter-service';
import { PostTwitterService } from '../services/post-twitter-service';
import { TwitterService } from '../services/twitter-service';
Comment on lines 7 to 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { TwitterController } from '../controllers/twitter-controller';
import { AuthorizeTwitterService } from '../services/authorize-twitter-service';
import { PostTwitterService } from '../services/post-twitter-service';
import { TwitterService } from '../services/twitter-service';
import { TwitterController } from '@/features/twitter/controllers/twitter-controller';
import { AuthorizeTwitterService } from '@/features/twitter/services/authorize-twitter-service';
import { PostTwitterService } from '@/features/twitter/services/post-twitter-service';
import { TwitterService } from '@/features/twitter/services/twitter-service';


export function twitterControllerFactory() {
Expand All @@ -28,7 +29,15 @@ export function twitterControllerFactory() {
tokenRepository
);

const twitterController = new TwitterController(authorizeTwitterService);
const postTwitterService = new PostTwitterService(
new Logger({ service: 'postTwitterService' }),
axiosAdapter
);

const twitterController = new TwitterController(
authorizeTwitterService,
postTwitterService
);

return {
twitterController,
Expand Down
1 change: 1 addition & 0 deletions src/features/twitter/routes/twitter-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { twitterController } = twitterControllerFactory();

router.get('/login', twitterController.login);
router.get('/callback', twitterController.callback);
router.post('/tweets', twitterController.tweet);

export default {
prefix: 'twitter',
Expand Down
34 changes: 34 additions & 0 deletions src/features/twitter/services/post-twitter-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { HttpAdapter } from '@/shared/infra/http/http-adapter';
import type { Logger } from '@/shared/infra/logger/logger';
import type { Service } from '@/shared/protocols/service';

import type { TwitterPostResponse } from '../models/twitter-models';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import type { TwitterPostResponse } from '../models/twitter-models';
import type { TwitterPostResponse } from '@/features/twitter/models/twitter-models';


type Input = {
file?: File;
text?: string;
};

export class PostTwitterService implements Service<Input, void> {
constructor(
private readonly logger: Logger,
private readonly http: HttpAdapter
) {}

async execute({ file, text }) {
try {
const { data } = await this.http.post<TwitterPostResponse>({
data: {
file,
text,
},
url: '/2/tweets',
});

return data.text;
} catch (err) {
this.logger.error(`Error on postTwitter in twitter service -${err}`);
throw err;
}
}
}
Loading