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

View File

@ -1,4 +1,5 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
@ -9,6 +10,7 @@ async function bootstrap() {
credentials: true, credentials: true,
allowedHeaders: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Observe', allowedHeaders: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Observe',
}); });
app.useGlobalPipes(new ValidationPipe());
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000);
} }
bootstrap(); 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 { SongService } from './song.service';
import { Song } from './song.entity'; import { Song } from './song.entity';
import { CreateSongDto } from './dto/create-song.dto'; import { CreateSongDto } from './dto/create-song.dto';
@ -20,11 +20,13 @@ export class SongController {
} }
@Post() @Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createSongDto: CreateSongDto): Promise<number> { async create(@Body() createSongDto: CreateSongDto): Promise<number> {
return this.songService.create(createSongDto); return this.songService.create(createSongDto);
} }
@Put(':id') @Put(':id')
@UsePipes(new ValidationPipe({ transform: true }))
async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise<string> { async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise<string> {
return this.songService.update(id, updateSongDto); return this.songService.update(id, updateSongDto);
} }

View File

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

View File

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