Skip to content

Commit

Permalink
refactor: Optimize audio retrieval by replacing raw SQL query with Ty…
Browse files Browse the repository at this point in the history
…peORM find method to get all data
  • Loading branch information
the-sabra committed Nov 14, 2024
1 parent f95126b commit 7d25ee3
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions src/audio/audio.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { ConflictException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TilawaSurah } from 'src/surah/entities/tilawa-surah.entity';
import { Repository } from 'typeorm';
Expand Down Expand Up @@ -37,42 +33,21 @@ export class AudioService {
tilawa_id = tilawa[0].id;
}

const result = await this.tilawaSurahRepo.query(
`SELECT ts.tilawa_id AS tilawa_id,
ts.surah_id AS surah_id,
ts.url AS url,
r.name_english AS reciter_name_english,
r.name_arabic AS reciter_name_arabic,
s.name_arabic AS surah_name_arabic,
s.name_complex AS surah_name_complex,
s.image AS surah_image
FROM tilawa_surah ts
LEFT JOIN tilawa t ON t.id = ts.tilawa_id
INNER JOIN reciter r ON r.id = t.reciter_id
LEFT JOIN surah s ON s.id = ts.surah_id
WHERE ts.surah_id = ? AND ts.tilawa_id = ?;`,
[surah_id, tilawa_id],
);

if (!result.length) {
throw new NotFoundException('Audio not found');
}

const audio = {
tilawa_id,
url: result[0].url,
surah: {
name_arabic: result[0].surah_name_arabic,
name_complex: result[0].surah_name_complex,
image: result[0].surah_image,
const audio = await this.tilawaSurahRepo.find({
select: ['tilawa_id', 'url'],
where: {
surah_id,
tilawa_id,
},
reciter: {
name_arabic: result[0].reciter_name_arabic,
name_english: result[0].reciter_name_english,
relations: {
surah: true,
tilawa: {
reciter: true,
},
},
};
});

return audio;
return audio[0];
}

getAudioLrc(filterAudioLrcDto: FilterAudioLrcDto) {
Expand Down

0 comments on commit 7d25ee3

Please sign in to comment.