This repository has no description
semble
/
src
/
modules
/
cards
/
infrastructure
/
http
/
controllers
/
UpdateUrlCardAssociationsController.ts
1.6 kB
49 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { UpdateUrlCardAssociationsUseCase } from '../../../application/useCases/commands/UpdateUrlCardAssociationsUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6
7export class UpdateUrlCardAssociationsController extends Controller {
8 constructor(
9 private updateUrlCardAssociationsUseCase: UpdateUrlCardAssociationsUseCase,
10 ) {
11 super();
12 }
13
14 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
15 try {
16 const { cardId, note, addToCollections, removeFromCollections } =
17 req.body;
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 const result = await this.updateUrlCardAssociationsUseCase.execute({
29 cardId,
30 curatorId,
31 note,
32 addToCollections,
33 removeFromCollections,
34 });
35
36 if (result.isErr()) {
37 // Check if the error is an authentication error
38 if (result.error instanceof AuthenticationError) {
39 return this.unauthorized(res, result.error.message);
40 }
41 return this.fail(res, result.error);
42 }
43
44 return this.ok(res, result.value);
45 } catch (error: any) {
46 return this.handleError(res, error);
47 }
48 }
49}