-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat 🎸 (be): add post reaction model and migration * feat 🎸 (be): add post reaction service, controller and routes * styles 💅 : created post-reaction component * feat 🎸 : progress on create/delete actions * refactor ✨ : general improvements in UI * test 🧪 : add reactions minimal browser test
- Loading branch information
1 parent
664da6c
commit 7076823
Showing
20 changed files
with
483 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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,38 @@ | ||
import PostReactionService from '#services/post_reaction_service'; | ||
import { errorsReducer } from '#utils/index'; | ||
import { postReactionValidator } from '#validators/post'; | ||
import { inject } from '@adonisjs/core'; | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
import { errors } from '@vinejs/vine'; | ||
|
||
@inject() | ||
export default class PostReactionsController { | ||
|
||
constructor( | ||
private readonly service: PostReactionService | ||
) { } | ||
|
||
async create(ctx: HttpContext) { | ||
const postId = ctx.params.id; | ||
const userId = ctx.auth.user?.id!; | ||
const payload = ctx.request.body(); | ||
try { | ||
const { reaction } = await postReactionValidator.validate(payload) | ||
const [preExistant, resource] = await this.service.create(userId, postId, reaction); | ||
return preExistant ? ctx.response.ok(resource) : ctx.response.created(resource); | ||
} catch (error) { | ||
if (error instanceof errors.E_VALIDATION_ERROR) { | ||
const reducedErrors = errorsReducer(error.messages) | ||
return ctx.response.badRequest(reducedErrors); | ||
} | ||
return ctx.response.badRequest(); | ||
} | ||
} | ||
|
||
async destroy(ctx: HttpContext) { | ||
const postId = ctx.params.id; | ||
const userId = ctx.auth.user?.id!; | ||
await this.service.destroy(userId, postId); | ||
return ctx.response.noContent() | ||
} | ||
} |
This file contains 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 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 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,8 @@ | ||
export enum PostReactionType { | ||
LIKE = 'LIKE', | ||
LOVE = 'LOVE', | ||
THANKFUL = 'THANKFUL', | ||
FUNNY = 'FUNNY', | ||
ANGRY = 'ANGRY', | ||
CONGRATULATIONS = 'CONGRATULATIONS', | ||
} |
This file contains 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 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 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,33 @@ | ||
import { DateTime } from 'luxon' | ||
import { BaseModel, belongsTo, column } from '@adonisjs/lucid/orm' | ||
import Post from '#models/post' | ||
import User from '#models/user' | ||
import { PostReactionType } from '#enums/post' | ||
import type { BelongsTo } from '@adonisjs/lucid/types/relations' | ||
import type { UUID } from 'crypto' | ||
|
||
export default class PostReaction extends BaseModel { | ||
@column({ isPrimary: true }) | ||
declare id: UUID | ||
|
||
@belongsTo(() => User) | ||
declare user: BelongsTo<typeof User> | ||
|
||
@column() | ||
declare userId: UUID | ||
|
||
@belongsTo(() => Post) | ||
declare post: BelongsTo<typeof Post> | ||
|
||
@column() | ||
declare postId: UUID | ||
|
||
@column() | ||
declare type: PostReactionType | ||
|
||
@column.dateTime({ autoCreate: true }) | ||
declare createdAt: DateTime | ||
|
||
@column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
declare updatedAt: DateTime | ||
} |
This file contains 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,49 @@ | ||
import { PostReactionType } from '#enums/post'; | ||
import PostReaction from '#models/post_reaction' | ||
import type { UUID } from 'crypto' | ||
|
||
export default class PostReactionService { | ||
constructor() { } | ||
|
||
async show(userId: UUID, postId: UUID): Promise<PostReaction | null> { | ||
return PostReaction.findBy({ | ||
userId, | ||
postId, | ||
}) | ||
} | ||
|
||
async create(userId: UUID, postId: UUID, type: PostReactionType): Promise<[boolean, PostReaction]> { | ||
const existant = await this.show( | ||
userId, | ||
postId, | ||
) | ||
|
||
let resource: PostReaction; | ||
if (existant) { | ||
resource = await this.update(existant, type) | ||
} else { | ||
resource = await PostReaction.create({ | ||
userId, | ||
postId, | ||
type | ||
}) | ||
} | ||
return [!!existant, resource] | ||
|
||
} | ||
|
||
async update(reaction: PostReaction, type: PostReactionType): Promise<PostReaction> { | ||
reaction.type = type; | ||
await reaction.save() | ||
return reaction | ||
} | ||
|
||
async destroy(userId: UUID, postId: UUID): Promise<void> { | ||
const reaction = await this.show( | ||
userId, | ||
postId, | ||
) | ||
if (!reaction) return | ||
await reaction.delete() | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.