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

Added users contollers + photos controllers + connected them to index and wrote all routes

parent 8ac0a8fb
import multer from "multer";
import shortid from "shortid";
import path from 'path'
import { config } from "../index.config";
import express, { Request, Response, Router } from "express";
import { PhotosService, photosService } from "../services/photosService";
import IModifiedRequest from "../interfaces/IModifiedRequest";
import IPhotoDto from "../interfaces/IPhotoDto";
import { EStatuses } from "../enum/EStatuses";
import { auth } from "../middlewares/auth";
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 PhotosController {
private router: Router
private service: PhotosService
constructor(){
this.router = express.Router()
this.service = photosService
this.router.get('/', this.getAllPhotos)
this.router.get('/:id', this.getPhotosByUserId)
this.router.post('/', [auth, upload.single('photo')], this.addPhoto)
this.router.delete('/:id',auth, this.deletePhotoById)
}
public getRouter() {
return this.router;
}
private getAllPhotos = async(req: Request, res: Response): Promise<void> => {
const response = await this.service.getAllPhotos()
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.status(200).send(response)
}
}
private addPhoto = async(req: Request, res: Response): Promise<void> => {
const modifiedReq = req as IModifiedRequest
if (typeof modifiedReq.verifiedData === 'object'){
const photoDto: IPhotoDto = {
user: modifiedReq.verifiedData._id,
title: req.body.title,
photo: req.file ? req.file.filename : '',
}
const response = await this.service.addPhoto(photoDto)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.status(200).send(response)
}
} else{
res.status(418).send('error in request. Some invalid field appeared')
}
}
private getPhotosByUserId = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getPhotosByUserId(req.params.id)
response.status === EStatuses.FAILURE ? res.status(418).send(response) : res.status(200).send(response)
}
private deletePhotoById = async (req: Request, res: Response): Promise<void> => {
const modifiedReq = req as IModifiedRequest
if (typeof modifiedReq.verifiedData === 'object'){
const response = await this.service.deletePhotoById(req.params.id, modifiedReq.verifiedData._id)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
res.status(200).send(response)
}
} else{
res.status(418).send('error in request. Some invalid field appeared')
}
}
}
export const photosController = new PhotosController()
\ No newline at end of file
import express, { Request, Response, Router } from "express"
import IResponse from "../interfaces/IResponse"
import IUserGetDto from "../interfaces/IUserGetDto"
import { EStatuses } from "../enum/EStatuses"
import { auth } from "../middlewares/auth"
import IModifiedRequest from "../interfaces/IModifiedRequest"
import { UsersService, usersService } from "../services/usersService"
export class UsersController {
private service: UsersService
private router: Router
constructor(){
this.service = usersService
this.router = express.Router()
this.router.post('/', this.createUser)
this.router.post('/login', this.loginUser)
this.router.get('/token', auth, this.checkToken)
}
public getRouter = () => {
return this.router
}
private createUser = async (req: Request, res: Response): Promise<void> => {
const response: IResponse<IUserGetDto | null> = await this.service.createUser(req.body)
res.status(200).send(response)
}
public loginUser = async(req: Request, res: Response): Promise<void> => {
const response: IResponse<IUserGetDto | null> = await this.service.loginUser(req.body)
res.status(200).send(response)
}
public checkToken = async (expressReq: Request, res: Response): Promise<void> => {
const req = expressReq as IModifiedRequest
const response: IResponse<IUserGetDto | null> = {
status: EStatuses.SUCCESS,
result: req.verifiedData as IUserGetDto,
message: 'Token is ok'
}
res.status(200).send(response)
}
}
export const usersController = new UsersController()
\ No newline at end of file
......@@ -3,6 +3,8 @@ import dotenv from 'dotenv';
import cors from 'cors';
import { healthCheckController } from "./controllers/healthCheckController";
import { mongooseDB } from "./repository/mongooseDB";
import { usersController } from "./controllers/usersController";
import { photosController } from "./controllers/photosController";
dotenv.config()
......@@ -18,6 +20,8 @@ class App {
public init = async(): Promise<void> => {
try{
this.app.use('/health-check', healthCheckController.getRouter())
this.app.use('/users', usersController.getRouter())
this.app.use('/photos', photosController.getRouter())
this.app.listen(process.env.APP_PORT, () => {
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`);
})
......
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