mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
add Skill
This commit is contained in:
@@ -43,7 +43,7 @@ export default class AnnouncesController {
|
||||
})
|
||||
}
|
||||
|
||||
public async update({ request, params, response }: HttpContextContract) {
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(AnnounceUpdateValidator)
|
||||
const announce = await Announce.findOrFail(params.id)
|
||||
|
||||
@@ -54,6 +54,7 @@ export default class AnnouncesController {
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await announce.related('file').associate(cover)
|
||||
await announce.merge(data).save()
|
||||
|
||||
return response.status(200).send({
|
||||
announce
|
||||
|
||||
59
app/Controllers/Http/SkillsController.ts
Normal file
59
app/Controllers/Http/SkillsController.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import File from "App/Models/File";
|
||||
import Skill from "App/Models/Skill";
|
||||
import SkillStoreValidator from "App/Validators/skill/SkillStoreValidator";
|
||||
import SkillUpdateValidator from "App/Validators/skill/SkillUpdateValidator";
|
||||
|
||||
export default class SkillsController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
const skills = await Skill
|
||||
.query()
|
||||
.preload('file')
|
||||
return response.status(200).send({
|
||||
skills: skills
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(SkillStoreValidator)
|
||||
const skill = await Skill.create(data)
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await skill.related('file').associate(cover)
|
||||
|
||||
return response.status(200).send({
|
||||
skill: skill
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const skill = await Skill.findOrFail(params.id)
|
||||
skill.load('file')
|
||||
return response.status(200).send({
|
||||
skill
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(SkillUpdateValidator)
|
||||
const skill = await Skill.findOrFail(params.id)
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await skill.related('file').associate(cover)
|
||||
await skill.merge(data).save()
|
||||
|
||||
return response.status(200).send({
|
||||
skill
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
const skill = await Skill.findOrFail(params.id)
|
||||
await skill.delete()
|
||||
return response.status(200).send({
|
||||
message: 'Skill successfully deleted!'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
23
app/Models/Skill.ts
Normal file
23
app/Models/Skill.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import {BaseModel, BelongsTo, belongsTo, column} from '@ioc:Adonis/Lucid/Orm'
|
||||
import File from "App/Models/File";
|
||||
|
||||
export default class Skill extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column()
|
||||
public name: string
|
||||
|
||||
@belongsTo(() => File)
|
||||
public file: BelongsTo<typeof File>
|
||||
|
||||
@column()
|
||||
public fileId: number
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||
public updatedAt: DateTime
|
||||
}
|
||||
15
app/Validators/skill/SkillStoreValidator.ts
Normal file
15
app/Validators/skill/SkillStoreValidator.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class SkillStoreValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
name: schema.string(),
|
||||
cover: schema.string(),
|
||||
})
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
}
|
||||
16
app/Validators/skill/SkillUpdateValidator.ts
Normal file
16
app/Validators/skill/SkillUpdateValidator.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
|
||||
export default class SkillUpdateValidator {
|
||||
constructor (protected ctx: HttpContextContract) {
|
||||
}
|
||||
|
||||
public schema = schema.create({
|
||||
name: schema.string.optional(),
|
||||
cover: schema.string.optional(),
|
||||
})
|
||||
|
||||
public messages = {
|
||||
required: 'The field {{field}} is required'
|
||||
}
|
||||
}
|
||||
22
database/migrations/1628936238073_skills.ts
Normal file
22
database/migrations/1628936238073_skills.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
||||
|
||||
export default class Skills extends BaseSchema {
|
||||
protected tableName = 'skills'
|
||||
|
||||
public async up () {
|
||||
this.schema.createTable(this.tableName, (table) => {
|
||||
table.increments('id')
|
||||
table.string('name')
|
||||
table
|
||||
.integer('file_id')
|
||||
.unsigned()
|
||||
.references('files.id')
|
||||
.onDelete('CASCADE')
|
||||
table.timestamps(true, true)
|
||||
})
|
||||
}
|
||||
|
||||
public async down () {
|
||||
this.schema.dropTable(this.tableName)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ Route.group(() => {
|
||||
|
||||
Route.resource('/announces', 'AnnouncesController').except(['edit', 'create'])
|
||||
|
||||
Route.resource('/skills', 'SkillsController').except(['edit', 'create'])
|
||||
|
||||
Route.resource('/projects', 'ProjectsController').except(['edit', 'create'])
|
||||
|
||||
Route.resource('/profile', 'ProfilesController').only(['index', 'update'])
|
||||
|
||||
Reference in New Issue
Block a user