mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Added Tags
This commit is contained in:
58
app/Controllers/Http/TagsController.ts
Normal file
58
app/Controllers/Http/TagsController.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import TagStoreValidator from "App/Validators/tag/TagStoreValidator";
|
||||
import TagUpdateValidator from "App/Validators/tag/TagUpdateValidator";
|
||||
import Tag from "App/Models/Tag";
|
||||
|
||||
export default class TagsController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
const tags = await Tag
|
||||
.query()
|
||||
.preload('label')
|
||||
return response.status(200).send({
|
||||
tags: tags
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(TagStoreValidator)
|
||||
const tag = await Tag.create({})
|
||||
|
||||
await tag.related('label').associate(await getTranslation(data.label))
|
||||
|
||||
return response.status(200).send({
|
||||
tag: tag
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const tag = await Tag.findOrFail(params.id)
|
||||
tag.load('label')
|
||||
return response.status(200).send({
|
||||
tag
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(TagUpdateValidator)
|
||||
const tag = await Tag.findOrFail(params.id)
|
||||
|
||||
if (data.label) {
|
||||
await tag.related('label').associate(await getTranslation(data.label))
|
||||
}
|
||||
|
||||
return response.status(200).send({
|
||||
tag
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
const tag = await Tag.findOrFail(params.id)
|
||||
await tag.delete()
|
||||
return response.status(200).send({
|
||||
message: 'Tag successfully deleted!'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
21
app/Models/Tag.ts
Normal file
21
app/Models/Tag.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import {BaseModel, BelongsTo, belongsTo, column} from '@ioc:Adonis/Lucid/Orm'
|
||||
import Translation from "App/Models/Translation";
|
||||
|
||||
export default class Tag extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@belongsTo(() => Translation, {
|
||||
foreignKey: 'labelId'
|
||||
})
|
||||
public label: BelongsTo<typeof Translation>
|
||||
|
||||
public labelId: number
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime
|
||||
}
|
||||
15
app/Validators/tag/TagStoreValidator.ts
Normal file
15
app/Validators/tag/TagStoreValidator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class TagStoreValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
label: schema.string()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required',
|
||||
}
|
||||
}
|
||||
15
app/Validators/tag/TagUpdateValidator.ts
Normal file
15
app/Validators/tag/TagUpdateValidator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class TagUpdateValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
label: schema.string.optional()
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required',
|
||||
}
|
||||
}
|
||||
21
database/migrations/1629146001824_tags.ts
Normal file
21
database/migrations/1629146001824_tags.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||
|
||||
export default class Tags extends BaseSchema {
|
||||
protected tableName = 'tags'
|
||||
|
||||
public async up () {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id').primary()
|
||||
table
|
||||
.integer('label_id')
|
||||
.unsigned()
|
||||
.references('translations.id')
|
||||
.onDelete('CASCADE')
|
||||
table.timestamps(true, true)
|
||||
})
|
||||
}
|
||||
|
||||
public async down () {
|
||||
this.schema.dropTable(this.tableName)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user