Skip to content

Commit

Permalink
update: 코드를 일관성있게 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
JoStar33 committed Aug 10, 2024
1 parent 077906f commit 07ddbad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/mocks/auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { delay, http } from 'msw';
import { ISignInRequest, ISignUpRequest } from '@/types/auth';
import { commonUrl } from '.';
import userDatabase from './fakeDatabase/userDatabase';
import userDatabase from './fakeDatabase/resources/user';

const authUrl = (path?: string) => `${commonUrl(`/auth${path}`)}`;

const authHandler = [
http.post(`${authUrl('/sign-in')}`, async ({ request }) => {
const data = (await request.json()) as ISignInRequest;
const userDetail = userDatabase.GET.detail({ key: 'email', value: data.email });
const userDetail = userDatabase.Get.detail({ key: 'email', value: data.email });
if (userDetail.code === 400)
return new Response(
JSON.stringify({
Expand Down Expand Up @@ -75,7 +75,7 @@ const authHandler = [
}),
http.post(`${authUrl('/sign-up')}`, async ({ request }) => {
const data = (await request.json()) as ISignUpRequest;
const userDetail = userDatabase.GET.detail({ key: 'email', value: data.email });
const userDetail = userDatabase.Get.detail({ key: 'email', value: data.email });

if (userDetail.code === 200) {
return new Response(
Expand All @@ -93,7 +93,7 @@ const authHandler = [
);
}

const createdInfo = userDatabase.CREATE.user(data);
const createdInfo = userDatabase.Create.user(data);

if (createdInfo.code !== 200) {
return new Response(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
import { ISignUpRequest } from '@/types/auth';
import { IDatabaseUser } from '../types/auth';
import { IValueResponseReturn } from '../types';
import { IDatabaseUser } from '../../types/auth';

const userDatabase = {
GET: {
Get: {
list: () => {
const localStorageUserList = localStorage.getItem('userList') ?? '';
const parsedUserList: IDatabaseUser[] = localStorageUserList ? JSON.parse(localStorageUserList) : [];
return {
code: 200,
value: parsedUserList,
message: '성공!',
message: '회원목록 조회 성공',
};
},
detail: ({ key, value }: { key: keyof IDatabaseUser; value: string }) => {
try {
const userList = (userDatabase.GET.list() as IValueResponseReturn<IDatabaseUser[]>).value;
const userList = userDatabase.Get.list().value;
const findUser = userList.find((element) => element[key] === value);
if (!findUser) {
return {
code: 400,
message: '회원정보가 존재하지 않습니다!',
message: '회원정보가 없음.',
};
}
return {
code: 200,
message: '성공!',
message: '회원 상세정보 조회 성공.',
value: findUser,
};
} catch (error: any) {
console.log(error);
return {
code: 500,
message: error,
message: '사용자 상세정보 조회중 에러발생.',
};
}
},
},
CREATE: {
Create: {
user: (request: ISignUpRequest) => {
try {
const userList = (userDatabase.GET.list() as IValueResponseReturn<IDatabaseUser[]>).value;
const userList = userDatabase.Get.list().value;
const lastUserId = userList[userList.length] ? userList[userList.length].id : 0;
localStorage.setItem('userList', JSON.stringify([...userList, { id: lastUserId + 1, ...request }]));
return {
code: 200,
message: '회원가입 성공!',
message: '사용자 등록 성공.',
};
} catch (error: any) {
console.log(error);
return {
code: 500,
message: error,
message: '사용자 정보 등록중 에러발생',
};
}
},
},
UPDATE: {},
DELETE: {},
Update: {},
Delete: {},
};

export default userDatabase;

0 comments on commit 07ddbad

Please sign in to comment.