Working on posts

This commit is contained in:
2021-08-23 17:55:49 +02:00
parent bb9ac0156b
commit 8c5032fa72
15 changed files with 101 additions and 12 deletions

View File

@@ -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
})