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