Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reviews impl.ts을 역할에 따라 분리 #602

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
bookInfoNotFound,
getUser,
reviewNotFound,
} from '../shared';
import { ReviewsService } from './service';
import { ReviewNotFoundError } from './service/errors';
} from '../../shared';
import { ReviewsService } from '../service';
import { ReviewNotFoundError } from '../service/errors';

type PostDeps = Pick<ReviewsService, 'createReview'>;
type MkPost = (services: PostDeps) => HandlerFor<typeof contract.reviews.post>;
Expand All @@ -21,11 +21,15 @@ export const mkPostReviews: MkPost =

return match(result)
.with(P.instanceOf(BookInfoNotFoundError), () => bookInfoNotFound)
.otherwise(() => ({ status: 201, body: '리뷰가 작성되었습니다.' } as const));
.otherwise(
() => ({ status: 201, body: '리뷰가 작성되었습니다.' } as const),
);
};

type PatchDeps = Pick<ReviewsService, 'toggleReviewVisibility'>;
type MkPatch = (services: PatchDeps) => HandlerFor<typeof contract.reviews.patch>;
type MkPatch = (
services: PatchDeps,
) => HandlerFor<typeof contract.reviews.patch>;
export const mkPatchReviews: MkPatch =
({ toggleReviewVisibility }) =>
async ({ params: { reviewsId }, req: { user } }) => {
Expand All @@ -34,7 +38,13 @@ export const mkPatchReviews: MkPatch =

return match(result)
.with(P.instanceOf(ReviewNotFoundError), () => reviewNotFound)
.otherwise(() => ({ status: 200, body: '리뷰 공개 여부가 업데이트되었습니다.' } as const));
.otherwise(
() =>
({
status: 200,
body: '리뷰 공개 여부가 업데이트되었습니다.',
} as const),
);
};

type PutDeps = Pick<ReviewsService, 'updateReview'>;
Expand All @@ -54,7 +64,9 @@ export const mkPutReviews: MkPut =
};

type DeleteDeps = Pick<ReviewsService, 'removeReview'>;
type MkDelete = (services: DeleteDeps) => HandlerFor<typeof contract.reviews.delete>;
type MkDelete = (
services: DeleteDeps,
) => HandlerFor<typeof contract.reviews.delete>;
export const mkDeleteReviews: MkDelete =
({ removeReview }) =>
async ({ params: { reviewsId }, req: { user } }) => {
Expand Down
14 changes: 14 additions & 0 deletions backend/src/v2/reviews/controller/impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ReviewsService } from '../service';
import {
mkDeleteReviews,
mkPatchReviews,
mkPostReviews,
mkPutReviews,
} from './controller';

export const implReviewController = (service: ReviewsService) => ({
post: mkPostReviews(service),
patch: mkPatchReviews(service),
put: mkPutReviews(service),
delete: mkDeleteReviews(service),
});
1 change: 1 addition & 0 deletions backend/src/v2/reviews/controller/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./controller";
35 changes: 5 additions & 30 deletions backend/src/v2/reviews/impl.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
import { contract } from '@jiphyeonjeon-42/contracts';
import { initServer } from '@ts-rest/express';
import jipDataSource from '~/app-data-source';
import BookInfo from '~/entity/entities/BookInfo';
import Reviews from '~/entity/entities/Reviews';
import { roleSet } from '~/v1/auth/auth.type';
import authValidate from '~/v1/auth/auth.validate';

import { Repository } from 'typeorm';
import { mkDeleteReviews, mkPatchReviews, mkPostReviews, mkPutReviews } from './controller';
import {
ReviewsService as ReviewService,
mkCreateReview,
mkRemoveReview,
mkToggleReviewVisibility,
mkUpdateReview,
} from './service';

const implReviewService = (repos: {
reviews: Repository<Reviews>;
bookInfo: Repository<BookInfo>;
}) => ({
createReview: mkCreateReview(repos),
removeReview: mkRemoveReview(repos),
updateReview: mkUpdateReview(repos),
toggleReviewVisibility: mkToggleReviewVisibility(repos),
});

const implReviewController = (service: ReviewService) => ({
post: mkPostReviews(service),
patch: mkPatchReviews(service),
put: mkPutReviews(service),
delete: mkDeleteReviews(service),
});
import jipDataSource from '~/app-data-source';
import BookInfo from '~/entity/entities/BookInfo';
import Reviews from '~/entity/entities/Reviews';
import { implReviewController } from './controller/impl';
import { implReviewService } from './service/impl';

const service = implReviewService({
reviews: jipDataSource.getRepository(Reviews),
bookInfo: jipDataSource.getRepository(BookInfo),
});

const handler = implReviewController(service);

const s = initServer();
Expand Down
19 changes: 19 additions & 0 deletions backend/src/v2/reviews/service/impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Repository } from 'typeorm';
import type BookInfo from '~/entity/entities/BookInfo';
import type Reviews from '~/entity/entities/Reviews';
import {
mkCreateReview,
mkRemoveReview,
mkToggleReviewVisibility,
mkUpdateReview,
} from './service';

export const implReviewService = (repos: {
reviews: Repository<Reviews>;
bookInfo: Repository<BookInfo>;
}) => ({
createReview: mkCreateReview(repos),
removeReview: mkRemoveReview(repos),
updateReview: mkUpdateReview(repos),
toggleReviewVisibility: mkToggleReviewVisibility(repos),
});