Adding unique album validator
Some checks failed
Music Collection CI Workflow / test (./frontend) (push) Has been cancelled
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-collection-backend, ./backend) (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
Music Collection CI Workflow / deploy (push) Has been cancelled
Music Collection CI Workflow / test (./backend) (push) Has been cancelled
Some checks failed
Music Collection CI Workflow / test (./frontend) (push) Has been cancelled
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-collection-backend, ./backend) (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
Music Collection CI Workflow / deploy (push) Has been cancelled
Music Collection CI Workflow / test (./backend) (push) Has been cancelled
This commit is contained in:
parent
4fcc93f37d
commit
bcff3547ea
@ -1,5 +1,6 @@
|
|||||||
import { Entity, Column, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
|
import { Entity, Column, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
import { AlbumTitleUnique } from './middleware/uniqueAlbumTitle.middleware';
|
||||||
import { Song } from '../song/song.entity';
|
import { Song } from '../song/song.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
|
32
backend/src/album/middleware/uniqueAlbumTitle.middleware.ts
Normal file
32
backend/src/album/middleware/uniqueAlbumTitle.middleware.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
|
@ValidatorConstraint({ async: true })
|
||||||
|
export class UniqueAlbumTitle implements ValidatorConstraintInterface {
|
||||||
|
constructor(@InjectRepository(Album)
|
||||||
|
private albumRepository: Repository<Album>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
validate(albumTitle: string) {
|
||||||
|
return this.albumRepository.findOneBy({
|
||||||
|
where: {
|
||||||
|
id: title
|
||||||
|
}).then((albumTitle) => {
|
||||||
|
return albumTitle === undefined;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AlbumTitleUnique(validationOptions?: ValidationOptions) {
|
||||||
|
return function (object: object, propertyName: string) {
|
||||||
|
registerDecorator({
|
||||||
|
target: object.constructor,
|
||||||
|
propertyName: propertyName,
|
||||||
|
options: validationOptions,
|
||||||
|
constraints: [],
|
||||||
|
validator: AlbumTitleUnique,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user