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

Added all methods to backend part. Planning to start work with websockets

parent d52ebea0
import express, { Request, Response, Router } from "express"
import { MessagesService, messagesService } from "../services/messagesService"
import { auth } from "../middlewares/auth"
import IResponse from "../interfaces/IResponse"
import IMessage from "../interfaces/IMessage"
import { EStatuses } from "../enum/EStatuses"
import IModifiedRequest from "../interfaces/IModifiedRequest"
import IMessageDto from "../interfaces/IMessageDto"
export class MessagesController {
private service: MessagesService
private router: Router
constructor(){
this.service = messagesService
this.router = express.Router()
this.router.get('/', auth, this.getMessages)
this.router.post('/', auth, this.addMessage)
}
public getRouter = () => {
return this.router
}
private getMessages = async (req: Request, res: Response): Promise<void> => {
const response: IResponse<IMessage[] | null> = await this.service.getMessages()
if (response.status === EStatuses.SUCCESS){
res.status(200).send(response)
} else{
res.status(418).send(response)
}
}
private addMessage = async (req: Request, res: Response): Promise<void> => {
const modifiedReq = req as IModifiedRequest
if (typeof modifiedReq.verifiedData === 'object'){
const {message} = req.body
const {_id} = modifiedReq.verifiedData
const messageDto: IMessageDto = {
user: _id,
message: message
}
const response: IResponse<IMessage | null> = await this.service.addMessage(messageDto)
if (response.status === EStatuses.SUCCESS){
res.status(200).send(response)
} else{
res.status(418).send(response)
}
} else{
res.status(418).send('error in request. Some invalid field appeared')
}
}
}
export const messagesController = new MessagesController()
\ No newline at end of file
import express, { Request, Response, Router } from "express"
import { EStatuses } from "../enum/EStatuses";
import IResponse from "../interfaces/IResponse";
import IUser from "../interfaces/IUser";
import IUserGetDto from "../interfaces/IUserGetDto";
import { auth } from "../middlewares/auth";
import { usersService, UsersService } from "../services/usersService";
......
import path from 'path'
export const config = {
port: 8000,
filePath: path.join(__dirname, '../public/uploads')
}
import express, { Express } from "express";
import dotenv from 'dotenv';
import cors from 'cors';
import { healthCheckController } from "./controllers/healthCheck";
import { mongooseDB } from "./repository/mongooseDB";
import { usersController } from "./controllers/usersController";
import { messagesController } from "./controllers/messagesController";
dotenv.config()
class App {
private app: Express
constructor() {
this.app = express()
this.app.use(express.json())
this.app.use(cors())
}
public init = async(): Promise<void> => {
try{
this.app.use('/health-check', healthCheckController.getRouter())
this.app.use('/users', usersController.getRouter())
this.app.use('/messages', messagesController.getRouter())
this.app.listen(process.env.APP_PORT, () => {
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`)
})
await mongooseDB.init()
process.on('exit', () => {
mongooseDB.close()
})
} catch(err: unknown){
console.log(err);
}
}
}
const app = new App();
app.init();
\ No newline at end of file
......@@ -3,6 +3,6 @@ import IUser from "./IUser";
export default interface IMessage{
_id: Types.ObjectId
author: IUser['username']
user: IUser
message: string
}
\ No newline at end of file
import IMessage from "./IMessage";
export default interface IMessageDto{
author: IMessage['author']
user: IMessage['_id']
message: IMessage['message']
}
\ No newline at end of file
import mongoose, { Schema } from "mongoose";
import IMessage from "../interfaces/IMessage";
const MessageSchema: Schema = new Schema<IMessage>({
user: {
type: Schema.Types.ObjectId,
ref: 'User',
minlength: 1,
required: [true, 'User id has to be provided']
},
message: {
type: String,
trim: true,
minlength: 1,
required: [true, 'Username should exist']
}
})
export const Message = mongoose.model<IMessage>('Message', MessageSchema)
\ No newline at end of file
......@@ -6,6 +6,9 @@ import IUserGetDto from '../interfaces/IUserGetDto'
import { generateJWT } from '../helpers/generateJWT'
import IUserCreateDto from '../interfaces/IUserCreateDto'
import { User } from '../models/User'
import IMessage from '../interfaces/IMessage'
import { Message } from '../models/Message'
import IMessageDto from '../interfaces/IMessageDto'
dotenv.config()
......@@ -81,6 +84,47 @@ export class MongooseDB {
return response
}
}
public getMessages = async(): Promise<IResponse<IMessage[] | null>> => {
try{
const data = await Message.find().populate('user').limit(30)
const response: IResponse<IMessage[] | 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 addMessage = async(messageDto: IMessageDto): Promise<IResponse<IMessage | null>> => {
try{
const message = new Message(messageDto)
const responseData = await message.save()
const response: IResponse<IMessage> = {
status: EStatuses.SUCCESS,
result: responseData,
message: 'Artist 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 mongooseDB = new MongooseDB()
\ No newline at end of file
import IMessage from "../interfaces/IMessage";
import IMessageDto from "../interfaces/IMessageDto";
import IResponse from "../interfaces/IResponse";
import { MongooseDB, mongooseDB } from "../repository/mongooseDB";
export class MessagesService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public getMessages = async(): Promise<IResponse<IMessage[] | null>> => {
return await this.repository.getMessages()
}
public addMessage = async(messageDto: IMessageDto): Promise<IResponse<IMessage | null>> => {
return await this.repository.addMessage(messageDto)
}
}
export const messagesService = new MessagesService()
\ 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