playlist websockets
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user