music-collection/backend/src/song/song.service.ts
Phill Pover daeb697086
Some checks failed
Music Collection CI Workflow / test (./backend) (push) Successful in 28s
Music Collection CI Workflow / test (./frontend) (push) Failing after 28s
Music Collection CI Workflow / deploy (push) Has been skipped
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/msuic-collection-backend, ./backend) (push) Has been skipped
Music Collection CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/music-collection-frontend, ./frontend) (push) Has been skipped
Fixing tests
2025-04-06 22:59:58 +01:00

53 lines
1.5 KiB
TypeScript

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<Song>
) {}
findAll(): Promise<Song[]> {
return this.songRepository.find();
}
findOneById(id: number): Promise<Song | null> {
return this.songRepository.findOneBy({ id: id });
}
async create(createSongDto: CreateSongDto): Promise<number> {
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<string> {
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<void> {
await this.songRepository.delete(id);
}
}