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