Commit bc729e6b authored by Цой Данил's avatar Цой Данил 💬

#6 added all validations to models

parent 0fd51a75
......@@ -7,21 +7,25 @@ const AlbumSchema = new Schema<IAlbum>({
title:{
type: String,
require: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Album title should exist']
},
artist:{
type: Schema.Types.ObjectId,
ref: 'Artist',
required: true
minlength: 1,
required: [true, 'Artists name should exist']
},
releaseYear:{
type: Date,
required: true
required: [true, 'Release year of album should exist']
},
coverImage:{
type: String,
required: true
}
required: [true, 'Cover image should exist']
},
}, {versionKey: false})
export const Album = mongoose.model<IAlbum>('Album', AlbumSchema)
\ No newline at end of file
......@@ -7,16 +7,23 @@ const ArtistSchema = new Schema<IArtist>({
name:{
type: String,
require: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Artists name should exist']
},
photo:{
type: String,
require: true
require: true,
trim: true,
minlength: 1,
required: [true, 'Artists photo should exist']
},
information:{
type: String,
require: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Artists info should exist']
}
}, {versionKey: false})
......
......@@ -8,17 +8,21 @@ const TrackSchema = new Schema<ITrack>({
title:{
type: String,
require: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Track title should exist']
},
album:{
type: Schema.Types.ObjectId,
ref: 'Album',
required: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Album should exist']
},
length:{
type: Number,
required: true
minimum: 10,
required: [true, 'Track length should exist']
}
}, {versionKey: false})
......
......@@ -6,12 +6,12 @@ const TrackHistorySchema: Schema = new Schema<ITrackHistory>({
user:{
type: Schema.Types.ObjectId,
ref: 'User',
required: true
required: [true, 'User should exist']
},
track:{
type: Schema.Types.ObjectId,
ref: 'Track',
required: true
required: [true, 'Track should exist']
},
datetime:{
type: Date,
......
......@@ -7,14 +7,16 @@ import { v4 } from "uuid";
const UserSchema: Schema = new Schema<IUser>({
username: {
type: String,
required: true,
unique: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Username should exist']
},
password: {
type: String,
required: true,
trim: true
trim: true,
minlength: 1,
required: [true, 'Password should exist']
},
token: {
type: String,
......
......@@ -68,7 +68,6 @@ export class MongooseDB {
public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => {
try {
if (albumDto.title.trim() === '' || !albumDto.artist || !albumDto.releaseYear || !albumDto.coverImage) throw new Error('All fields should exist')
const album = new Album(albumDto)
const data = await album.save()
const response: IResponse<IAlbum> = {
......@@ -159,7 +158,7 @@ export class MongooseDB {
public addTrack = async(trackDto: ITrackDto): Promise<IResponse<ITrack | null>> => {
try {
if (trackDto.title.trim() === '' || !trackDto.album || !trackDto.length || trackDto.length <= 0) throw new Error('All fields should exist and have valid value')
if (trackDto.length <= 0) throw new Error('All fields should exist and have valid value')
const album = new Track(trackDto)
const data = await album.save()
const response: IResponse<ITrack> = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment