Skip to content
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
40 changes: 40 additions & 0 deletions src/articles/articles.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,45 @@ export const articlesPlugin = new Elysia().use(setupArticles).group(
summary: 'Delete Comment',
},
},
)
.post(
'/:slug/favorite',
async ({ params, store, request }) =>
store.articlesService.favoriteArticle(
params.slug,
await store.authService.getUserIdFromHeader(request.headers),
),
{
beforeHandle: app.store.authService.requireLogin,
response: ReturnedArticleResponseSchema,
detail: {
summary: 'Favorite Article',
security: [
{
tokenAuth: [],
},
],
},
},
)
.delete(
'/:slug/favorite',
async ({ params, store, request }) =>
store.articlesService.unfavoriteArticle(
params.slug,
await store.authService.getUserIdFromHeader(request.headers),
),
{
beforeHandle: app.store.authService.requireLogin,
response: ReturnedArticleResponseSchema,
detail: {
summary: 'Unfavorite Article',
security: [
{
tokenAuth: [],
},
],
},
},
),
);
38 changes: 38 additions & 0 deletions src/articles/articles.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,42 @@ export class ArticlesRepository {
and(eq(articles.slug, slug), eq(articles.authorId, currentUserId)),
);
}

async favoriteArticle(slug: string, currentUserId: number) {
// TODO: Use a transaction to optimize from 1-3 ops to 1 op
const article = await this.findBySlug(slug);
if (!article) {
return null;
}

// Insert the favorite and get the updated article state
await this.db
.insert(favoriteArticles)
.values({ articleId: article.id, userId: currentUserId })
.onConflictDoNothing();

// Return the updated article state
return this.findBySlug(slug);
}

async unfavoriteArticle(slug: string, currentUserId: number) {
// TODO: Use a transaction to optimize from 1-3 ops to 1 op
const article = await this.findBySlug(slug);
if (!article) {
return null;
}

// Delete the favorite and get the updated article state
await this.db
.delete(favoriteArticles)
.where(
and(
eq(favoriteArticles.articleId, article.id),
eq(favoriteArticles.userId, currentUserId),
),
);

// Return the updated article state
return this.findBySlug(slug);
}
}
16 changes: 16 additions & 0 deletions src/articles/articles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@ export class ArticlesService {
},
};
}

async favoriteArticle(slug: string, currentUserId: number) {
const article = await this.repository.favoriteArticle(slug, currentUserId);
if (!article) {
throw new NotFoundError('Article not found');
}
return await this.generateArticleResponse(article, currentUserId);
}

async unfavoriteArticle(slug: string, currentUserId: number) {
const article = await this.repository.unfavoriteArticle(slug, currentUserId);
if (!article) {
throw new NotFoundError('Article not found');
}
return await this.generateArticleResponse(article, currentUserId);
}
}
Loading