This repository has no description
1.4 kB
43 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { FollowTargetUseCase } from '../../../application/useCases/commands/FollowTargetUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6
7export class FollowTargetController extends Controller {
8 constructor(private followTargetUseCase: FollowTargetUseCase) {
9 super();
10 }
11
12 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
13 try {
14 const { targetId, targetType } = req.body;
15 const followerId = req.did;
16
17 if (!followerId) {
18 return this.unauthorized(res);
19 }
20
21 if (!targetId || !targetType) {
22 return this.badRequest(res, 'Target ID and type are required');
23 }
24
25 const result = await this.followTargetUseCase.execute({
26 followerId,
27 targetId,
28 targetType,
29 });
30
31 if (result.isErr()) {
32 if (result.error instanceof AuthenticationError) {
33 return this.unauthorized(res, result.error.message);
34 }
35 return this.fail(res, result.error);
36 }
37
38 return this.ok(res, result.value);
39 } catch (error: any) {
40 return this.handleError(res, error);
41 }
42 }
43}