import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Song } from './song.entity'; import { CreateSongDto } from './dto/create-song.dto'; import { UpdateSongDto } from './dto/update-song.dto'; @Injectable() export class SongService { constructor( @InjectRepository(Song) private songRepository: Repository ) {} findAll(): Promise { return this.songRepository.find(); } findOneById(id: number): Promise { return this.songRepository.findOneBy({ id: id }); } async create(createSongDto: CreateSongDto): Promise { const song = this.songRepository.create({ title: createSongDto.title, duration: createSongDto.duration }); const savedSong = await this.songRepository.save(song); return savedSong.id; } async update(id: number, updateSongDto: UpdateSongDto): Promise { if (id === updateSongDto.id) { const song = this.songRepository.findOneBy({ id: updateSongDto.id }); if (!song) return "Song not found"; await this.songRepository.update({ id: updateSongDto.id }, { title: updateSongDto.title, duration: updateSongDto.duration }); return "Song updated successfully"; } else { return "Song ID does not match posted data" } } async remove(id: number): Promise { await this.songRepository.delete(id); } }