This repository has no description
1.5 kB
46 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { AddUrlToLibraryUseCase } from '../../../application/useCases/commands/AddUrlToLibraryUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6
7export class AddUrlToLibraryController extends Controller {
8 constructor(private addUrlToLibraryUseCase: AddUrlToLibraryUseCase) {
9 super();
10 }
11
12 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
13 try {
14 const { url, note, collectionIds, viaCardId } = req.body;
15 const curatorId = req.did;
16
17 if (!curatorId) {
18 return this.unauthorized(res);
19 }
20
21 if (!url) {
22 return this.badRequest(res, 'URL is required');
23 }
24
25 const result = await this.addUrlToLibraryUseCase.execute({
26 url,
27 note,
28 collectionIds: collectionIds || [],
29 curatorId,
30 viaCardId,
31 });
32
33 if (result.isErr()) {
34 // Check if the error is an authentication error
35 if (result.error instanceof AuthenticationError) {
36 return this.unauthorized(res, result.error.message);
37 }
38 return this.fail(res, result.error);
39 }
40
41 return this.ok(res, result.value);
42 } catch (error: any) {
43 return this.handleError(res, error);
44 }
45 }
46}