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

#2 #3 added more logic + in artists controller + started workig with albums.…

#2 #3 added more logic + in artists controller + started workig with albums. Added controllers and services to it. Currently testing for work
parent 67484438
import express, { Request, Response, Router } from "express";
import multer from "multer";
import shortid from "shortid";
import { config } from "../index.config";
import path from 'path'
import { albumsService, AlbumsService } from "../services/albumsService";
import { EStatuses } from "../enum/EStatuses";
const storage = multer.diskStorage({
destination(req, file, callback){
callback(null, config.filePath)
},
filename(req, file, callback){
callback(null, shortid.generate() + path.extname(file.originalname))
},
})
const upload = multer({storage})
export class AlbumsController {
private router: Router
private service: AlbumsService
constructor() {
this.router = express.Router()
this.service = albumsService
this.router.get('/', this.getAlbums)
this.router.post('/', upload.single('coverImage'), this.addAlbum)
}
getRouter () {
return this.router;
}
private getAlbums = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getAlbums()
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.send(response)
}
}
private addAlbum = async (req: Request, res: Response): Promise<void> => {
const albumDto = req.body
albumDto.coverImage = req.file ? req.file.filename : ''
const response = await this.service.addAlbum(albumDto)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.send(response)
}
}
}
...@@ -3,7 +3,7 @@ import multer from "multer"; ...@@ -3,7 +3,7 @@ import multer from "multer";
import shortid from "shortid"; import shortid from "shortid";
import { config } from "../index.config"; import { config } from "../index.config";
import path from 'path' import path from 'path'
import { artistsService, ArtistsService } from "../services/atistsService"; import { artistsService, ArtistsService } from "../services/artistsService";
import { EStatuses } from "../enum/EStatuses"; import { EStatuses } from "../enum/EStatuses";
const storage = multer.diskStorage({ const storage = multer.diskStorage({
...@@ -31,12 +31,16 @@ export class ArtistsController { ...@@ -31,12 +31,16 @@ export class ArtistsController {
return this.router; return this.router;
} }
private getArtists = async (req: Request, res: Response) => { private getArtists = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getArtists() const response = await this.service.getArtists()
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.send(response) res.send(response)
} }
}
private addArtist = async (req: Request, res: Response) => { private addArtist = async (req: Request, res: Response): Promise<void> => {
const artistDto = req.body const artistDto = req.body
artistDto.photo = req.file ? req.file.filename : '' artistDto.photo = req.file ? req.file.filename : ''
const response = await this.service.addArtist(artistDto) const response = await this.service.addArtist(artistDto)
......
...@@ -4,6 +4,7 @@ import cors from 'cors'; ...@@ -4,6 +4,7 @@ import cors from 'cors';
import { HealthCheckController } from "./controllers/healthCheck"; import { HealthCheckController } from "./controllers/healthCheck";
import { mongooseDB } from "./repository/mongooseDB"; import { mongooseDB } from "./repository/mongooseDB";
import { ArtistsController } from "./controllers/artistsController"; import { ArtistsController } from "./controllers/artistsController";
import { AlbumsController } from "./controllers/albumsController";
dotenv.config() dotenv.config()
...@@ -20,6 +21,7 @@ class App { ...@@ -20,6 +21,7 @@ class App {
try{ try{
this.app.use('/health-check', new HealthCheckController().getRouter()) this.app.use('/health-check', new HealthCheckController().getRouter())
this.app.use('/artists', new ArtistsController().getRouter()) this.app.use('/artists', new ArtistsController().getRouter())
this.app.use('/albums', new AlbumsController().getRouter())
this.app.listen(process.env.APP_PORT, () => { this.app.listen(process.env.APP_PORT, () => {
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`) console.log(`Server is running on http://localhost:${process.env.APP_PORT}`)
}) })
......
import { Document, ObjectId } from "mongoose" import { Document, ObjectId } from "mongoose"
import IArtist from "./IArtist";
export default interface IArtist extends Document{ export default interface IAlbum extends Document{
_id: ObjectId _id: ObjectId
title: string; title: string;
artist: IArtist['_id'] artist: IArtist['_id']
......
...@@ -23,4 +23,4 @@ const AlbumSchema = new Schema<IAlbum>({ ...@@ -23,4 +23,4 @@ const AlbumSchema = new Schema<IAlbum>({
} }
}) })
export const Album = mongoose.model<IAlbum>('Artist', AlbumSchema) export const Album = mongoose.model<IAlbum>('Album', AlbumSchema)
\ No newline at end of file \ No newline at end of file
import { EStatuses } from "../enum/EStatuses";
import IAlbum from "../interfaces/IAlbum";
import IResponse from "../interfaces/IResponse";
import IAlbumDto from "../interfaces/IAlbumDto";
import { Album } from "../models/Album";
import IArtist from "../interfaces/IArtist";
export class AlbumsService {
public getAlbums = async(): Promise<IResponse<IAlbum[] | null>> => {
try {
const data = await Album.find().populate('Artist')
const response: IResponse<IAlbum[]> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Albums 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 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> = {
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
}
}
}
export const albumsService = new AlbumsService()
...@@ -34,7 +34,7 @@ export class ArtistsService { ...@@ -34,7 +34,7 @@ export class ArtistsService {
const response: IResponse<IArtist> = { const response: IResponse<IArtist> = {
status: EStatuses.SUCCESS, status: EStatuses.SUCCESS,
result: data, result: data,
message: 'Artists found' message: 'Artist added'
} }
return response return response
} catch (err: unknown){ } catch (err: unknown){
......
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