mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 20:19:26 +01:00
Lint and update
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import Post from "App/Models/Post";
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import PostUpdateValidator from "App/Validators/post/PostUpdateValidator";
|
||||
import File from "App/Models/File";
|
||||
import PostStoreValidator from "App/Validators/post/PostStoreValidator";
|
||||
import PostColor from "App/Models/PostColor";
|
||||
import {getTranslation} from "App/Utils/TranslationsUtils";
|
||||
import Post from 'App/Models/Post'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import PostUpdateValidator from 'App/Validators/post/PostUpdateValidator'
|
||||
import File from 'App/Models/File'
|
||||
import PostStoreValidator from 'App/Validators/post/PostStoreValidator'
|
||||
import PostColor from 'App/Models/PostColor'
|
||||
import { getTranslation } from 'App/Utils/TranslationsUtils'
|
||||
|
||||
export default class PostsController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
public async index({ response }: HttpContextContract) {
|
||||
return response.status(200).send({
|
||||
posts: await Post.query()
|
||||
.orderBy('id', 'desc')
|
||||
@@ -19,11 +18,11 @@ export default class PostsController {
|
||||
.preload('color')
|
||||
.preload('content')
|
||||
.preload('title')
|
||||
.preload('description')
|
||||
.preload('description'),
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
public async store({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(PostStoreValidator)
|
||||
const post = await Post.create(data)
|
||||
|
||||
@@ -40,11 +39,11 @@ export default class PostsController {
|
||||
await post.related('tags').sync(data.tags!)
|
||||
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
public async show({ params, response }: HttpContextContract) {
|
||||
const post = await Post.findOrFail(params.id)
|
||||
await post.load('cover')
|
||||
await post.load('title')
|
||||
@@ -55,16 +54,16 @@ export default class PostsController {
|
||||
tags.preload('label')
|
||||
})
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
public async get ({ params, response }: HttpContextContract) {
|
||||
public async get({ params, response }: HttpContextContract) {
|
||||
const post = await Post.firstOrCreate({
|
||||
slug: params.slug
|
||||
slug: params.slug,
|
||||
}, {
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
likes: 0,
|
||||
})
|
||||
await post.load('tags', (tags) => {
|
||||
tags.preload('label')
|
||||
@@ -75,11 +74,11 @@ export default class PostsController {
|
||||
await post.load('content')
|
||||
await post.load('color')
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
public async update({ request, params, response }: HttpContextContract) {
|
||||
const post = await Post.findOrFail(params.id)
|
||||
const data = await request.validate(PostUpdateValidator)
|
||||
|
||||
@@ -97,43 +96,42 @@ export default class PostsController {
|
||||
if (color) await post.related('color').associate(color)
|
||||
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
public async destroy({ response, params }: HttpContextContract) {
|
||||
const post = await Post.findOrFail(params.id)
|
||||
await post.delete()
|
||||
return response.status(200).send({
|
||||
message: 'Post successfully deleted!'
|
||||
message: 'Post successfully deleted!',
|
||||
})
|
||||
}
|
||||
|
||||
public async like ({ params, response }: HttpContextContract) {
|
||||
public async like({ params, response }: HttpContextContract) {
|
||||
const post = await Post.firstOrCreate({
|
||||
slug: params.slug
|
||||
slug: params.slug,
|
||||
}, {
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
likes: 0,
|
||||
})
|
||||
const getLikes = post.likes
|
||||
await post.merge({
|
||||
likes: getLikes + 1
|
||||
likes: getLikes + 1,
|
||||
}).save()
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
public async unlike ({ params, response }: HttpContextContract) {
|
||||
public async unlike({ params, response }: HttpContextContract) {
|
||||
const post = await Post.findByOrFail('slug', params.slug)
|
||||
const getLikes = post.likes
|
||||
await post.merge({
|
||||
likes: getLikes - 1
|
||||
likes: getLikes - 1,
|
||||
}).save()
|
||||
return response.status(200).send({
|
||||
post
|
||||
post,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user