A social knowledge tool for researchers built on ATProto
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { AddCardToLibraryUseCase } from '../../../application/useCases/commands/AddCardToLibraryUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5
6export class AddCardToLibraryController extends Controller {
7 constructor(private addCardToLibraryUseCase: AddCardToLibraryUseCase) {
8 super();
9 }
10
11 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
12 try {
13 const { cardId, collectionIds } = req.body;
14 const curatorId = req.did;
15
16 if (!curatorId) {
17 return this.unauthorized(res);
18 }
19
20 if (!cardId) {
21 return this.badRequest(res, 'Card ID is required');
22 }
23
24 // collectionIds is optional, but if provided should be an array
25 if (collectionIds !== undefined && !Array.isArray(collectionIds)) {
26 return this.badRequest(res, 'Collection IDs must be an array');
27 }
28
29 const result = await this.addCardToLibraryUseCase.execute({
30 cardId,
31 collectionIds,
32 curatorId,
33 });
34
35 if (result.isErr()) {
36 return this.fail(res, result.error);
37 }
38
39 return this.ok(res, result.value);
40 } catch (error: any) {
41 return this.fail(res, error);
42 }
43 }
44}