This repository has no description
semble
/
src
/
modules
/
cards
/
infrastructure
/
http
/
controllers
/
RemoveCardFromCollectionController.ts
2.0 kB
62 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { RemoveCardFromCollectionUseCase } from '../../../application/useCases/commands/RemoveCardFromCollectionUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6
7export class RemoveCardFromCollectionController extends Controller {
8 constructor(
9 private removeCardFromCollectionUseCase: RemoveCardFromCollectionUseCase,
10 ) {
11 super();
12 }
13
14 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
15 try {
16 const { cardId } = req.params;
17 const { collectionIds: collectionIdsParam } = req.query;
18 const curatorId = req.did;
19
20 if (!curatorId) {
21 return this.unauthorized(res);
22 }
23
24 if (!cardId) {
25 return this.badRequest(res, 'Card ID is required');
26 }
27
28 if (!collectionIdsParam || typeof collectionIdsParam !== 'string') {
29 return this.badRequest(
30 res,
31 'Collection IDs query parameter is required',
32 );
33 }
34
35 const collectionIds = collectionIdsParam
36 .split(',')
37 .filter((id) => id.trim() !== '');
38
39 if (collectionIds.length === 0) {
40 return this.badRequest(res, 'At least one collection ID is required');
41 }
42
43 const result = await this.removeCardFromCollectionUseCase.execute({
44 cardId,
45 collectionIds,
46 curatorId,
47 });
48
49 if (result.isErr()) {
50 // Check if the error is an authentication error
51 if (result.error instanceof AuthenticationError) {
52 return this.unauthorized(res, result.error.message);
53 }
54 return this.fail(res, result.error);
55 }
56
57 return this.ok(res, result.value);
58 } catch (error: any) {
59 return this.handleError(res, error);
60 }
61 }
62}