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

Added all functions for user. Login, create, checktoken and etc

parent 569a1e4f
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 IModifiedRequest from "../interfaces/IModifiedRequest";
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('/sessions', 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
......@@ -81,4 +81,6 @@ export class MongooseDB {
return response
}
}
}
\ No newline at end of file
}
export const mongooseDB = new MongooseDB()
\ No newline at end of file
import IResponse from "../interfaces/IResponse";
import IUser from "../interfaces/IUser";
import IUserCreateDto from "../interfaces/IUserCreateDto";
import IUserGetDto from "../interfaces/IUserGetDto";
import { mongooseDB, MongooseDB } from "../repository/mongooseDB";
export class UsersService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public createUser = async (userDto: IUserCreateDto): Promise<IResponse<IUserGetDto | null>> => {
return await this.repository.createUser(userDto)
}
public loginUser = async (userDto: IUserCreateDto): Promise<IResponse<IUserGetDto | null>> => {
return await this.repository.loginUser(userDto)
}
}
export const usersService = new UsersService()
\ 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