feat: bootstrap NestJS backend with auth, stations, users, health-check, now-playing
This commit is contained in:
54
src/now-playing/now-playing.service.ts
Normal file
54
src/now-playing/now-playing.service.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { NowPlayingGateway } from './now-playing.gateway';
|
||||
|
||||
@Injectable()
|
||||
export class NowPlayingService {
|
||||
private readonly logger = new Logger(NowPlayingService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly gateway: NowPlayingGateway,
|
||||
) {}
|
||||
|
||||
async updateNowPlaying(
|
||||
stationId: string,
|
||||
data: { song: string; artist: string; coverUrl?: string },
|
||||
) {
|
||||
const nowPlaying = await this.prisma.nowPlaying.upsert({
|
||||
where: { stationId },
|
||||
create: {
|
||||
stationId,
|
||||
song: data.song,
|
||||
artist: data.artist,
|
||||
coverUrl: data.coverUrl,
|
||||
},
|
||||
update: {
|
||||
song: data.song,
|
||||
artist: data.artist,
|
||||
coverUrl: data.coverUrl,
|
||||
},
|
||||
});
|
||||
|
||||
this.gateway.broadcastNowPlaying(stationId, {
|
||||
song: data.song,
|
||||
artist: data.artist,
|
||||
coverUrl: data.coverUrl,
|
||||
updatedAt: nowPlaying.updatedAt,
|
||||
});
|
||||
|
||||
return nowPlaying;
|
||||
}
|
||||
|
||||
async getNowPlaying(stationId: string) {
|
||||
return this.prisma.nowPlaying.findUnique({
|
||||
where: { stationId },
|
||||
});
|
||||
}
|
||||
|
||||
async getAllNowPlaying() {
|
||||
return this.prisma.nowPlaying.findMany({
|
||||
include: { station: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user