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,7 +1,7 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import Announce from "App/Models/Announce";
|
||||
import AnnounceUpdateValidator from "App/Validators/announce/AnnounceUpdateValidator";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import File from "App/Models/File";
|
||||
|
||||
export default class AnnouncesController {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import Experience from "App/Models/Experience";
|
||||
import ExperienceStoreValidator from "App/Validators/experience/ExperienceStoreValidator";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import ExperienceUpdateValidator from "App/Validators/experience/ExperienceUpdateValidator";
|
||||
|
||||
export default class ExperiencesController {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import FormationStoreValidator from "App/Validators/formation/FormationStoreValidator";
|
||||
import FormationUpdateValidator from "App/Validators/formation/FormationUpdateValidator";
|
||||
import Formation from "App/Models/Formation";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import Information from "App/Models/Information";
|
||||
import InformationUpdateValidator from "App/Validators/information/InformationUpdateValidator";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
|
||||
export default class InformationsController {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import Maintenance from "App/Models/Maintenance";
|
||||
import MaintenanceUpdateValidator from "App/Validators/maintenance/MaintenanceUpdateValidator";
|
||||
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import {getDiscordActivity} from "App/Tasks/GetDiscordActivity";
|
||||
import {getDiscordActivity} from "App/Utils/GetDiscordActivity";
|
||||
|
||||
export default class ProfileController {
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import Project from "App/Models/Project";
|
||||
import ProjectStoreValidator from "App/Validators/project/ProjectStoreValidator";
|
||||
import ProjectUpdateValidator from "App/Validators/project/ProjectUpdateValidator";
|
||||
import File from "App/Models/File";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
|
||||
export default class ProjectsController {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import getTranslation from "App/Utils/getTranslation";
|
||||
import TagStoreValidator from "App/Validators/tag/TagStoreValidator";
|
||||
import TagUpdateValidator from "App/Validators/tag/TagUpdateValidator";
|
||||
import Tag from "App/Models/Tag";
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import {BaseModel, column, manyToMany, ManyToMany} from '@ioc:Adonis/Lucid/Orm'
|
||||
import {BaseModel, BelongsTo, belongsTo, column, manyToMany, ManyToMany} from '@ioc:Adonis/Lucid/Orm'
|
||||
import Tag from "App/Models/Tag";
|
||||
import Translation from "App/Models/Translation";
|
||||
import File from "App/Models/File";
|
||||
|
||||
export default class Post extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
@@ -15,6 +17,33 @@ export default class Post extends BaseModel {
|
||||
@column()
|
||||
public likes: number
|
||||
|
||||
@belongsTo(() => Translation, {
|
||||
foreignKey: 'titleId'
|
||||
})
|
||||
public title: BelongsTo<typeof Translation>
|
||||
|
||||
@column()
|
||||
public titleId: number
|
||||
|
||||
@belongsTo(() => Translation, {
|
||||
foreignKey: 'descriptionId'
|
||||
})
|
||||
public description: BelongsTo<typeof Translation>
|
||||
|
||||
@column()
|
||||
public descriptionId: number
|
||||
|
||||
@belongsTo(() => File, {
|
||||
foreignKey: 'coverId'
|
||||
})
|
||||
public cover: BelongsTo<typeof File>
|
||||
|
||||
@column()
|
||||
public coverId: number
|
||||
|
||||
@column()
|
||||
public readingTime: number
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
|
||||
21
app/Validators/post/PostStoreValidator.ts
Normal file
21
app/Validators/post/PostStoreValidator.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {schema} from '@ioc:Adonis/Core/Validator'
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class PostStoreValidator {
|
||||
|
||||
public schema = schema.create({
|
||||
slug: schema.string(),
|
||||
likes: schema.number(),
|
||||
tags: schema.array().members(schema.string()),
|
||||
title: schema.string(),
|
||||
description: schema.string(),
|
||||
cover: schema.string(),
|
||||
readingTime: schema.number()
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,11 @@ export default class PostUpdateValidator {
|
||||
public schema = schema.create({
|
||||
slug: schema.string.optional(),
|
||||
likes: schema.number.optional(),
|
||||
tags: schema.array.optional().members(schema.string())
|
||||
tags: schema.array.optional().members(schema.string()),
|
||||
title: schema.string.optional(),
|
||||
description: schema.string.optional(),
|
||||
cover: schema.string.optional(),
|
||||
readingTime: schema.number.optional()
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
|
||||
@@ -19,9 +19,9 @@ Route.group(() => {
|
||||
|
||||
Route.resource('/informations', 'InformationsController').only(['index', 'update'])
|
||||
|
||||
Route.resource('/posts', 'PostsController').only(['index', 'destroy'])
|
||||
Route.resource('/posts', 'PostsController').except(['edit', 'create'])
|
||||
Route.group(() => {
|
||||
Route.get('/:slug', 'PostsController.show')
|
||||
Route.get('/:slug', 'PostsController.get')
|
||||
Route.post('/:slug/like', 'PostsController.like')
|
||||
Route.post('/:slug/unlike', 'PostsController.unlike')
|
||||
}).prefix('/posts')
|
||||
|
||||
Reference in New Issue
Block a user