Skip to content

Commit 3fd6417

Browse files
committed
기존 샘플코드 앞에 "Sample" 접두어 붙여줌
1 parent 00ea930 commit 3fd6417

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1042
-1036
lines changed

src/api/controllers/PetController.ts

-81
This file was deleted.
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { IsNotEmpty, IsNumber, IsUUID, ValidateNested } from 'class-validator';
2+
import {
3+
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put,
4+
} from 'routing-controllers';
5+
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
6+
7+
import { SamplePetNotFoundError } from '../errors/SamplePetNotFoundError';
8+
import { SamplePet } from '../models/SamplePet';
9+
import { SamplePetService } from '../services/SamplePetService';
10+
import { SampleUserResponse } from './SampleUserController';
11+
12+
class BasePet {
13+
@IsNotEmpty()
14+
public name: string;
15+
16+
@IsNumber()
17+
public age: number;
18+
}
19+
20+
export class SamplePetResponse extends BasePet {
21+
@IsUUID()
22+
public id: string;
23+
24+
@ValidateNested()
25+
public user: SampleUserResponse;
26+
}
27+
28+
class CreatePetBody extends BasePet {
29+
@IsUUID()
30+
public userId: string;
31+
}
32+
33+
@Authorized()
34+
@JsonController('/samplePets')
35+
@OpenAPI({ security: [{ basicAuth: [] }] })
36+
export class SamplePetController {
37+
38+
constructor(
39+
private petService: SamplePetService,
40+
) {
41+
}
42+
43+
@Get()
44+
@ResponseSchema(SamplePetResponse, { isArray: true })
45+
public find(): Promise<SamplePet[]> {
46+
return this.petService.find();
47+
}
48+
49+
@Get('/:id')
50+
@OnUndefined(SamplePetNotFoundError)
51+
@ResponseSchema(SamplePetResponse)
52+
public one(@Param('id') id: string): Promise<SamplePet | undefined> {
53+
return this.petService.findOne(id);
54+
}
55+
56+
@Post()
57+
@ResponseSchema(SamplePetResponse)
58+
public create(@Body({ required: true }) body: CreatePetBody): Promise<SamplePet> {
59+
const pet = new SamplePet();
60+
pet.age = body.age;
61+
pet.name = body.name;
62+
pet.userId = body.userId;
63+
64+
return this.petService.create(pet);
65+
}
66+
67+
@Put('/:id')
68+
@ResponseSchema(SamplePetResponse)
69+
public update(@Param('id') id: string, @Body() body: BasePet): Promise<SamplePet> {
70+
const pet = new SamplePet();
71+
pet.age = body.age;
72+
pet.name = body.name;
73+
74+
return this.petService.update(id, pet);
75+
}
76+
77+
@Delete('/:id')
78+
public delete(@Param('id') id: string): Promise<void> {
79+
return this.petService.delete(id);
80+
}
81+
82+
}
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Type } from 'class-transformer';
2+
import { IsEmail, IsNotEmpty, IsUUID, ValidateNested } from 'class-validator';
3+
import {
4+
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Req,
5+
} from 'routing-controllers';
6+
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
7+
8+
import { SampleUserNotFoundError } from '../errors/SampleUserNotFoundError';
9+
import { SampleUser } from '../models/SampleUser';
10+
import { SampleUserService } from '../services/SampleUserService';
11+
import { SamplePetResponse } from './SamplePetController';
12+
13+
class SampleBaseUser {
14+
@IsNotEmpty()
15+
public firstName: string;
16+
17+
@IsNotEmpty()
18+
public lastName: string;
19+
20+
@IsEmail()
21+
@IsNotEmpty()
22+
public email: string;
23+
24+
@IsNotEmpty()
25+
public username: string;
26+
}
27+
28+
export class SampleUserResponse extends SampleBaseUser {
29+
@IsUUID()
30+
public id: string;
31+
32+
@ValidateNested({ each: true })
33+
@Type(() => SamplePetResponse)
34+
public pets: SamplePetResponse[];
35+
}
36+
37+
class SampleCreateUserBody extends SampleBaseUser {
38+
@IsNotEmpty()
39+
public password: string;
40+
}
41+
42+
@Authorized()
43+
@JsonController('/sampleUsers')
44+
@OpenAPI({ security: [{ basicAuth: [] }] })
45+
export class SampleUserController {
46+
47+
constructor(
48+
private userService: SampleUserService,
49+
) {
50+
}
51+
52+
@Get()
53+
@ResponseSchema(SampleUserResponse, { isArray: true })
54+
public find(): Promise<SampleUser[]> {
55+
return this.userService.find();
56+
}
57+
58+
@Get('/me')
59+
@ResponseSchema(SampleUserResponse, { isArray: true })
60+
public findMe(@Req() req: any): Promise<SampleUser[]> {
61+
return req.user;
62+
}
63+
64+
@Get('/:id')
65+
@OnUndefined(SampleUserNotFoundError)
66+
@ResponseSchema(SampleUserResponse)
67+
public one(@Param('id') id: string): Promise<SampleUser | undefined> {
68+
return this.userService.findOne(id);
69+
}
70+
71+
@Post()
72+
@ResponseSchema(SampleUserResponse)
73+
public create(@Body() body: SampleCreateUserBody): Promise<SampleUser> {
74+
const user = new SampleUser();
75+
user.email = body.email;
76+
user.firstName = body.firstName;
77+
user.lastName = body.lastName;
78+
user.password = body.password;
79+
user.username = body.username;
80+
81+
return this.userService.create(user);
82+
}
83+
84+
@Put('/:id')
85+
@ResponseSchema(SampleUserResponse)
86+
public update(@Param('id') id: string, @Body() body: SampleBaseUser): Promise<SampleUser> {
87+
const user = new SampleUser();
88+
user.email = body.email;
89+
user.firstName = body.firstName;
90+
user.lastName = body.lastName;
91+
user.username = body.username;
92+
93+
return this.userService.update(id, user);
94+
}
95+
96+
@Delete('/:id')
97+
public delete(@Param('id') id: string): Promise<void> {
98+
return this.userService.delete(id);
99+
}
100+
101+
}

src/api/controllers/UserController.ts

-100
This file was deleted.

src/api/errors/PetNotFoundError.ts

-7
This file was deleted.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { HttpError } from 'routing-controllers';
2+
3+
export class SamplePetNotFoundError extends HttpError {
4+
constructor() {
5+
super(404, 'SamplePet not found!');
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { HttpError } from 'routing-controllers';
2+
3+
export class SampleUserNotFoundError extends HttpError {
4+
constructor() {
5+
super(404, 'SampleUser not found!');
6+
}
7+
}

src/api/errors/UserNotFoundError.ts

-7
This file was deleted.

0 commit comments

Comments
 (0)