mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Add PostColor and content
This commit is contained in:
46
app/Controllers/Http/PostColorsController.ts
Normal file
46
app/Controllers/Http/PostColorsController.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import PostColor from "App/Models/PostColor";
|
||||
import PostColorStoreValidator from "App/Validators/postColor/PostColorStoreValidator";
|
||||
import PostColorUpdateValidator from "App/Validators/postColor/PostColorUpdateValidator";
|
||||
|
||||
export default class PostColorsController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
return response.status(200).send({
|
||||
users: await PostColor.all()
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(PostColorStoreValidator)
|
||||
const postColor = await PostColor.create(data)
|
||||
return response.status(200).send({
|
||||
postColor
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const postColor = await PostColor.findOrFail(params.id)
|
||||
return response.status(200).send({
|
||||
postColor
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(PostColorUpdateValidator)
|
||||
const postColor = await PostColor.findOrFail(params.id)
|
||||
await postColor.merge(data).save()
|
||||
return response.status(200).send({
|
||||
postColor
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
const postColor = await PostColor.findOrFail(params.id)
|
||||
await postColor.delete()
|
||||
return response.status(200).send({
|
||||
message: 'PostColor successfully deleted!'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ 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";
|
||||
import PostColor from "App/Models/PostColor";
|
||||
|
||||
export default class PostsController {
|
||||
|
||||
@@ -15,6 +16,8 @@ export default class PostsController {
|
||||
tags.preload('label')
|
||||
})
|
||||
.preload('cover')
|
||||
.preload('color')
|
||||
.preload('content')
|
||||
.preload('title')
|
||||
.preload('description')
|
||||
})
|
||||
@@ -23,12 +26,19 @@ 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)
|
||||
const color = await PostColor.findByOrFail('name', data.color)
|
||||
|
||||
await post.related('cover').associate(cover)
|
||||
await post.related('color').associate(color)
|
||||
|
||||
await post.related('description').associate(await getTranslation(data.description))
|
||||
await post.related('title').associate(await getTranslation(data.title))
|
||||
await post.related('content').associate(await getTranslation(data.content))
|
||||
|
||||
await post.related('tags').sync(data.tags!)
|
||||
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
@@ -39,6 +49,8 @@ export default class PostsController {
|
||||
await post.load('cover')
|
||||
await post.load('title')
|
||||
await post.load('description')
|
||||
await post.load('content')
|
||||
await post.load('color')
|
||||
await post.load('tags', (tags) => {
|
||||
tags.preload('label')
|
||||
})
|
||||
@@ -60,6 +72,8 @@ export default class PostsController {
|
||||
await post.load('cover')
|
||||
await post.load('description')
|
||||
await post.load('title')
|
||||
await post.load('content')
|
||||
await post.load('color')
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
@@ -70,12 +84,18 @@ export default class PostsController {
|
||||
const data = await request.validate(PostUpdateValidator)
|
||||
|
||||
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!))
|
||||
await post.related('content').associate(await getTranslation(data.content!))
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await post.related('cover').associate(cover)
|
||||
|
||||
const color = await PostColor.findBy('name', data.color)
|
||||
if (color) await post.related('color').associate(color)
|
||||
|
||||
return response.status(200).send({
|
||||
post
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import {BaseModel, BelongsTo, belongsTo, column, manyToMany, ManyToMany} from '@
|
||||
import Tag from "App/Models/Tag";
|
||||
import Translation from "App/Models/Translation";
|
||||
import File from "App/Models/File";
|
||||
import PostColor from "App/Models/PostColor";
|
||||
|
||||
export default class Post extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
@@ -49,6 +50,14 @@ export default class Post extends BaseModel {
|
||||
@column()
|
||||
public contentId: number
|
||||
|
||||
@belongsTo(() => PostColor, {
|
||||
foreignKey: 'postColorId'
|
||||
})
|
||||
public color: BelongsTo<typeof PostColor>
|
||||
|
||||
@column()
|
||||
public postColorId: number
|
||||
|
||||
@column()
|
||||
public readingTime: number
|
||||
|
||||
|
||||
19
app/Models/PostColor.ts
Normal file
19
app/Models/PostColor.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
|
||||
|
||||
export default class PostColor extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column()
|
||||
public name: string
|
||||
|
||||
@column()
|
||||
public color: string
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime
|
||||
}
|
||||
@@ -12,6 +12,8 @@ export default class PostStoreValidator {
|
||||
cover: schema.string(),
|
||||
readingTime: schema.number(),
|
||||
date: schema.string(),
|
||||
color: schema.string(),
|
||||
content: schema.string()
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
|
||||
@@ -12,6 +12,8 @@ export default class PostUpdateValidator {
|
||||
cover: schema.string.optional(),
|
||||
readingTime: schema.number.optional(),
|
||||
date: schema.string.optional(),
|
||||
color: schema.string.optional(),
|
||||
content: schema.string.optional()
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
|
||||
16
app/Validators/postColor/PostColorStoreValidator.ts
Executable file
16
app/Validators/postColor/PostColorStoreValidator.ts
Executable file
@@ -0,0 +1,16 @@
|
||||
import {schema} from '@ioc:Adonis/Core/Validator'
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class PostColorStoreValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
name: schema.string(),
|
||||
color: schema.string()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
}
|
||||
16
app/Validators/postColor/PostColorUpdateValidator.ts
Normal file
16
app/Validators/postColor/PostColorUpdateValidator.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {schema} from '@ioc:Adonis/Core/Validator'
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class PostColorUpdateValidator {
|
||||
public schema = schema.create({
|
||||
name: schema.string.optional(),
|
||||
color: schema.string.optional()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,11 @@ export default class Posts extends BaseSchema {
|
||||
.unsigned()
|
||||
.references('translations.id')
|
||||
.onDelete('CASCADE')
|
||||
table
|
||||
.integer('post_color_id')
|
||||
.unsigned()
|
||||
.references('post_colors.id')
|
||||
.onDelete('CASCADE')
|
||||
table.timestamps(true, true)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ export default class Maintenances extends BaseSchema {
|
||||
|
||||
public async up () {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id')
|
||||
table.increments('id').primary()
|
||||
table.boolean('active').defaultTo(false).notNullable()
|
||||
table
|
||||
.integer('reason_id')
|
||||
|
||||
18
database/migrations/1630182451186_post_colors.ts
Normal file
18
database/migrations/1630182451186_post_colors.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||
|
||||
export default class PostColors extends BaseSchema {
|
||||
protected tableName = 'post_colors'
|
||||
|
||||
public async up () {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id').primary()
|
||||
table.string('name').notNullable()
|
||||
table.string('color').notNullable()
|
||||
table.timestamps(true, true)
|
||||
})
|
||||
}
|
||||
|
||||
public async down () {
|
||||
this.schema.dropTable(this.tableName)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ Route.group(() => {
|
||||
|
||||
Route.resource('/maintenance', 'MaintenancesController').only(['index', 'update'])
|
||||
|
||||
Route.resource('/colors', 'PostColorsController').only(['index', 'update'])
|
||||
|
||||
Route.resource('/experiences', 'ExperiencesController').except(['edit', 'create'])
|
||||
|
||||
Route.resource('/formations', 'FormationsController').except(['edit', 'create'])
|
||||
|
||||
Reference in New Issue
Block a user