Skip to content

Commit a3c187a

Browse files
committed
feat: add get one host endpoint and related contract updates
- Implement GET_ONE route for hosts in contract routes - Add new GET_ONE route constant in hosts routes - Create get-one command and related DTOs/models - Add new error constant for get one host error - Implement getOneHost method in hosts service and controller
1 parent f557793 commit a3c187a

File tree

14 files changed

+113
-5
lines changed

14 files changed

+113
-5
lines changed

libs/contract/api/controllers/hosts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export const HOSTS_ROUTES = {
77
UPDATE: 'update',
88
UPDATE_MANY: 'many',
99
REORDER: 'reorder',
10+
GET_ONE: 'get-one',
1011
} as const;

libs/contract/api/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export const REST_API = {
9090
REORDER: `${ROOT}/${CONTROLLERS.HOSTS_CONTROLLER}/${CONTROLLERS.HOSTS_ROUTES.REORDER}`,
9191
DELETE: (uuid: string) =>
9292
`${ROOT}/${CONTROLLERS.HOSTS_CONTROLLER}/${CONTROLLERS.HOSTS_ROUTES.DELETE}/${uuid}`,
93+
GET_ONE: (uuid: string) =>
94+
`${ROOT}/${CONTROLLERS.HOSTS_CONTROLLER}/${CONTROLLERS.HOSTS_ROUTES.GET_ONE}/${uuid}`,
9395
},
9496
SYSTEM: {
9597
STATS: `${ROOT}/${CONTROLLERS.SYSTEM_CONTROLLER}/${CONTROLLERS.SYSTEM_ROUTES.STATS}`,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { z } from 'zod';
2+
3+
import { REST_API } from '../../api';
4+
import { HostsSchema } from '../..';
5+
6+
export namespace GetOneHostCommand {
7+
export const url = REST_API.HOSTS.GET_ONE;
8+
export const TSQ_url = url(':uuid');
9+
10+
export const RequestSchema = z.object({
11+
uuid: z.string().uuid(),
12+
});
13+
14+
export type Request = z.infer<typeof RequestSchema>;
15+
16+
export const ResponseSchema = z.object({
17+
response: HostsSchema,
18+
});
19+
20+
export type Response = z.infer<typeof ResponseSchema>;
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './create.command';
22
export * from './delete.command';
33
export * from './get-all.command';
4+
export * from './get-one.command';
45
export * from './reorder.command';
56
export * from './update.command';

libs/contract/constants/errors/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,9 @@ export const ERRORS = {
338338
message: 'Disable node error',
339339
httpCode: 500,
340340
},
341+
GET_ONE_HOST_ERROR: {
342+
code: 'A070',
343+
message: 'Get one host error',
344+
httpCode: 500,
345+
},
341346
} as const;

libs/contract/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@remnawave/backend-contract",
3-
"version": "0.3.7",
3+
"version": "0.3.8",
44
"public": true,
55
"license": "AGPL-3.0-only",
66
"description": "A contract library for Remnawave Backend. It can be used in backend and frontend.",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createZodDto } from 'nestjs-zod';
2+
3+
import { GetOneHostCommand } from '@libs/contracts/commands';
4+
5+
export class GetOneHostRequestDto extends createZodDto(GetOneHostCommand.RequestSchema) {}
6+
export class GetOneHostResponseDto extends createZodDto(GetOneHostCommand.ResponseSchema) {}

src/modules/hosts/dtos/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './create-host.dto';
22
export * from './delete-host.dto';
33
export * from './get-all-hosts.dto';
4+
export * from './get-one.dto';
45
export * from './reorder-hots.dto';
56
export * from './update-host.dto';

src/modules/hosts/hosts.controller.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ import { ReorderHostRequestDto, ReorderHostResponseDto } from './dtos/reorder-ho
3232
import { CreateHostRequestDto, CreateHostResponseDto } from './dtos/create-host.dto';
3333
import { DeleteHostRequestDto, DeleteHostResponseDto } from './dtos/delete-host.dto';
3434
import { GetAllHostsResponseModel } from './models/get-all-hosts.response.model';
35+
import { GetOneHostResponseModel } from './models/get-one-host.response.model';
3536
import { CreateHostResponseModel } from './models/create-host.response.model';
3637
import { UpdateHostResponseModel } from './models/update-host.response.model';
3738
import { GetAllHostsResponseDto } from './dtos/get-all-hosts.dto';
3839
import { UpdateHostResponseDto } from './dtos/update-host.dto';
3940
import { UpdateHostRequestDto } from './dtos/update-host.dto';
41+
import { GetOneHostResponseDto } from './dtos/get-one.dto';
42+
import { GetOneHostRequestDto } from './dtos/get-one.dto';
4043
import { HostsService } from './hosts.service';
4144

4245
@ApiBearerAuth('Authorization')
@@ -98,6 +101,23 @@ export class HostsController {
98101
};
99102
}
100103

104+
@ApiOkResponse({
105+
type: GetOneHostResponseDto,
106+
description: 'Host fetched successfully',
107+
})
108+
@ApiOperation({ summary: 'Get One Host', description: 'Get one host by uuid' })
109+
@ApiParam({ name: 'uuid', type: String, description: 'UUID of the host', required: true })
110+
@HttpCode(HttpStatus.OK)
111+
@Get(HOSTS_ROUTES.GET_ONE + '/:uuid')
112+
async getOneHost(@Param() paramData: GetOneHostRequestDto): Promise<GetOneHostResponseDto> {
113+
const result = await this.hostsService.getOneHost(paramData.uuid);
114+
115+
const data = errorHandler(result);
116+
return {
117+
response: new GetOneHostResponseModel(data),
118+
};
119+
}
120+
101121
@ApiBody({ type: ReorderHostRequestDto })
102122
@ApiOkResponse({
103123
type: ReorderHostResponseDto,

src/modules/hosts/hosts.service.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ export class HostsService {
122122
}
123123
}
124124

125+
public async getOneHost(hostUuid: string): Promise<ICommandResponse<HostsEntity>> {
126+
try {
127+
const result = await this.hostsRepository.findByUUID(hostUuid);
128+
129+
if (!result) {
130+
return {
131+
isOk: false,
132+
...ERRORS.HOST_NOT_FOUND,
133+
};
134+
}
135+
136+
return {
137+
isOk: true,
138+
response: result,
139+
};
140+
} catch (error) {
141+
this.logger.error(error);
142+
return { isOk: false, ...ERRORS.GET_ONE_HOST_ERROR };
143+
}
144+
}
145+
125146
public async reorderHosts(dto: ReorderHostRequestDto): Promise<
126147
ICommandResponse<{
127148
isUpdated: boolean;

0 commit comments

Comments
 (0)