Skip to content

Commit

Permalink
feat: Implement random tilawa selection and update DTO for optional r…
Browse files Browse the repository at this point in the history
…eciter ID #31
  • Loading branch information
the-sabra committed Feb 5, 2025
1 parent 4c03900 commit f0b5e17
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
14 changes: 3 additions & 11 deletions src/audio/audio.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConflictException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TilawaSurah } from 'src/surah/entities/tilawa-surah.entity';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { CreateAudioDto } from './dto/create-audio.dto';
import { FilterAudioDto } from './dto/filter-audio.dto';
import { ReciterService } from 'src/reciter/reciter.service';
Expand Down Expand Up @@ -65,20 +65,12 @@ export class AudioService {
throw new Error('Limit must be greater than 0');
}

const tilawat = await this.reciterService.getReciterTilawa(reciter_id);

if (!tilawat?.length) {
throw new Error('No tilawat found for this reciter');
}

// Use more efficient random selection for tilawa
const randomTilawaIndex = Math.floor(Math.random() * tilawat.length);
const randomTilawa = tilawat[randomTilawaIndex];
const randomTilawa = await this.reciterService.getRandomTilawa(reciter_id);

const audioRecords = await this.tilawaSurahRepo.find({
select: ['tilawa_id', 'url'],
where: {
tilawa_id: randomTilawa.id,
tilawa_id: In(randomTilawa.map((tilawa) => tilawa.id)),
},
relations: {
surah: true,
Expand Down
5 changes: 3 additions & 2 deletions src/audio/dto/random.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transform } from 'class-transformer';
import { IsNumber, IsPositive } from 'class-validator';
import { IsNumber, IsOptional, IsPositive } from 'class-validator';

export class RandomDto {
@IsNumber()
Expand All @@ -10,5 +10,6 @@ export class RandomDto {
@IsNumber()
@IsPositive()
@Transform(({ value }) => parseInt(value))
reciter_id: number;
@IsOptional()
reciter_id?: number;
}
21 changes: 20 additions & 1 deletion src/reciter/reciter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { CreateReciterDto } from './dto/create-reciter.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Reciter } from './entities/reciter.entity';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { Tilawa } from './entities/tilawa.entity';
import { AddTilawaDto } from './dto/add-tilawa.dto';
import { ReciterFilterDto } from './dto/reciter-filter.dto';
Expand Down Expand Up @@ -113,6 +113,25 @@ export class ReciterService {
return tilawa;
}

async getRandomTilawa(reciterId: number) {
const tilawa = await this.tilawaRepository.find({
where: {
reciter_id: reciterId || In([1, 2, 3, 4, 14, 6, 13, 15, 18]),
},
});

const size = 5;
const randomTilawas = [];
while (randomTilawas.length < Math.min(size, tilawa.length)) {
const randomTilawa = tilawa[Math.floor(Math.random() * tilawa.length)];
if (!randomTilawas.includes(randomTilawa)) {
randomTilawas.push(randomTilawa);
}
}

return randomTilawas;
}

addReciterTilawa(id: number, addTilawaDto: Omit<AddTilawaDto, 'reciter_id'>) {
const tilawa = this.tilawaRepository.create({
...addTilawaDto,
Expand Down

0 comments on commit f0b5e17

Please sign in to comment.