-
Notifications
You must be signed in to change notification settings - Fork 6
Implement comments feature for articles #134
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
ca6358e
add article CRUD endpoints
Hajbo b919017
Implement comments feature for articles
yamcodes 5c4447f
improve names and repository
yamcodes 813b6c8
Fix delete method in CommentsRepository to return the result of the d…
yamcodes 557f2d2
Rename to slugify
Hajbo d3e679f
Add trailing slashes
Hajbo aba29d2
Return nulls if no article was found
Hajbo b7714e1
Update models & migrations
Hajbo c879e3a
Fix db:drop utility
Hajbo 969bcc8
Specify error for optional authentication
Hajbo d811f95
Fix returned article count
Hajbo 100453b
Fix feed by only returning articles from followed users, run bun fix
Hajbo e707eb6
Simplifiy update logic
Hajbo 7c8a103
Merge branch 'main' into feature/articles-crud
Hajbo f90e537
Merge branch 'feature/articles-crud' into 78-implement-article-commen…
yamcodes 08df554
edit taglist
yamcodes 76b0ec8
fix more weird ai stuff
yamcodes 4a70667
fix missing types
yamcodes b8a055a
fix DeleteArticleResponse schema
yamcodes 23ec6aa
Add article CRUD endpoints and some others (#129)
Hajbo a6cc3fe
fix dates
yamcodes 91447f6
Refactor comment author retrieval and add findByUserId method
yamcodes 83ab1c4
Enhance comment retrieval with author details
yamcodes 41ee814
Refactor comment author profile retrieval in CommentsService
yamcodes 7f75dc5
Merge branch 'main' into 78-implement-article-comment-feature-2
yamcodes 1cd3254
remove unused method
yamcodes f8e7faa
Enhance CommentsService and ProfilesService with improved type handli…
yamcodes 18114bd
Refactor CommentsRepository and CommentsService for enhanced comment …
yamcodes 29923e7
Simplify findById method in CommentsRepository
yamcodes dd9b96e
Update error handling in CommentsService to use NotFoundError
yamcodes 581e9db
Update error handling in CommentsService to throw NotFoundError for m…
yamcodes 3d964a3
Merge branch 'main' into 78-implement-article-comment-feature-2
yamcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import type { Database } from '@/database.providers'; | ||
| import { articles, comments } from '@articles/articles.model'; | ||
| import { and, desc, eq } from 'drizzle-orm'; | ||
| import type { CommentToCreate } from './comments.schema'; | ||
|
|
||
| export class CommentsRepository { | ||
| constructor(private readonly db: Database) {} | ||
|
|
||
| async create(commentData: CommentToCreate) { | ||
| const [comment] = await this.db | ||
| .insert(comments) | ||
| .values(commentData) | ||
| .returning(); | ||
| return comment; | ||
| } | ||
|
|
||
| /** | ||
| * Find a comment by its id | ||
| * @param id - The id of the comment | ||
| * @returns The comment | ||
| */ | ||
| async findById(id: number) { | ||
| const result = await this.db.query.comments.findFirst({ | ||
| where: eq(comments.id, id), | ||
| }); | ||
| return result; | ||
| } | ||
Hajbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Find all comments by article id | ||
| * | ||
| * Note: this operation is optimized to include the author and their followers. | ||
| * Use it with caution. If you need something simpler, consider refactoring this method and making the "with" option dynamic. | ||
| * @param articleId - The id of the article | ||
| * @returns An array of comments | ||
| */ | ||
| async findManyByArticleId(articleId: number) { | ||
| const result = await this.db.query.comments.findMany({ | ||
| where: eq(comments.articleId, articleId), | ||
| orderBy: [desc(comments.createdAt)], | ||
| with: { | ||
| author: { | ||
| columns: { | ||
| id: true, | ||
| username: true, | ||
| bio: true, | ||
| image: true, | ||
| }, | ||
| with: { | ||
| followers: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| async findBySlug(slug: string) { | ||
| const result = await this.db.query.articles.findFirst({ | ||
| where: eq(articles.slug, slug), | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| async delete(commentId: number, authorId: number) { | ||
| return await this.db | ||
| .delete(comments) | ||
| .where(and(eq(comments.id, commentId), eq(comments.authorId, authorId))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { selectUserSchemaRaw } from '@/users/users.schema'; | ||
| import { comments } from '@articles/articles.model'; | ||
| import { type Static, Type } from '@sinclair/typebox'; | ||
| import { createInsertSchema, createSelectSchema } from 'drizzle-typebox'; | ||
|
|
||
| export const insertCommentSchemaRaw = createInsertSchema(comments); | ||
| export const selectCommentSchemaRaw = createSelectSchema(comments); | ||
|
|
||
| export const AddCommentSchema = Type.Object({ | ||
| comment: Type.Object({ | ||
| body: Type.String(), | ||
| }), | ||
| }); | ||
|
|
||
| export type CommentToCreate = Static<typeof AddCommentSchema>['comment'] & { | ||
| authorId: number; | ||
| articleId: number; | ||
| }; | ||
|
|
||
| export const ReturnedCommentSchema = Type.Composite([ | ||
| Type.Omit(selectCommentSchemaRaw, ['articleId', 'authorId']), | ||
| Type.Object({ | ||
| author: Type.Composite([ | ||
| Type.Omit(selectUserSchemaRaw, [ | ||
| 'id', | ||
| 'email', | ||
| 'password', | ||
| 'createdAt', | ||
| 'updatedAt', | ||
| ]), | ||
| Type.Object({ | ||
| following: Type.Boolean(), | ||
| }), | ||
| ]), | ||
| }), | ||
| ]); | ||
|
|
||
| export type ReturnedComment = Static<typeof ReturnedCommentSchema>; | ||
|
|
||
| export const ReturnedCommentResponse = Type.Object({ | ||
| comment: ReturnedCommentSchema, | ||
| }); | ||
|
|
||
| export const ReturnedCommentsResponse = Type.Object({ | ||
| comments: Type.Array(ReturnedCommentSchema), | ||
| }); | ||
|
|
||
| export const DeleteCommentResponse = Type.Object({}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.