Refactor VocabService to improve direct property handling and enhance language loading
- Updated VocabService to calculate direct where properties after setting all direct properties, ensuring accurate query conditions. - Enhanced filtering of AND conditions to remove empty objects before assignment, improving query efficiency. - Added logic to load native language names for courses, ensuring accurate mapping of language IDs to names. - Improved comments for clarity on the new logic and its implications on course retrieval.
This commit is contained in:
@@ -612,15 +612,10 @@ export default class VocabService {
|
|||||||
// Kombiniere alle AND-Bedingungen
|
// Kombiniere alle AND-Bedingungen
|
||||||
// Wenn sowohl andConditions als auch direkte where-Eigenschaften existieren,
|
// Wenn sowohl andConditions als auch direkte where-Eigenschaften existieren,
|
||||||
// müssen sie kombiniert werden
|
// müssen sie kombiniert werden
|
||||||
const directWhereProps = Object.keys(where).filter(key => key !== Op.and && key !== Op.or);
|
// WICHTIG: directWhereProps muss NACH dem Setzen aller direkten Eigenschaften berechnet werden
|
||||||
|
const directWhereProps = Object.keys(where).filter(key => {
|
||||||
// Debug: Prüfe, ob direkte Eigenschaften gesetzt wurden
|
// Filtere Op.and und Op.or heraus (sind Symbol-Keys)
|
||||||
console.log(`[getCourses] Debug - Vor Kombination:`, {
|
return key !== Op.and && key !== Op.or && typeof key === 'string';
|
||||||
directWhereProps,
|
|
||||||
whereKeys: Object.keys(where),
|
|
||||||
andConditionsLength: andConditions.length,
|
|
||||||
languageId,
|
|
||||||
nativeLanguageId
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (andConditions.length > 0) {
|
if (andConditions.length > 0) {
|
||||||
@@ -632,15 +627,18 @@ export default class VocabService {
|
|||||||
delete where[key];
|
delete where[key];
|
||||||
}
|
}
|
||||||
andConditions.push(directWhere);
|
andConditions.push(directWhere);
|
||||||
console.log(`[getCourses] Debug - Direkte Eigenschaften zu andConditions hinzugefügt:`, directWhere);
|
|
||||||
}
|
}
|
||||||
where[Op.and] = andConditions;
|
// Entferne leere Objekte aus andConditions
|
||||||
console.log(`[getCourses] Debug - Finale andConditions:`, JSON.stringify(andConditions, null, 2));
|
const filteredConditions = andConditions.filter(cond => {
|
||||||
|
return cond && typeof cond === 'object' && Object.keys(cond).length > 0;
|
||||||
|
});
|
||||||
|
// Setze andConditions nur, wenn sie nicht leer sind
|
||||||
|
if (filteredConditions.length > 0) {
|
||||||
|
where[Op.and] = filteredConditions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Wenn nur direkte Eigenschaften existieren (undConditions.length === 0),
|
// Wenn nur direkte Eigenschaften existieren (andConditions.length === 0),
|
||||||
// bleiben sie in where (nichts zu tun, sie sind bereits dort)
|
// bleiben sie in where (nichts zu tun, sie sind bereits dort)
|
||||||
|
|
||||||
console.log(`[getCourses] Debug - Finale WHERE-Klausel:`, JSON.stringify(where, null, 2));
|
|
||||||
|
|
||||||
const courses = await VocabCourse.findAll({
|
const courses = await VocabCourse.findAll({
|
||||||
where,
|
where,
|
||||||
@@ -666,17 +664,37 @@ export default class VocabService {
|
|||||||
// Lade Sprachnamen für alle Kurse
|
// Lade Sprachnamen für alle Kurse
|
||||||
const languageIds = [...new Set(coursesData.map(c => c.languageId))];
|
const languageIds = [...new Set(coursesData.map(c => c.languageId))];
|
||||||
if (languageIds.length > 0) {
|
if (languageIds.length > 0) {
|
||||||
const [languages] = await sequelize.query(
|
const languages = await sequelize.query(
|
||||||
`SELECT id, name FROM community.vocab_language WHERE id IN (:languageIds)`,
|
`SELECT id, name FROM community.vocab_language WHERE id IN (:languageIds)`,
|
||||||
{
|
{
|
||||||
replacements: { languageIds },
|
replacements: { languageIds },
|
||||||
type: sequelize.QueryTypes.SELECT
|
type: sequelize.QueryTypes.SELECT
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const languageMap = new Map(languages.map(l => [l.id, l.name]));
|
if (Array.isArray(languages)) {
|
||||||
coursesData.forEach(c => {
|
const languageMap = new Map(languages.map(l => [l.id, l.name]));
|
||||||
c.languageName = languageMap.get(c.languageId) || null;
|
coursesData.forEach(c => {
|
||||||
});
|
c.languageName = languageMap.get(c.languageId) || null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lade Muttersprachen-Namen für alle Kurse
|
||||||
|
const nativeLanguageIds = [...new Set(coursesData.map(c => c.nativeLanguageId).filter(id => id !== null))];
|
||||||
|
if (nativeLanguageIds.length > 0) {
|
||||||
|
const nativeLanguages = await sequelize.query(
|
||||||
|
`SELECT id, name FROM community.vocab_language WHERE id IN (:nativeLanguageIds)`,
|
||||||
|
{
|
||||||
|
replacements: { nativeLanguageIds },
|
||||||
|
type: sequelize.QueryTypes.SELECT
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (Array.isArray(nativeLanguages)) {
|
||||||
|
const nativeLanguageMap = new Map(nativeLanguages.map(l => [l.id, l.name]));
|
||||||
|
coursesData.forEach(c => {
|
||||||
|
c.nativeLanguageName = c.nativeLanguageId ? nativeLanguageMap.get(c.nativeLanguageId) || null : null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return coursesData;
|
return coursesData;
|
||||||
|
|||||||
Reference in New Issue
Block a user