-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathpeople.controller.ts
More file actions
96 lines (90 loc) · 2.59 KB
/
people.controller.ts
File metadata and controls
96 lines (90 loc) · 2.59 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 { Controller, Get, Post, Body, Query, Param, NotFoundException } from '@nestjs/common';
import { ApiQuery, ApiTags } from '@nestjs/swagger';
import { ZodResponse } from 'nestjs-zod';
import { CreatePersonFormDto, PersonDto, PersonListDto, PersonFilterDto, Person, GetPersonParams, GetPersonResponse } from './people.dto';
@ApiTags('People')
@Controller('api/people')
export class PeopleController {
private mockPeople: Person[] = [
{
id: 1,
name: 'Luke Skywalker',
height: '172',
mass: '77',
hairColor: 'blond',
skinColor: 'fair',
eyeColor: 'blue',
birthYear: '19BBY',
gender: 'male' as const,
homeworldId: '1',
filmIds: ['1', '2', '3', '6'],
speciesIds: [],
vehicleIds: ['14', '30'],
starshipIds: ['12', '22'],
created: new Date('2025-12-11T17:04:50.197Z')
},
{
id: 2,
name: 'C-3PO',
height: '167',
mass: '75',
hairColor: 'n/a',
skinColor: 'gold',
eyeColor: 'yellow',
birthYear: '112BBY',
gender: 'n/a' as const,
homeworldId: '1',
filmIds: ['1', '2', '3', '4', '5', '6'],
speciesIds: ['2'],
vehicleIds: [],
starshipIds: [],
created: new Date('2025-12-12T17:04:50.197Z')
},
{
id: 3,
name: 'R2-D2',
height: '96',
mass: '32',
hairColor: 'n/a',
skinColor: 'white, blue',
eyeColor: 'red',
birthYear: '33BBY',
gender: 'n/a' as const,
homeworldId: '8',
filmIds: ['1', '2', '3', '4', '5', '6'],
speciesIds: ['2'],
vehicleIds: [],
starshipIds: [],
created: new Date('2025-12-13T17:04:50.197Z')
},
];
@Get()
@ZodResponse({ type: PersonListDto, description: 'List of all people' })
getPeople(@Query() query: PersonFilterDto) {
return {
data: this.mockPeople.filter(person => query.filter?.name ? person.name.includes(query.filter.name) : true),
};
}
@Get(':id')
@ZodResponse({ type: GetPersonResponse, description: 'List of all people' })
getPerson(@Param() { id }: GetPersonParams) {
const person = this.mockPeople.find(person => person.id === id);
if (!person) {
throw new NotFoundException('Person not found');
}
return {
data: person,
};
}
@Post()
@ZodResponse({ type: PersonDto, description: 'Person created successfully' })
createPerson(@Body() createPersonDto: CreatePersonFormDto) {
const newPerson = {
...createPersonDto,
id: Math.floor(Math.random() * 1000) + 4,
created: new Date()
};
this.mockPeople.push(newPerson);
return newPerson;
}
}