-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
96 lines (94 loc) · 3.33 KB
/
route.ts
File metadata and controls
96 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'reflect-metadata';
import type { NextRequest, NextResponse } from 'next/server';
import { container } from 'tsyringe';
import { handleApiRequest } from '@api/helpers/api/handle-api-request';
import type { VaultModelDto } from '@shared/dto/models/vault.model.dto';
import { CreateVaultUseCase } from '@api/usecases/vaults/create-vault.usecase';
import { GetMyVaultsUseCase } from '@api/usecases/vaults/get-my-vaults.usecase';
import { StatusCodes } from 'http-status-codes';
import type { CreateVaultDataDto } from '@shared/dto/output/data/create-vault.data.dto';
import type { GetMyVaultsDataDto } from '@shared/dto/output/data/get-my-vaults.data.dto';
import type { HttpResponseDto } from '@shared/dto/output/responses/abstract/http.response.dto';
import type { CreateVaultPayloadDto } from '@shared/dto/input/payloads/create-vault.payload.dto';
/**
* @swagger
* /api/vaults:
* get:
* tags: [Vaults]
* summary: Get my vaults
* responses:
* 200:
* description: Returns my vaults
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/GetMyVaultsBodyDto'
* 500:
* description: Internal Server Error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/HttpErrorDto'
*/
export async function GET(): Promise<
NextResponse<HttpResponseDto<GetMyVaultsDataDto>>
> {
const getMyVaultsUseCase: GetMyVaultsUseCase =
container.resolve(GetMyVaultsUseCase);
return await handleApiRequest<GetMyVaultsDataDto>(async () => {
const myVaults: VaultModelDto[] = await getMyVaultsUseCase.handle();
const response: GetMyVaultsDataDto = { myVaults };
return response;
});
}
/**
* @swagger
* /api/vaults:
* post:
* tags: [Vaults]
* summary: Create a vault
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateVaultPayloadDto'
* responses:
* 201:
* description: Returns the vault created
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateVaultBodyDto'
* 409:
* description: Vault already exists
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BusinessErrorDto'
* 422:
* description: The vault label must not exceed 255 characters
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BusinessErrorDto'
* 500:
* description: Internal Server Error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/HttpErrorDto'
*/
export async function POST(
request: NextRequest
): Promise<NextResponse<HttpResponseDto<CreateVaultDataDto>>> {
const payload: CreateVaultPayloadDto = await request.json();
const createVaultUseCase: CreateVaultUseCase =
container.resolve(CreateVaultUseCase);
return await handleApiRequest<CreateVaultDataDto>(async () => {
const vaultCreated: VaultModelDto =
await createVaultUseCase.handle(payload);
const response: CreateVaultDataDto = { vaultCreated };
return response;
}, StatusCodes.CREATED);
}