Skip to content

Commit 3832306

Browse files
committed
refactor: simplify parameter formatting in SongService methods for improved readability
1 parent 5b4efc9 commit 3832306

File tree

1 file changed

+20
-91
lines changed

1 file changed

+20
-91
lines changed

apps/backend/src/song/song.service.ts

Lines changed: 20 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@ export class SongService {
3333
});
3434
}
3535

36-
public async uploadSong({
37-
file,
38-
user,
39-
body,
40-
}: {
41-
body: UploadSongDto;
42-
file: Express.Multer.File;
43-
user: UserDocument;
44-
}): Promise<UploadSongResponseDto> {
36+
public async uploadSong({ file, user, body,}: { body: UploadSongDto; file: Express.Multer.File; user: UserDocument;}): Promise<UploadSongResponseDto> {
4537
const song = await this.songUploadService.processUploadedSong({
4638
file,
4739
user,
@@ -69,10 +61,7 @@ export class SongService {
6961
return UploadSongResponseDto.fromSongWithUserDocument(populatedSong);
7062
}
7163

72-
public async deleteSong(
73-
publicId: string,
74-
user: UserDocument,
75-
): Promise<UploadSongResponseDto> {
64+
public async deleteSong( publicId: string, user: UserDocument,): Promise<UploadSongResponseDto> {
7665
const foundSong = await this.songModel
7766
.findOne({ publicId: publicId })
7867
.exec();
@@ -99,11 +88,7 @@ export class SongService {
9988
return UploadSongResponseDto.fromSongWithUserDocument(populatedSong);
10089
}
10190

102-
public async patchSong(
103-
publicId: string,
104-
body: UploadSongDto,
105-
user: UserDocument,
106-
): Promise<UploadSongResponseDto> {
91+
public async patchSong( publicId: string, body: UploadSongDto, user: UserDocument,): Promise<UploadSongResponseDto> {
10792
const foundSong = await this.songModel.findOne({
10893
publicId: publicId,
10994
});
@@ -192,10 +177,7 @@ export class SongService {
192177
return songs.map((song) => SongPreviewDto.fromSongDocumentWithUser(song));
193178
}
194179

195-
public async searchSongs(
196-
query: PageQueryDTO,
197-
q: string,
198-
): Promise<SongPreviewDto[]> {
180+
public async searchSongs( query: PageQueryDTO, q: string,): Promise<SongPreviewDto[]> {
199181
const page = parseInt(query.page?.toString() ?? '1');
200182
const limit = parseInt(query.limit?.toString() ?? '10');
201183
const order = query.order ? query.order : false;
@@ -234,10 +216,7 @@ export class SongService {
234216
return songs.map((song) => SongPreviewDto.fromSongDocumentWithUser(song));
235217
}
236218

237-
public async getRecentSongs(
238-
page: number,
239-
limit: number,
240-
): Promise<SongPreviewDto[]> {
219+
public async getRecentSongs( page: number, limit: number,): Promise<SongPreviewDto[]> {
241220
const queryObject: any = {
242221
visibility: 'public',
243222
};
@@ -269,9 +248,7 @@ export class SongService {
269248
.exec();
270249
}
271250

272-
public async getSongsBeforeTimespan(
273-
timespan: number,
274-
): Promise<SongWithUser[]> {
251+
public async getSongsBeforeTimespan(timespan: number,): Promise<SongWithUser[]> {
275252
return this.songModel
276253
.find<SongWithUser>({
277254
visibility: 'public',
@@ -285,10 +262,7 @@ export class SongService {
285262
.exec();
286263
}
287264

288-
public async getSong(
289-
publicId: string,
290-
user: UserDocument | null,
291-
): Promise<SongViewDto> {
265+
public async getSong( publicId: string, user: UserDocument | null,): Promise<SongViewDto> {
292266
const foundSong = await this.songModel.findOne({ publicId: publicId });
293267

294268
if (!foundSong) {
@@ -318,12 +292,7 @@ export class SongService {
318292
}
319293

320294
// TODO: service should not handle HTTP -> https://www.reddit.com/r/node/comments/uoicw1/should_i_return_status_code_from_service_layer/
321-
public async getSongDownloadUrl(
322-
publicId: string,
323-
user: UserDocument | null,
324-
src?: string,
325-
packed: boolean = false,
326-
): Promise<string> {
295+
public async getSongDownloadUrl( publicId: string, user: UserDocument | null, src?: string, packed: boolean = false,): Promise<string> {
327296
const foundSong = await this.songModel.findOne({ publicId: publicId });
328297

329298
if (!foundSong) {
@@ -368,13 +337,7 @@ export class SongService {
368337
}
369338
}
370339

371-
public async getMySongsPage({
372-
query,
373-
user,
374-
}: {
375-
query: PageQueryDTO;
376-
user: UserDocument;
377-
}): Promise<SongPageDto> {
340+
public async getMySongsPage({ query, user,}: { query: PageQueryDTO; user: UserDocument;}): Promise<SongPageDto> {
378341
const page = parseInt(query.page?.toString() ?? '1');
379342
const limit = parseInt(query.limit?.toString() ?? '10');
380343
const order = query.order ? query.order : false;
@@ -404,10 +367,7 @@ export class SongService {
404367
};
405368
}
406369

407-
public async getSongEdit(
408-
publicId: string,
409-
user: UserDocument,
410-
): Promise<UploadSongDto> {
370+
public async getSongEdit( publicId: string, user: UserDocument,): Promise<UploadSongDto> {
411371
const foundSong = await this.songModel
412372
.findOne({ publicId: publicId })
413373
.exec();
@@ -455,11 +415,7 @@ export class SongService {
455415
}, {} as Record<string, number>);
456416
}
457417

458-
public async getSongsByCategory(
459-
category: string,
460-
page: number,
461-
limit: number,
462-
): Promise<SongPreviewDto[]> {
418+
public async getSongsByCategory( category: string, page: number, limit: number,): Promise<SongPreviewDto[]> {
463419
const songs = (await this.songModel
464420
.find({
465421
category : category,
@@ -474,10 +430,7 @@ export class SongService {
474430
return songs.map((song) => SongPreviewDto.fromSongDocumentWithUser(song));
475431
}
476432

477-
public async getRandomSongs(
478-
count: number,
479-
category: string,
480-
): Promise<SongPreviewDto[]> {
433+
public async getRandomSongs( count: number, category: string,): Promise<SongPreviewDto[]> {
481434
const songs = (await this.songModel
482435
.aggregate([
483436
{
@@ -513,15 +466,8 @@ export class SongService {
513466
year : new Date(Date.now()).setFullYear(now.getFullYear() - 1),
514467
all : new Date(0).getTime(),
515468
};
516-
517-
const songs: Record<TimespanType, SongWithUser[]> = {
518-
hour : [],
519-
day : [],
520-
week : [],
521-
month: [],
522-
year : [],
523-
all : [],
524-
};
469+
470+
const songs: Record<TimespanType, SongWithUser[]> = { hour: [], day: [], week: [], month: [], year: [], all: [],};
525471

526472
for (const [timespan, time] of Object.entries(times)) {
527473
const songPage = await this.getSongsForTimespan(time);
@@ -547,29 +493,12 @@ export class SongService {
547493

548494
const featuredSongs = FeaturedSongsDto.create();
549495

550-
featuredSongs.hour = songs.hour.map((song) =>
551-
SongPreviewDto.fromSongDocumentWithUser(song),
552-
);
553-
554-
featuredSongs.day = songs.day.map((song) =>
555-
SongPreviewDto.fromSongDocumentWithUser(song),
556-
);
557-
558-
featuredSongs.week = songs.week.map((song) =>
559-
SongPreviewDto.fromSongDocumentWithUser(song),
560-
);
561-
562-
featuredSongs.month = songs.month.map((song) =>
563-
SongPreviewDto.fromSongDocumentWithUser(song),
564-
);
565-
566-
featuredSongs.year = songs.year.map((song) =>
567-
SongPreviewDto.fromSongDocumentWithUser(song),
568-
);
569-
570-
featuredSongs.all = songs.all.map((song) =>
571-
SongPreviewDto.fromSongDocumentWithUser(song),
572-
);
496+
featuredSongs.hour = songs.hour.map((song) => SongPreviewDto.fromSongDocumentWithUser(song),);
497+
featuredSongs.day = songs.day.map((song) => SongPreviewDto.fromSongDocumentWithUser(song), );
498+
featuredSongs.week = songs.week.map((song) => SongPreviewDto.fromSongDocumentWithUser(song),);
499+
featuredSongs.month = songs.month.map((song) => SongPreviewDto.fromSongDocumentWithUser(song),);
500+
featuredSongs.year = songs.year.map((song) => SongPreviewDto.fromSongDocumentWithUser(song),);
501+
featuredSongs.all = songs.all.map((song) => SongPreviewDto.fromSongDocumentWithUser(song),);
573502

574503
return featuredSongs;
575504
}

0 commit comments

Comments
 (0)