mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-31 11:57:50 +01:00
Working on posts
This commit is contained in:
@@ -3,15 +3,32 @@ import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
|||||||
|
|
||||||
export default class PostsController {
|
export default class PostsController {
|
||||||
|
|
||||||
public async getLikes ({ params, response }: HttpContextContract) {
|
public async index ({ response }: HttpContextContract) {
|
||||||
let post = await Post.findBy('slug', params.slug)
|
return response.status(200).send({
|
||||||
|
posts: await Post.query()
|
||||||
|
.preload('tags', (tags) => {
|
||||||
|
tags.preload('label')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (!post) {
|
public async show ({ params, response }: HttpContextContract) {
|
||||||
post = await Post.create({
|
const post = await Post.findByOrFail('slug', params.slug)
|
||||||
slug: params.slug,
|
await post.load('tags', (tags) => {
|
||||||
likes: 0
|
tags.preload('label')
|
||||||
})
|
})
|
||||||
}
|
return response.status(200).send({
|
||||||
|
post
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getLikes ({ params, response }: HttpContextContract) {
|
||||||
|
const post = await Post.firstOrCreate({
|
||||||
|
slug: params.slug
|
||||||
|
}, {
|
||||||
|
slug: params.slug,
|
||||||
|
likes: 0
|
||||||
|
})
|
||||||
|
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
likes: post.likes
|
likes: post.likes
|
||||||
@@ -19,33 +36,23 @@ export default class PostsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async like ({ params, response }: HttpContextContract) {
|
public async like ({ params, response }: HttpContextContract) {
|
||||||
let post = await Post.findBy('slug', params.slug)
|
const post = await Post.firstOrCreate({
|
||||||
|
slug: params.slug
|
||||||
if (!post) {
|
}, {
|
||||||
post = await Post.create({
|
slug: params.slug,
|
||||||
slug: params.slug,
|
likes: 0
|
||||||
likes: 0
|
})
|
||||||
})
|
post.likes = post.likes++
|
||||||
}
|
await post.save()
|
||||||
|
|
||||||
const getLikes = post.likes + 1
|
|
||||||
|
|
||||||
await post.merge({
|
|
||||||
likes: getLikes
|
|
||||||
}).save()
|
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
post
|
post
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async unlike ({ params, response }: HttpContextContract) {
|
public async unlike ({ params, response }: HttpContextContract) {
|
||||||
let post = await Post.findByOrFail('slug', params.slug)
|
const post = await Post.findByOrFail('slug', params.slug)
|
||||||
|
post.likes = post.likes--
|
||||||
const getLikes = post.likes - 1
|
await post.save()
|
||||||
|
|
||||||
await post.merge({
|
|
||||||
likes: getLikes
|
|
||||||
}).save()
|
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
post
|
post
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -36,7 +36,9 @@ export default class ProjectsController {
|
|||||||
const project = await Project.findOrFail(params.id)
|
const project = await Project.findOrFail(params.id)
|
||||||
await project.load('cover')
|
await project.load('cover')
|
||||||
await project.load('description')
|
await project.load('description')
|
||||||
await project.load('tags')
|
await project.load('tags', (tags) => {
|
||||||
|
tags.preload('label')
|
||||||
|
})
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
project
|
project
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -20,9 +20,11 @@ Route.group(() => {
|
|||||||
Route.resource('/informations', 'InformationsController').only(['index', 'update'])
|
Route.resource('/informations', 'InformationsController').only(['index', 'update'])
|
||||||
|
|
||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
Route.get('/:slug', 'PostsController.getLikes')
|
Route.get('/:slug/likes', 'PostsController.getLikes')
|
||||||
Route.post('/:slug/like', 'PostsController.like')
|
Route.post('/:slug/like', 'PostsController.like')
|
||||||
Route.post('/:slug/unlike', 'PostsController.unlike')
|
Route.post('/:slug/unlike', 'PostsController.unlike')
|
||||||
|
Route.get('/', 'PostsController.index')
|
||||||
|
Route.get('/:slug', 'PostsController.show')
|
||||||
}).prefix('/posts')
|
}).prefix('/posts')
|
||||||
|
|
||||||
Route.resource('/subscribers', 'SubscribersController').only(['index', 'store', 'destroy'])
|
Route.resource('/subscribers', 'SubscribersController').only(['index', 'store', 'destroy'])
|
||||||
|
|||||||
Reference in New Issue
Block a user