48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const { Service } = require('@hapipal/schmervice');
|
||
|
const { parse } = require('json2csv');
|
||
|
|
||
|
|
||
|
module.exports = class MovieService extends Service {
|
||
|
|
||
|
async list() {
|
||
|
const { Movie } = this.server.models();
|
||
|
return Movie.query();
|
||
|
}
|
||
|
|
||
|
async create(payload) {
|
||
|
const { Movie } = this.server.models();
|
||
|
return Movie.query().insert(payload);
|
||
|
}
|
||
|
|
||
|
async get(id) {
|
||
|
const { Movie } = this.server.models();
|
||
|
return Movie.query().findById(id);
|
||
|
}
|
||
|
|
||
|
async update(id, payload) {
|
||
|
const { Movie } = this.server.models();
|
||
|
return Movie.query().patchAndFetchById(id, payload);
|
||
|
}
|
||
|
|
||
|
async delete(id) {
|
||
|
const { Movie } = this.server.models();
|
||
|
return Movie.query().deleteById(id);
|
||
|
}
|
||
|
|
||
|
async generateCsvData(movies) {
|
||
|
try {
|
||
|
// Définir les champs CSV à partir des propriétés des films
|
||
|
const fields = ['title', 'description', 'director', 'releaseDate'];
|
||
|
console.log(parse(movies, {fields}));
|
||
|
|
||
|
// Convertir la liste des films en données CSV
|
||
|
return parse(movies, {fields});
|
||
|
} catch (error) {
|
||
|
console.error('Erreur lors de la génération des données CSV :', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|