mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 20:19:26 +01:00
41 lines
801 B
TypeScript
41 lines
801 B
TypeScript
import Post from "App/Models/Post";
|
|
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
|
|
|
export default class PostsController {
|
|
|
|
public async getLikes ({params}: HttpContextContract) {
|
|
let post = await Post.findBy('slug', params.slug)
|
|
|
|
if (!post) {
|
|
post = await Post.create({
|
|
slug: params.slug,
|
|
likes: 0
|
|
})
|
|
}
|
|
|
|
return 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({
|
|
status: 200,
|
|
post
|
|
})
|
|
}
|
|
|
|
}
|