mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-24 00:40:25 +01:00
Working on posts
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import Post from "App/Models/Post";
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import PostUpdateValidator from "App/Validators/post/PostUpdateValidator";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import File from "App/Models/File";
|
||||
import PostStoreValidator from "App/Validators/post/PostStoreValidator";
|
||||
|
||||
export default class PostsController {
|
||||
|
||||
@@ -13,7 +16,34 @@ export default class PostsController {
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(PostStoreValidator)
|
||||
const post = await Post.create(data)
|
||||
const cover = await File.findByOrFail('label', data.cover)
|
||||
|
||||
await post.related('cover').associate(cover)
|
||||
await post.related('description').associate(await getTranslation(data.description))
|
||||
await post.related('title').associate(await getTranslation(data.title))
|
||||
await post.related('tags').sync(data.tags!)
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const post = await Post.findOrFail(params.id)
|
||||
await post.load('cover')
|
||||
await post.load('title')
|
||||
await post.load('description')
|
||||
await post.load('tags', (tags) => {
|
||||
tags.preload('label')
|
||||
})
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
}
|
||||
|
||||
public async get ({ params, response }: HttpContextContract) {
|
||||
const post = await Post.firstOrCreate({
|
||||
slug: params.slug
|
||||
}, {
|
||||
@@ -34,6 +64,11 @@ export default class PostsController {
|
||||
|
||||
await post.merge(data).save()
|
||||
await post.related('tags').sync(data.tags!)
|
||||
await post.related('description').associate(await getTranslation(data.description!))
|
||||
await post.related('title').associate(await getTranslation(data.title!))
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await post.related('cover').associate(cover)
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user