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

#6 added all validations to models

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