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

#1 added functions to post new artists/Cheked basic validations

parent 250bc1a7
......@@ -11,6 +11,7 @@
"dependencies": {
"@types/cors": "^2.8.13",
"@types/mongoose": "^5.11.97",
"@types/shortid": "^0.0.29",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
......@@ -21,6 +22,7 @@
"pg-hstore": "^2.3.4",
"sequelize": "^6.29.3",
"sequelize-typescript": "^2.1.5",
"shortid": "^2.2.16",
"uuid": "^9.0.0"
},
"devDependencies": {
......@@ -228,6 +230,11 @@
"@types/node": "*"
}
},
"node_modules/@types/shortid": {
"version": "0.0.29",
"resolved": "https://registry.npmjs.org/@types/shortid/-/shortid-0.0.29.tgz",
"integrity": "sha512-9BCYD9btg2CY4kPcpMQ+vCR8U6V8f/KvixYD5ZbxoWlkhddNF5IeZMVL3p+QFUkg+Hb+kPAG9Jgk4bnnF1v/Fw=="
},
"node_modules/@types/strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz",
......@@ -1194,6 +1201,11 @@
"mkdirp": "bin/cmd.js"
}
},
"node_modules/nanoid": {
"version": "2.1.11",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.11.tgz",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
},
"node_modules/negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
......@@ -1773,6 +1785,14 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
},
"node_modules/shortid": {
"version": "2.2.16",
"resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.16.tgz",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"dependencies": {
"nanoid": "^2.1.0"
}
},
"node_modules/side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
......@@ -2372,6 +2392,11 @@
"@types/node": "*"
}
},
"@types/shortid": {
"version": "0.0.29",
"resolved": "https://registry.npmjs.org/@types/shortid/-/shortid-0.0.29.tgz",
"integrity": "sha512-9BCYD9btg2CY4kPcpMQ+vCR8U6V8f/KvixYD5ZbxoWlkhddNF5IeZMVL3p+QFUkg+Hb+kPAG9Jgk4bnnF1v/Fw=="
},
"@types/strip-bom": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz",
......@@ -3099,6 +3124,11 @@
}
}
},
"nanoid": {
"version": "2.1.11",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.11.tgz",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
},
"negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
......@@ -3499,6 +3529,14 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
},
"shortid": {
"version": "2.2.16",
"resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.16.tgz",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"requires": {
"nanoid": "^2.1.0"
}
},
"side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
......
......@@ -12,6 +12,7 @@
"dependencies": {
"@types/cors": "^2.8.13",
"@types/mongoose": "^5.11.97",
"@types/shortid": "^0.0.29",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
......@@ -22,6 +23,7 @@
"pg-hstore": "^2.3.4",
"sequelize": "^6.29.3",
"sequelize-typescript": "^2.1.5",
"shortid": "^2.2.16",
"uuid": "^9.0.0"
},
"devDependencies": {
......
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 { artistsService, ArtistsService } from "../services/atistsService";
import IArtistDto from "../interfaces/IArtistDto";
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 ArtistsController {
private router: Router
private service: ArtistsService
constructor() {
this.router = express.Router()
this.service = artistsService
this.router.get('/', this.getArtists)
this.router.post('/', upload.single('photo'), this.addArtist)
}
getRouter () {
return this.router;
}
private getArtists = async (req: Request, res: Response) => {
const response = await this.service.getArtists()
res.send(response)
}
private addArtist = async (req: Request, res: Response) => {
const artistDto = req.body
artistDto.photo = req.file ? req.file.filename : ''
const response = await this.service.addArtist(artistDto)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.send(response)
}
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ import dotenv from 'dotenv';
import cors from 'cors';
import { HealthCheckController } from "./controllers/healthCheck";
import { mongooseDB } from "./repository/mongooseDB";
import { ArtistsController } from "./controllers/artistsController";
dotenv.config()
......@@ -18,6 +19,7 @@ class App {
public init = async(): Promise<void> => {
try{
this.app.use('/health-check', new HealthCheckController().getRouter())
this.app.use('/artists', new ArtistsController().getRouter())
this.app.listen(process.env.APP_PORT, () => {
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`)
})
......
import { EStatuses } from "../enum/EStatuses";
import IArtist from "../interfaces/IArtist";
import IArtistDto from "../interfaces/IArtistDto";
import IResponse from "../interfaces/IResponse";
import { Artist } from "../models/Artist";
......@@ -24,4 +25,28 @@ export class ArtistsService {
return response
}
}
public addArtist = async(artistDto: IArtistDto): Promise<IResponse<IArtist | null>> => {
try{
if (artistDto.name.trim() === '' || !artistDto.photo || artistDto.information.trim() === '') throw new Error ('All fields should exist')
const artist = new Artist(artistDto)
const data = await artist.save()
const response: IResponse<IArtist> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Artists 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
}
}
}
export const artistsService = new ArtistsService()
\ 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