mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Working on posts
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Post from "App/Models/Post";
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import PostUpdateValidator from "App/Validators/post/PostUpdateValidator";
|
||||
|
||||
export default class PostsController {
|
||||
|
||||
@@ -13,7 +14,12 @@ export default class PostsController {
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const post = await Post.findByOrFail('slug', params.slug)
|
||||
const post = await Post.firstOrCreate({
|
||||
slug: params.slug
|
||||
}, {
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
})
|
||||
await post.load('tags', (tags) => {
|
||||
tags.preload('label')
|
||||
})
|
||||
@@ -22,16 +28,22 @@ export default class PostsController {
|
||||
})
|
||||
}
|
||||
|
||||
public async getLikes ({ params, response }: HttpContextContract) {
|
||||
const post = await Post.firstOrCreate({
|
||||
slug: params.slug
|
||||
}, {
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
})
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const post = await Post.findOrFail(params.id)
|
||||
const data = await request.validate(PostUpdateValidator)
|
||||
|
||||
await post.merge(data).save()
|
||||
await post.related('tags').sync(data.tags!)
|
||||
return response.status(200).send({
|
||||
likes: post.likes
|
||||
post
|
||||
})
|
||||
}
|
||||
|
||||
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!'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
17
app/Validators/post/PostUpdateValidator.ts
Normal file
17
app/Validators/post/PostUpdateValidator.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import {schema} from '@ioc:Adonis/Core/Validator'
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class PostUpdateValidator {
|
||||
|
||||
public schema = schema.create({
|
||||
slug: schema.string.optional(),
|
||||
likes: schema.number.optional(),
|
||||
tags: schema.array.optional().members(schema.string())
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,9 @@ Route.group(() => {
|
||||
Route.resource('/informations', 'InformationsController').only(['index', 'update'])
|
||||
|
||||
Route.group(() => {
|
||||
Route.get('/:slug/likes', 'PostsController.getLikes')
|
||||
Route.resource('/', 'PostsController').only(['index', 'show', 'destroy'])
|
||||
Route.post('/:slug/like', 'PostsController.like')
|
||||
Route.post('/:slug/unlike', 'PostsController.unlike')
|
||||
Route.get('/', 'PostsController.index')
|
||||
Route.get('/:slug', 'PostsController.show')
|
||||
}).prefix('/posts')
|
||||
|
||||
Route.resource('/subscribers', 'SubscribersController').only(['index', 'store', 'destroy'])
|
||||
|
||||
Reference in New Issue
Block a user