mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 20:19:26 +01:00
55 lines
1.1 KiB
TypeScript
Executable File
55 lines
1.1 KiB
TypeScript
Executable File
import Post from "App/Models/Post";
|
|
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
|
|
|
export default class PostsController {
|
|
|
|
public async getLikes ({ params, response }: HttpContextContract) {
|
|
let post = await Post.findBy('slug', params.slug)
|
|
|
|
if (!post) {
|
|
post = await Post.create({
|
|
slug: params.slug,
|
|
likes: 0
|
|
})
|
|
}
|
|
|
|
return response.status(200).send({
|
|
likes: post.likes
|
|
})
|
|
}
|
|
|
|
public async like ({ params, response }: HttpContextContract) {
|
|
let post = await Post.findBy('slug', params.slug)
|
|
|
|
if (!post) {
|
|
post = await Post.create({
|
|
slug: params.slug,
|
|
likes: 0
|
|
})
|
|
}
|
|
|
|
const getLikes = post.likes + 1
|
|
|
|
await post.merge({
|
|
likes: getLikes
|
|
}).save()
|
|
return response.status(200).send({
|
|
post
|
|
})
|
|
}
|
|
|
|
public async unlike ({ params, response }: HttpContextContract) {
|
|
let post = await Post.findByOrFail('slug', params.slug)
|
|
|
|
const getLikes = post.likes - 1
|
|
|
|
await post.merge({
|
|
likes: getLikes
|
|
}).save()
|
|
return response.status(200).send({
|
|
post
|
|
})
|
|
}
|
|
|
|
}
|