Fixing song create and update
Some checks failed
Music Collection CI Workflow / test (./backend) (push) Failing after 37s
Music Collection CI Workflow / test (./frontend) (push) Successful in 34s
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-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
Music Collection CI Workflow / deploy (push) Has been skipped

This commit is contained in:
Phill Pover 2025-04-07 00:46:36 +01:00
parent a37a23e5d3
commit 8c8248f8c0
5 changed files with 34 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { AlbumService } from './album.service';
import { Album } from './album.entity';
import { CreateAlbumDto } from './dto/create-album.dto';
@ -20,11 +20,13 @@ export class AlbumController {
}
@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createAlbumDto: CreateAlbumDto): Promise<number> {
return this.albumService.create(createAlbumDto);
}
@Put(':id')
@UsePipes(new ValidationPipe({ transform: true }))
async update(@Param('id') id: number, @Body() updateAlbumDto: UpdateAlbumDto): Promise<string> {
return this.albumService.update(id, updateAlbumDto);
}

View File

@ -1,4 +1,5 @@
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';
async function bootstrap() {
@ -9,6 +10,7 @@ async function bootstrap() {
credentials: true,
allowedHeaders: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Observe',
});
app.useGlobalPipes(new ValidationPipe());
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();

View File

@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { SongService } from './song.service';
import { Song } from './song.entity';
import { CreateSongDto } from './dto/create-song.dto';
@ -20,11 +20,13 @@ export class SongController {
}
@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createSongDto: CreateSongDto): Promise<number> {
return this.songService.create(createSongDto);
}
@Put(':id')
@UsePipes(new ValidationPipe({ transform: true }))
async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise<string> {
return this.songService.update(id, updateSongDto);
}

View File

@ -15,6 +15,6 @@ export class Song {
@Column()
trackNumber: number
@ManyToOne(() => Album, (album) => album.songs)
@ManyToOne(() => Album, (album) => album.songs, { cascade: true })
album: Album
}

View File

@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Album } from '../album/album.entity';
import { Song } from './song.entity';
import { CreateSongDto } from './dto/create-song.dto';
import { UpdateSongDto } from './dto/update-song.dto';
@ -9,7 +10,9 @@ import { UpdateSongDto } from './dto/update-song.dto';
export class SongService {
constructor(
@InjectRepository(Song)
private songRepository: Repository<Song>
private songRepository: Repository<Song>,
@InjectRepository(Album)
private albumRepository: Repository<Album>
) {}
findAll(): Promise<Song[]> {
@ -25,26 +28,33 @@ export class SongService {
}
async create(createSongDto: CreateSongDto): Promise<number> {
const song = this.songRepository.create({
title: createSongDto.title,
duration: createSongDto.duration,
trackNumber: createSongDto.trackNumber
});
const savedSong = await this.songRepository.save(song);
return savedSong.id;
const album = await this.albumRepository.findOneBy({id: createSongDto.albumId});
if (album) {
const song = new Song();
song.title = createSongDto.title;
song.duration = createSongDto.duration;
song.trackNumber = createSongDto.trackNumber;
song.album = album;
const savedSong = await this.songRepository.save(song);
return savedSong.id;
} else {
throw new Error(`Unable to find Album with ID ${createSongDto.albumId}`);
}
}
async update(id: number, updateSongDto: UpdateSongDto): Promise<string> {
if (id === updateSongDto.id) {
const song = this.songRepository.findOneBy({ id: updateSongDto.id });
if (!song)
const album = await this.albumRepository.findOneBy({id: updateSongDto.albumId});
const songToUpdate = await this.songRepository.findOneBy({ id: updateSongDto.id });
if (!songToUpdate || !album)
return "Song not found";
await this.songRepository.update({ id: updateSongDto.id }, {
title: updateSongDto.title,
duration: updateSongDto.duration,
trackNumber: updateSongDto.trackNumber
});
songToUpdate.id = updateSongDto.id;
songToUpdate.title = updateSongDto.title;
songToUpdate.duration = updateSongDto.duration;
songToUpdate.trackNumber = updateSongDto.trackNumber;
songToUpdate.album = album;
await this.songRepository.update({ id: updateSongDto.id }, songToUpdate);
return "Song updated successfully";
} else {
return "Song ID does not match posted data"