mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-23 16:30:27 +01:00
Added Experiences and Formations
This commit is contained in:
60
app/Controllers/Http/ExperiencesController.ts
Normal file
60
app/Controllers/Http/ExperiencesController.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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 ExperienceUpdateValidator from "App/Validators/experience/ExperienceUpdateValidator";
|
||||
|
||||
export default class ExperiencesController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
const experiences = await Experience
|
||||
.query()
|
||||
.orderBy('begin_date', 'desc')
|
||||
.preload('title')
|
||||
return response.status(200).send({
|
||||
experiences: experiences
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(ExperienceStoreValidator)
|
||||
const experience = await Experience.create(data)
|
||||
await experience.related('title').associate(await getTranslation(data.title))
|
||||
|
||||
return response.status(200).send({
|
||||
experience: experience
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const experience = await Experience.findOrFail(params.id)
|
||||
experience.load('title')
|
||||
return response.status(200).send({
|
||||
experience
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(ExperienceUpdateValidator)
|
||||
const experience = await Experience.findOrFail(params.id)
|
||||
|
||||
if (data.title) {
|
||||
await experience.related('title').associate(await getTranslation(data.title))
|
||||
}
|
||||
|
||||
await experience.merge(data).save()
|
||||
|
||||
return response.status(200).send({
|
||||
experience
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
const experience = await Experience.findOrFail(params.id)
|
||||
await experience.delete()
|
||||
return response.status(200).send({
|
||||
message: 'Experience successfully deleted!'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
68
app/Controllers/Http/FormationsController.ts
Normal file
68
app/Controllers/Http/FormationsController.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import getTranslation from "App/Tasks/getTranslation";
|
||||
import FormationStoreValidator from "App/Validators/formation/FormationStoreValidator";
|
||||
import FormationUpdateValidator from "App/Validators/formation/FormationUpdateValidator";
|
||||
import Formation from "App/Models/Formation";
|
||||
|
||||
export default class FormationsController {
|
||||
|
||||
public async index ({ response }: HttpContextContract) {
|
||||
const formations = await Formation
|
||||
.query()
|
||||
.orderBy('begin_date', 'desc')
|
||||
.preload('title')
|
||||
.preload('description')
|
||||
return response.status(200).send({
|
||||
formations: formations
|
||||
})
|
||||
}
|
||||
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(FormationStoreValidator)
|
||||
const formation = await Formation.create(data)
|
||||
|
||||
await formation.related('title').associate(await getTranslation(data.title))
|
||||
await formation.related('description').associate(await getTranslation(data.description))
|
||||
|
||||
return response.status(200).send({
|
||||
formation: formation
|
||||
})
|
||||
}
|
||||
|
||||
public async show ({ params, response }: HttpContextContract) {
|
||||
const formation = await Formation.findOrFail(params.id)
|
||||
formation.load('title')
|
||||
formation.load('description')
|
||||
return response.status(200).send({
|
||||
formation
|
||||
})
|
||||
}
|
||||
|
||||
public async update ({ request, params, response }: HttpContextContract) {
|
||||
const data = await request.validate(FormationUpdateValidator)
|
||||
const formation = await Formation.findOrFail(params.id)
|
||||
|
||||
if (data.title) {
|
||||
await formation.related('title').associate(await getTranslation(data.title))
|
||||
}
|
||||
|
||||
if (data.description) {
|
||||
await formation.related('description').associate(await getTranslation(data.description))
|
||||
}
|
||||
|
||||
await formation.merge(data).save()
|
||||
|
||||
return response.status(200).send({
|
||||
formation
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy ({ response, params }: HttpContextContract) {
|
||||
const formation = await Formation.findOrFail(params.id)
|
||||
await formation.delete()
|
||||
return response.status(200).send({
|
||||
message: 'Formation successfully deleted!'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user