Skip to content

Commit 3903bfe

Browse files
committed
Refactor articles and comments modules with schema and DTO separation
1 parent 01cfd8e commit 3903bfe

38 files changed

+313
-324
lines changed

db/drop.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { exit } from 'node:process';
22
import { db } from '@/database.providers';
3-
import { articles, comments, favoriteArticles } from '@articles/articles.model';
3+
import {
4+
articles,
5+
comments,
6+
favoriteArticles,
7+
} from '@/articles/schema/favorite-articles.schema';
48
import dbConfig from '@db/config';
59
import { articleTags, tags } from '@tags/tags.model';
610
import { userFollows, users } from '@users/users.model';

src/articles/articles.controller.ts

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import { setupArticles } from '@articles/articles.module';
2-
import {
3-
ArticleFeedQuerySchema,
4-
DeleteArticleResponse,
5-
InsertArticleSchema,
6-
ListArticlesQuerySchema,
7-
ReturnedArticleListSchema,
8-
ReturnedArticleResponseSchema,
9-
UpdateArticleSchema,
10-
} from '@articles/articles.schema';
112
import { Elysia, t } from 'elysia';
123
import {
13-
AddCommentSchema,
14-
DeleteCommentResponse,
15-
ReturnedCommentResponse,
16-
ReturnedCommentsResponse,
17-
} from './comments/comments.schema';
4+
ArticleResponseDto,
5+
CreateArticleDto,
6+
ListArticlesQueryDto,
7+
ListArticlesResponseDto,
8+
UpdateArticleDto,
9+
} from './dto';
10+
import { CommentResponseDto, CreateCommentDto } from '@comments/dto';
1811

1912
export const articlesController = new Elysia().use(setupArticles).group(
2013
'/articles',
@@ -35,8 +28,8 @@ export const articlesController = new Elysia().use(setupArticles).group(
3528
),
3629
}),
3730
{
38-
query: ListArticlesQuerySchema,
39-
response: ReturnedArticleListSchema,
31+
query: ListArticlesQueryDto,
32+
response: ListArticlesResponseDto,
4033
detail: {
4134
summary: 'List Articles',
4235
},
@@ -51,8 +44,8 @@ export const articlesController = new Elysia().use(setupArticles).group(
5144
),
5245
{
5346
beforeHandle: app.store.authService.requireLogin,
54-
body: InsertArticleSchema,
55-
response: ReturnedArticleResponseSchema,
47+
body: CreateArticleDto,
48+
response: ArticleResponseDto,
5649
detail: {
5750
summary: 'Create Article',
5851
security: [
@@ -75,8 +68,8 @@ export const articlesController = new Elysia().use(setupArticles).group(
7568
}),
7669
{
7770
beforeHandle: app.store.authService.requireLogin,
78-
query: ArticleFeedQuerySchema,
79-
response: ReturnedArticleListSchema,
71+
query: ListArticlesQueryDto,
72+
response: ListArticlesResponseDto,
8073
detail: {
8174
summary: 'Artifle Feed',
8275
security: [
@@ -97,7 +90,7 @@ export const articlesController = new Elysia().use(setupArticles).group(
9790
),
9891
),
9992
{
100-
response: ReturnedArticleResponseSchema,
93+
response: ArticleResponseDto,
10194
detail: {
10295
summary: 'Get Article',
10396
},
@@ -113,8 +106,8 @@ export const articlesController = new Elysia().use(setupArticles).group(
113106
),
114107
{
115108
beforeHandle: app.store.authService.requireLogin,
116-
body: UpdateArticleSchema,
117-
response: ReturnedArticleResponseSchema,
109+
body: UpdateArticleDto,
110+
response: ArticleResponseDto,
118111
detail: {
119112
summary: 'Update Article',
120113
security: [
@@ -127,14 +120,14 @@ export const articlesController = new Elysia().use(setupArticles).group(
127120
)
128121
.delete(
129122
'/:slug',
130-
async ({ params, store, request }) =>
131-
store.articlesService.deleteArticle(
123+
async ({ params, store, request }) => {
124+
await store.articlesService.deleteArticle(
132125
params.slug,
133126
await store.authService.getUserIdFromHeader(request.headers),
134-
),
127+
);
128+
},
135129
{
136130
beforeHandle: app.store.authService.requireLogin,
137-
response: DeleteArticleResponse,
138131
detail: {
139132
summary: 'Delete Article',
140133
security: [
@@ -157,11 +150,8 @@ export const articlesController = new Elysia().use(setupArticles).group(
157150
},
158151
{
159152
beforeHandle: app.store.authService.requireLogin,
160-
params: t.Object({
161-
slug: t.String(),
162-
}),
163-
body: AddCommentSchema,
164-
response: ReturnedCommentResponse,
153+
body: CreateCommentDto,
154+
response: CommentResponseDto,
165155
detail: {
166156
summary: 'Add Comment to Article',
167157
},
@@ -181,10 +171,9 @@ export const articlesController = new Elysia().use(setupArticles).group(
181171
};
182172
},
183173
{
184-
params: t.Object({
185-
slug: t.String(),
174+
response: t.Object({
175+
comments: t.Array(CommentResponseDto),
186176
}),
187-
response: ReturnedCommentsResponse,
188177
detail: {
189178
summary: 'Get Comments from Article',
190179
},
@@ -198,15 +187,13 @@ export const articlesController = new Elysia().use(setupArticles).group(
198187
Number.parseInt(params.id, 10),
199188
await store.authService.getUserIdFromHeader(request.headers),
200189
);
201-
return {};
202190
},
203191
{
204192
beforeHandle: app.store.authService.requireLogin,
205193
params: t.Object({
206194
slug: t.String(),
207195
id: t.String(),
208196
}),
209-
response: DeleteCommentResponse,
210197
detail: {
211198
summary: 'Delete Comment',
212199
},
@@ -221,7 +208,7 @@ export const articlesController = new Elysia().use(setupArticles).group(
221208
),
222209
{
223210
beforeHandle: app.store.authService.requireLogin,
224-
response: ReturnedArticleResponseSchema,
211+
response: ArticleResponseDto,
225212
detail: {
226213
summary: 'Favorite Article',
227214
security: [
@@ -241,7 +228,7 @@ export const articlesController = new Elysia().use(setupArticles).group(
241228
),
242229
{
243230
beforeHandle: app.store.authService.requireLogin,
244-
response: ReturnedArticleResponseSchema,
231+
response: ArticleResponseDto,
245232
detail: {
246233
summary: 'Unfavorite Article',
247234
security: [

src/articles/articles.model.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/articles/articles.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CommentsRepository } from '@/articles/comments/comments.repository';
2-
import { CommentsService } from '@/articles/comments/comments.service';
1+
import { CommentsRepository } from '@/comments/comments.repository';
2+
import { CommentsService } from '@/comments/comments.service';
33
import { db } from '@/database.providers';
44
import { ArticlesRepository } from '@articles/articles.repository';
55
import { ArticlesService } from '@articles/articles.service';

src/articles/articles.repository.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import type {
2-
ArticleToCreate,
3-
ArticleToUpdate,
4-
} from '@/articles/articles.schema';
51
import type { Database } from '@/database.providers';
6-
import { userFollows, users } from '@/users/users.model';
7-
import { articles, favoriteArticles } from '@articles/articles.model';
2+
import { userFollows, users } from '@users/users.model';
3+
import { articles, favoriteArticles } from '@articles/schema';
84
import { articleTags } from '@tags/tags.model';
95
import { and, count, desc, eq, inArray, sql } from 'drizzle-orm';
106

src/articles/articles.schema.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/articles/articles.service.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { AuthorizationError, BadRequestError } from '@/errors';
22
import { slugify } from '@/utils/slugify';
33
import type { ArticlesRepository } from '@articles/articles.repository';
4-
import type {
5-
ArticleInDb,
6-
ArticleToCreate,
7-
ArticleToCreateData,
8-
ArticleToUpdateRequest,
9-
ReturnedArticleList,
10-
ReturnedArticleResponse,
11-
} from '@articles/articles.schema';
124
import type { ProfilesService } from '@profiles/profiles.service';
135
import type { TagsService } from '@tags/tags.service';
146
import { NotFoundError } from 'elysia';
@@ -145,7 +137,10 @@ export class ArticlesService {
145137
}
146138

147139
async unfavoriteArticle(slug: string, currentUserId: number) {
148-
const article = await this.repository.unfavoriteArticle(slug, currentUserId);
140+
const article = await this.repository.unfavoriteArticle(
141+
slug,
142+
currentUserId,
143+
);
149144
if (!article) {
150145
throw new NotFoundError('Article not found');
151146
}

0 commit comments

Comments
 (0)