Some checks failed
Music Collection CI Workflow / test (./backend) (push) Successful in 30s
Music Collection CI Workflow / test (./frontend) (push) Successful in 35s
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-collection-backend, ./backend) (push) Successful in 51s
Music Collection CI Workflow / deploy (push) Has been cancelled
Music Collection CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/music-collection-frontend, ./frontend) (push) Has been cancelled
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { DeleteResult, Repository } from 'typeorm';
|
|
import { Album } from './album.entity';
|
|
import { CreateAlbumDto } from './dto/create-album.dto';
|
|
import { UpdateAlbumDto } from './dto/update-album.dto';
|
|
|
|
@Injectable()
|
|
export class AlbumService {
|
|
constructor(
|
|
@InjectRepository(Album)
|
|
private albumRepository: Repository<Album>
|
|
) {}
|
|
|
|
findAll(): Promise<Album[]> {
|
|
return this.albumRepository.find({
|
|
order: {
|
|
artist: "ASC",
|
|
title: "ASC"
|
|
}
|
|
});
|
|
}
|
|
|
|
findOneById(id: number): Promise<Album | null> {
|
|
return this.albumRepository.findOne({
|
|
where: {
|
|
id: id
|
|
},
|
|
relations: {
|
|
songs: true
|
|
}
|
|
});
|
|
}
|
|
|
|
async create(createAlbumDto: CreateAlbumDto): Promise<Album | null> {
|
|
const album = this.albumRepository.create({
|
|
title: createAlbumDto.title,
|
|
artist: createAlbumDto.artist,
|
|
genre: createAlbumDto.genre
|
|
});
|
|
const savedAlbum = await this.albumRepository.save(album);
|
|
return savedAlbum;
|
|
}
|
|
|
|
async update(id: number, updateAlbumDto: UpdateAlbumDto): Promise<Album | null> {
|
|
if (id == updateAlbumDto.id) {
|
|
const albumToUpdate = await this.albumRepository.findOneBy({ id: updateAlbumDto.id });
|
|
if (!albumToUpdate) {
|
|
console.error("AlbumService: update: Didn't find album: ", id);
|
|
return null;
|
|
}
|
|
|
|
await this.albumRepository.update({ id: updateAlbumDto.id }, {
|
|
title: updateAlbumDto.title,
|
|
artist: updateAlbumDto.artist,
|
|
genre: updateAlbumDto.genre
|
|
});
|
|
const album = await this.albumRepository.findOneBy({ id: updateAlbumDto.id });
|
|
return album;
|
|
} else {
|
|
console.error("AlbumService: update: IDs do not match", id, updateAlbumDto);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async remove(id: number): Promise<DeleteResult> {
|
|
return await this.albumRepository.delete(id);
|
|
}
|
|
}
|