mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-18 22:11:34 +01:00
Working and optimise
This commit is contained in:
@@ -22,9 +22,7 @@ export default class AnnouncesController {
|
||||
public async store ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(AnnounceStoreValidator)
|
||||
const announce = await Announce.create(data)
|
||||
|
||||
const translation = await getTranslation(data.code)
|
||||
await announce.related('message').associate(translation)
|
||||
await announce.related('message').associate(await getTranslation(data.code))
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await announce.related('cover').associate(cover)
|
||||
@@ -48,12 +46,12 @@ export default class AnnouncesController {
|
||||
const announce = await Announce.findOrFail(params.id)
|
||||
|
||||
if (data.code) {
|
||||
const translation = await getTranslation(data.code)
|
||||
await announce.related('message').associate(translation)
|
||||
await announce.related('message').associate(await getTranslation(data.code))
|
||||
}
|
||||
|
||||
const cover = await File.findBy('label', data.cover)
|
||||
if (cover) await announce.related('cover').associate(cover)
|
||||
|
||||
await announce.merge(data).save()
|
||||
|
||||
return response.status(200).send({
|
||||
|
||||
@@ -21,7 +21,9 @@ export default class Announce extends BaseModel {
|
||||
@column()
|
||||
public messageId: number
|
||||
|
||||
@belongsTo(() => File)
|
||||
@belongsTo(() => File, {
|
||||
foreignKey: 'coverId'
|
||||
})
|
||||
public cover: BelongsTo<typeof File>
|
||||
|
||||
@column()
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import Translation from "App/Models/Translation";
|
||||
|
||||
export default async function getTranslation(code: string): Promise<Translation> {
|
||||
let translation = await Translation.findBy('code', code)
|
||||
if (!translation) {
|
||||
translation = await Translation.create({code: code})
|
||||
}
|
||||
return translation
|
||||
return await Translation.firstOrNew({code}, {code})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user