feat: bootstrap NestJS backend with auth, stations, users, health-check, now-playing
This commit is contained in:
114
src/stations/stations.service.ts
Normal file
114
src/stations/stations.service.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateStationDto } from './dto/create-station.dto';
|
||||
import { UpdateStationDto } from './dto/update-station.dto';
|
||||
|
||||
@Injectable()
|
||||
export class StationsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(filters: {
|
||||
search?: string;
|
||||
source?: string;
|
||||
online?: boolean;
|
||||
}) {
|
||||
const where: any = {};
|
||||
|
||||
if (filters.source) {
|
||||
where.source = filters.source;
|
||||
}
|
||||
if (filters.online !== undefined) {
|
||||
where.isOnline = filters.online;
|
||||
}
|
||||
if (filters.search) {
|
||||
where.OR = [
|
||||
{ name: { contains: filters.search, mode: 'insensitive' } },
|
||||
{ genre: { contains: filters.search, mode: 'insensitive' } },
|
||||
{ tags: { has: filters.search } },
|
||||
];
|
||||
}
|
||||
|
||||
return this.prisma.station.findMany({
|
||||
where,
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
include: { nowPlaying: true },
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const station = await this.prisma.station.findUnique({
|
||||
where: { id },
|
||||
include: { nowPlaying: true },
|
||||
});
|
||||
if (!station) throw new NotFoundException('Station not found');
|
||||
return station;
|
||||
}
|
||||
|
||||
async findByStationId(stationId: number) {
|
||||
return this.prisma.station.findUnique({
|
||||
where: { stationId },
|
||||
});
|
||||
}
|
||||
|
||||
async create(dto: CreateStationDto) {
|
||||
return this.prisma.station.create({
|
||||
data: {
|
||||
stationId: dto.stationId,
|
||||
name: dto.name,
|
||||
prefix: dto.prefix,
|
||||
streamUrl: dto.streamUrl,
|
||||
coverUrl: dto.coverUrl,
|
||||
genre: dto.genre,
|
||||
tags: dto.tags ?? [],
|
||||
sortOrder: dto.sortOrder,
|
||||
source: dto.source,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateStationDto) {
|
||||
return this.prisma.station.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...dto,
|
||||
tags: dto.tags,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
return this.prisma.station.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async upsertMany(stations: CreateStationDto[]) {
|
||||
const results = [];
|
||||
for (const dto of stations) {
|
||||
const result = await this.prisma.station.upsert({
|
||||
where: { stationId: dto.stationId },
|
||||
update: {
|
||||
name: dto.name,
|
||||
prefix: dto.prefix,
|
||||
streamUrl: dto.streamUrl,
|
||||
coverUrl: dto.coverUrl,
|
||||
genre: dto.genre,
|
||||
tags: dto.tags ?? [],
|
||||
sortOrder: dto.sortOrder,
|
||||
source: dto.source,
|
||||
},
|
||||
create: {
|
||||
stationId: dto.stationId,
|
||||
name: dto.name,
|
||||
prefix: dto.prefix,
|
||||
streamUrl: dto.streamUrl,
|
||||
coverUrl: dto.coverUrl,
|
||||
genre: dto.genre,
|
||||
tags: dto.tags ?? [],
|
||||
sortOrder: dto.sortOrder,
|
||||
source: dto.source,
|
||||
},
|
||||
});
|
||||
results.push(result);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user