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

#4 added basic services/models/interfaces to work with tracks

parent 55635b89
import { Document, ObjectId } from "mongoose" import IAlbum from "./IAlbum"
import IArtist from "./IArtist" import IArtist from "./IArtist"
export default interface IAlbumDto extends Document{ export default interface IAlbumDto{
title: string title: IAlbum['title']
artist: IArtist['_id'] artist: IArtist['_id']
releaseYear: Date releaseYear: IAlbum['releaseYear']
coverImage: File coverImage: File
} }
\ No newline at end of file
import { Types } from "mongoose";import IAlbum from "./IAlbum";
export default interface ITrack{
_id: Types.ObjectId
title: string
album: IAlbum['_id']
length: number
}
\ No newline at end of file
import ITrack from "./ITrack";
export default interface ITrackDto {
title: ITrack['title']
album: ITrack['album']
length: ITrack['length']
}
\ No newline at end of file
import mongoose from "mongoose";
import ITrack from "../interfaces/ITrack";
const Schema = mongoose.Schema
const TrackSchema = new Schema<ITrack>({
title:{
type: String,
require: true
},
album:{
type: Schema.Types.ObjectId,
ref: 'Album',
required: true
},
length:{
type: Number,
required: true
}
})
export const Track = mongoose.model<ITrack>('Track', TrackSchema)
\ No newline at end of file
import { Request } from "express";
import { EStatuses } from "../enum/EStatuses";
import IResponse from "../interfaces/IResponse";
import ITrack from "../interfaces/ITrack";
import ITrackDto from "../interfaces/ITrackDto";
import { Track } from "../models/Track";
export class TracksService {
public getTracks = async(req: Request): Promise<IResponse<ITrack[] | null>> => {
try{
let data
if (req.query.album){
data = await Track.find({album: req.query.album}).populate('album')
} else{
data = await Track.find().populate('album')
}
const response: IResponse<ITrack[] | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Tracks found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
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')
const album = new Track(trackDto)
const data = await album.save()
const response: IResponse<ITrack> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
}
\ No newline at end of file
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