Skip to content

Commit

Permalink
refactor: zod 타입 stringAsInt와 stringAsDate 활용
Browse files Browse the repository at this point in the history
  • Loading branch information
potados99 committed Feb 7, 2022
1 parent 418f3d6 commit 6797195
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 72 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"express": "^4.17.2",
"google-auth-library": "^7.11.0",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"mysql": "^2.14.1",
"reflect-metadata": "^0.1.10",
"serialize-error": "^8.1.0",
Expand All @@ -32,11 +33,11 @@
"zod": "^3.7.3"
},
"devDependencies": {
"@types/uuid-with-v6": "^1.1.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.0",
"@types/node": "^8.0.29",
"@types/uuid-with-v6": "^1.1.0",
"jest": "^27.4.5",
"ts-jest": "^27.1.2",
"ts-node": "3.3.0"
Expand Down
38 changes: 38 additions & 0 deletions src/server/libs/zodTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {z} from 'zod';

/**
* Express는 query와 params를 모두 string으로 줍니다.
*
* 따라서 만약 숫자로 된 인자를 받고 싶다면 ,
* refine()을 통해 숫자 스트링만 걸러낸 뒤에 transform()으로 숫자로 변환해야 합니다.
*/

export const stringAsInt = z.string().refine(isInt).transform(toInt);
export const stringAsBoolean = z.string().refine(isBoolean).transform(toBoolean);
export const stringAsDate = z.string().refine(isISOString).transform(toDate);

import moment from 'moment';

export function isInt(value: string): boolean {
return /^-?\d+$/.test(value);
}

export function toInt(value: string): number {
return parseInt(value, 10);
}

export function isBoolean(value: string): boolean {
return ['true', 'false'].includes(value);
}

export function toBoolean(value: string): boolean {
return value === 'true';
}

export function isISOString(value: string): boolean {
return moment(value, moment.ISO_8601, true).isValid();
}

export function toDate(value: string): Date {
return new Date(value);
}
4 changes: 2 additions & 2 deletions src/server/routes/comments/deleteComment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import CommentService from '../../../service/CommentService';
import {stringAsInt} from '../../libs/zodTypes';


const schema = defineSchema({
params: {
commentId: z.string(),
commentId: stringAsInt
},
});

Expand Down
26 changes: 10 additions & 16 deletions src/server/routes/comments/getComment.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import {getCustomRepository} from "typeorm";
import CommentRepository from "../../libs/application/comments/comment-repository";

import {stringAsInt} from '../../libs/zodTypes';
import CommentService from '../../../service/CommentService';

const schema = defineSchema({
params: {
commentId: z.string(),
},
params: {
commentId: stringAsInt,
},
});

export default defineRoute('get', '/comment/:commentId?', schema, async (req, res) => {
export default defineRoute('get', '/comment/:commentId', schema, async (req, res) => {
const {commentId} = req.params;

const {commentId} = req.params;
const Comment_Information = await getCustomRepository(CommentRepository).getComment(commentId);
console.log(Comment_Information)
// res.send(`comment ${commentId}의 id : ${Comment_Information.id} , content : ${Comment_Information.content} , createdAt : ${Comment_Information.createdAt}
// comment를 단 사용자 id :${Comment_Information.user.id} , email : ${Comment_Information.user.email} , nickname :${Comment_Information.user.nickname} , oauthProvider : ${Comment_Information.user.oauthProvider} , createAt :${Comment_Information.user.createdAt}
// comment가 달린 event id: ${Comment_Information.event.id} , host : ${Comment_Information.event.host} , category : ${Comment_Information.event.category} , ${Comment_Information.event.title} , body :${Comment_Information.event.body} , imageUuid: ${Comment_Information.event.imageUuid} , startAt : ${Comment_Information.event.startAt} , endAt : ${Comment_Information.event.endAt} , createAt : ${Comment_Information.event.createdAt}`);
const commentInformation = await CommentService.getComment(commentId);

return res.json(Comment_Information)
});
return res.json(commentInformation)
});
3 changes: 2 additions & 1 deletion src/server/routes/comments/makeComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {defineRoute} from '../../libs/route';
import CommentService from '../../../service/CommentService';
import UserService from '../../../service/UserService';
import EventService from '../../../service/EventService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
body: {
eventId: z.string(),
eventId: stringAsInt,
content: z.string(),
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/comments/updateComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import CommentService from '../../../service/CommentService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
commentId: z.string(),
commentId: stringAsInt,
},
query: {
content: z.string()
Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/events/deleteEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import EventService from '../../../service/EventService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
eventId: z.string(),
eventId: stringAsInt,
},
});

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/events/getEvent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import EventService from '../../../service/EventService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
eventId: z.string(),
eventId: stringAsInt,
},
});

Expand Down
43 changes: 22 additions & 21 deletions src/server/routes/events/makeEvent.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import {defineSchema} from '../../libs/schema';
import {string, z} from 'zod';
import MakeEvent from '../../../service/event/MakeEvent';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import {getGoogleOAuthInfo} from '../../../oauth';
import { getCustomRepository } from 'typeorm';
import ReturnUserInformation from '../../../service/ReturnUserInformation';
import EventService from '../../../service/EventService';
import UserService from '../../../service/UserService';
import {stringAsDate} from '../../libs/zodTypes';

const schema = defineSchema({
body: {
//일단 accessToken으로 받아옴
user: z.string(),
host:z.string(),
category: z.string(),
title: z.string(),
body: z.string(),
imageUuid: z.string().optional(),
startAt:z.date().optional(),
endAt:z.date().optional(),
}
body: {
host: z.string(),
category: z.string(),
title: z.string(),
body: z.string(),
imageUuid: z.string().optional(),
startAt: stringAsDate.optional(),
endAt: stringAsDate.optional(),
}
});

export default defineRoute('post', '/event', schema, async (req, res) => {
console.log('make Event!');
const {user,host,category,title, body, imageUuid, startAt, endAt } = req.body;
const Userstatus = await ReturnUserInformation.returnUser(user);
await MakeEvent.makeEvent(Userstatus,host,category,title, body, imageUuid, startAt, endAt);
res.sendStatus(201); //success
console.log('make Event!');

const {userId} = req;
const {host, category, title, body, imageUuid, startAt, endAt} = req.body;

const Userstatus = await UserService.getUser(userId)
await EventService.makeEvent(Userstatus, host, category, title, body, imageUuid, startAt, endAt);

res.sendStatus(201); //success
});


7 changes: 4 additions & 3 deletions src/server/routes/events/updateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import EventService from '../../../service/EventService';
import {stringAsDate, stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
eventId: z.string(),
eventId: stringAsInt,
},
query: {
host: z.string().optional(),
category: z.string().optional(),
title: z.string().optional(),
body: z.string().optional(),
imageUuid: z.string().optional(),
startAt: z.date().optional(),
endAt: z.date().optional(),
startAt: stringAsDate.optional(),
endAt: stringAsDate.optional(),
}
});

Expand Down
8 changes: 7 additions & 1 deletion src/server/routes/hello.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {defineRoute} from '../libs/route';
import {defineSchema} from '../libs/schema';
import {stringAsDate} from '../libs/zodTypes';

const schema = defineSchema({
body: {
aa: stringAsDate
}
});

export default defineRoute('get', '/hello', schema, async (req, res) => {
export default defineRoute('post', '/hello', schema, async (req, res) => {
console.log('요청옴!!!!!!!!!!!!!!!!!!!!!!!!!!!!');

console.log(req.body.aa);

return res.send('ㅎㅇㅎㅇ');
});
3 changes: 2 additions & 1 deletion src/server/routes/users/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import UserService from '../../../service/UserService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
id: z.string(),
id: stringAsInt,
},
});

Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/users/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import UserService from '../../../service/UserService';
import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
id: z.string(),
id: stringAsInt,
},
});

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/users/updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {defineSchema} from '../../libs/schema';
import {z} from 'zod';
import {defineRoute} from '../../libs/route';
import UserService from '../../../service/UserService';

import {stringAsInt} from '../../libs/zodTypes';

const schema = defineSchema({
params: {
id: z.string(),
id: stringAsInt,
},
query: {
nickname: z.string(),
Expand Down
14 changes: 8 additions & 6 deletions src/service/CommentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ class CommentService {
}).save();
}

async patchComment(commentId: string, req_query: Object): Promise<string> {
const commentIdStr2Num = parseInt(commentId)
async getComment(commentId: number): Promise<Comment | undefined> {
return await Comment.findOne(commentId);
}

async patchComment(commentId: number, req_query: Object): Promise<string> {
const patchevent = await Comment.update(
{id: commentIdStr2Num},
{id: commentId},
req_query
);
return patchevent.raw;
}

async deleteComment(commentId: string): Promise<string> {
const coomentIdStr2Num = parseInt(commentId)
async deleteComment(commentId: number): Promise<string> {
await Comment.delete({
id: coomentIdStr2Num
id: commentId
});
return;
}
Expand Down
19 changes: 6 additions & 13 deletions src/service/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ class EventService {
}).save();
}

async getEvent(eventId: string): Promise<Event> {
const eventIdStr2Num = parseInt(eventId)
return await Event.findOne({where: {id: eventIdStr2Num}});
async getEvent(eventId: number): Promise<Event> {
return await Event.findOne({where: {id: eventId}});
}

async patchEvent(eventId: string, req_query: Object): Promise<string> {
const eventIdStr2Num = parseInt(eventId)
async patchEvent(eventId: number, req_query: Object): Promise<string> {
const patchevent = await Event.update(
{
id: eventIdStr2Num
},
{id: eventId},
req_query
);
return patchevent.raw;
}

async deleteEvent(eventId: string): Promise<string> {
const eventIdStr2Num = parseInt(eventId)
await Event.delete({
id: eventIdStr2Num
});
async deleteEvent(id: number): Promise<string> {
await Event.delete({id});
return;
}
}
Expand Down

0 comments on commit 6797195

Please sign in to comment.