mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-29 02:50:27 +01:00
Add tags pivot in projects
This commit is contained in:
@@ -10,7 +10,8 @@ export default class ProjectsController {
|
|||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
projects: await Project.query()
|
projects: await Project.query()
|
||||||
.orderBy('id', 'asc')
|
.orderBy('id', 'asc')
|
||||||
.preload('file')
|
.preload('cover')
|
||||||
|
.preload('tags')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,7 +20,8 @@ export default class ProjectsController {
|
|||||||
const project = await Project.create(data)
|
const project = await Project.create(data)
|
||||||
const cover = await File.findByOrFail('label', data.cover)
|
const cover = await File.findByOrFail('label', data.cover)
|
||||||
|
|
||||||
await project.related('file').associate(cover)
|
await project.related('cover').associate(cover)
|
||||||
|
await project.related('tags').sync(data.tags!)
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
project
|
project
|
||||||
})
|
})
|
||||||
@@ -27,7 +29,8 @@ export default class ProjectsController {
|
|||||||
|
|
||||||
public async show ({ params, response }: HttpContextContract) {
|
public async show ({ params, response }: HttpContextContract) {
|
||||||
const project = await Project.findOrFail(params.id)
|
const project = await Project.findOrFail(params.id)
|
||||||
await project.load('file')
|
await project.load('cover')
|
||||||
|
await project.load('tags')
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
project
|
project
|
||||||
})
|
})
|
||||||
@@ -39,7 +42,8 @@ export default class ProjectsController {
|
|||||||
const cover = await File.findBy('label', data.cover)
|
const cover = await File.findBy('label', data.cover)
|
||||||
|
|
||||||
await project.merge(data).save()
|
await project.merge(data).save()
|
||||||
if (cover) await project.related('file').associate(cover)
|
if (cover) await project.related('cover').associate(cover)
|
||||||
|
await project.related('tags').sync(data.tags!)
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
project
|
project
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -19,11 +19,13 @@ export default class Project extends BaseModel {
|
|||||||
@column()
|
@column()
|
||||||
public url: string
|
public url: string
|
||||||
|
|
||||||
@belongsTo(() => File)
|
@belongsTo(() => File, {
|
||||||
public file: BelongsTo<typeof File>
|
foreignKey: 'coverId'
|
||||||
|
})
|
||||||
|
public cover: BelongsTo<typeof File>
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public fileId: number
|
public coverId: number
|
||||||
|
|
||||||
@manyToMany(() => Tag)
|
@manyToMany(() => Tag)
|
||||||
public tags: ManyToMany<typeof Tag>
|
public tags: ManyToMany<typeof Tag>
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ export default class ProjectStoreValidator {
|
|||||||
description: schema.string(),
|
description: schema.string(),
|
||||||
progress: schema.number(),
|
progress: schema.number(),
|
||||||
url: schema.string(),
|
url: schema.string(),
|
||||||
cover: schema.string()
|
cover: schema.string(),
|
||||||
|
tags: schema.array.optional().members(schema.string())
|
||||||
})
|
})
|
||||||
|
|
||||||
public messages = {
|
public messages = {
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ export default class ProjectUpdateValidator {
|
|||||||
description: schema.string.optional(),
|
description: schema.string.optional(),
|
||||||
progress: schema.number.optional(),
|
progress: schema.number.optional(),
|
||||||
url: schema.string.optional(),
|
url: schema.string.optional(),
|
||||||
cover: schema.string.optional()
|
cover: schema.string.optional(),
|
||||||
|
tags: schema.array.optional().members(schema.string())
|
||||||
})
|
})
|
||||||
public messages = {
|
public messages = {
|
||||||
required: 'The field {{field}} is required'
|
required: 'The field {{field}} is required'
|
||||||
|
|||||||
Reference in New Issue
Block a user