playlist websockets

This commit is contained in:
raiper34
2024-07-13 14:42:22 +02:00
parent 2f0e4626f8
commit 80a562c5cd
5 changed files with 25 additions and 18 deletions
@@ -1,7 +1,6 @@
import {Body, Controller, Get, Post} from '@nestjs/common';
import {PlaylistService} from "./playlist.service";
import {PlaylistModel} from "./playlist.model";
import {PlaylistEntity} from "./playlist.entity";
@Controller('playlist')
export class PlaylistController {
@@ -17,7 +16,7 @@ export class PlaylistController {
}
@Post()
async create(@Body() playlist: PlaylistModel): Promise<PlaylistEntity> {
return await this.service.create(playlist);
async create(@Body() playlist: PlaylistModel): Promise<void> {
await this.service.create(playlist);
}
}
+10 -4
View File
@@ -3,12 +3,17 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {PlaylistEntity} from "./playlist.entity";
import {TrackService} from "../track/track.service";
import {WebSocketGateway, WebSocketServer} from "@nestjs/websockets";
import {Server} from "socket.io";
const fetch = require('isomorphic-unfetch');
const { getData, getPreview, getTracks, getDetails } = require('spotify-url-info')(fetch);
@WebSocketGateway()
@Injectable()
export class PlaylistService {
@WebSocketServer() io: Server;
constructor(
@InjectRepository(PlaylistEntity)
private repository: Repository<PlaylistEntity>,
@@ -27,9 +32,10 @@ export class PlaylistService {
await this.repository.delete(id);
}
async create(playlist: PlaylistEntity): Promise<PlaylistEntity> {
async create(playlist: PlaylistEntity): Promise<void> {
const details = await getDetails(playlist.spotifyUrl);
const savedPlaylist = await this.repository.save({...playlist, name: details.preview.title});
this.io.emit('playlistNew', savedPlaylist);
for(let track of details.tracks) {
await this.trackService.create({
artist: track.artist,
@@ -37,10 +43,10 @@ export class PlaylistService {
spotifyUrl: track.previewUrl,
}, savedPlaylist);
}
return savedPlaylist;
}
async update(id: number, track: PlaylistEntity): Promise<void> {
await this.repository.update(id, track);
async update(id: number, playlist: PlaylistEntity): Promise<void> {
await this.repository.update(id, playlist);
this.io.emit('trackPlaylist', playlist);
}
}
+1 -1
View File
@@ -49,7 +49,7 @@ export class TrackService {
async update(id: number, track: TrackEntity): Promise<void> {
await this.repository.update(id, track);
this.io.emit('track', track);
this.io.emit('trackUpdate', track);
}
@Interval(1000)