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

#4 #5 added rouers and controllers for user. Homework 89

parent d5c731ca
import express, { Request, Response, Router } from "express"
import IResponse from "../interfaces/IResponse";
import IUser from "../interfaces/IUser";
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.addUser)
this.router.post('/sessions', this.loginUser)
}
public getRouter = () => {
return this.router
}
private addUser = async (req: Request, res: Response): Promise<void> => {
const response: IResponse<IUser | null> = await this.service.addUser(req.body)
res.status(200).send(response)
}
public loginUser = async(req: Request, res: Response): Promise<void> => {
const response: IResponse<IUser | null> = await this.service.loginUser(req.body)
res.status(200).send(response)
}
}
\ No newline at end of file
......@@ -197,7 +197,31 @@ export class MongooseDB {
}
}
public loginUser = async(userDto: IUserDto): Promise<IResponse<IUser | null>> => {
try{
const user = await User.findOne({username: userDto.username})
if (!user) throw new Error('User not found')
// @ts-ignore
const isMatch: boolean = await user.checkPassword(userDto.password)
if (!isMatch) throw new Error('Wrong password')
// @ts-ignore
user.generateToken()
const response: IResponse<IUser> = {
status: EStatuses.SUCCESS,
result: user,
message: 'Access granted'
}
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
......@@ -2,16 +2,21 @@ import IAlbum from "../interfaces/IAlbum";
import IResponse from "../interfaces/IResponse";
import IAlbumDto from "../interfaces/IAlbumDto";
import { Request } from "express";
import { mongooseDB } from "../repository/mongooseDB";
import { MongooseDB, mongooseDB } from "../repository/mongooseDB";
export class AlbumsService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | IAlbum | null>> => {
return await mongooseDB.getAlbums(req)
return await this.repository.getAlbums(req)
}
public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => {
return await mongooseDB.addAlbum(albumDto)
return await this.repository.addAlbum(albumDto)
}
}
......
import IArtist from "../interfaces/IArtist";
import IArtistDto from "../interfaces/IArtistDto";
import IResponse from "../interfaces/IResponse";
import { mongooseDB } from "../repository/mongooseDB";
import { MongooseDB, mongooseDB } from "../repository/mongooseDB";
export class ArtistsService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public getArtists = async(): Promise<IResponse<IArtist[] | null>> => {
return await mongooseDB.getArtists()
return await this.repository.getArtists()
}
public addArtist = async(artistDto: IArtistDto): Promise<IResponse<IArtist | null>> => {
return await mongooseDB.addArtist(artistDto)
return await this.repository.addArtist(artistDto)
}
}
......
......@@ -2,17 +2,21 @@ import { Request } from "express";
import IResponse from "../interfaces/IResponse";
import ITrack from "../interfaces/ITrack";
import ITrackDto from "../interfaces/ITrackDto";
import { mongooseDB } from "../repository/mongooseDB";
import { MongooseDB, mongooseDB } from "../repository/mongooseDB";
export class TracksService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public getTracks = async(req: Request): Promise<IResponse<ITrack[] | null>> => {
return await mongooseDB.getTracks(req)
return await this.repository.getTracks(req)
}
public addTrack = async(trackDto: ITrackDto): Promise<IResponse<ITrack | null>> => {
return await mongooseDB.addTrack(trackDto)
return await this.repository.addTrack(trackDto)
}
}
......
import IResponse from "../interfaces/IResponse";
import IUser from "../interfaces/IUser";
import IUserDto from "../interfaces/IUserDto";
import { mongooseDB, MongooseDB } from "../repository/mongooseDB";
export class UsersService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public addUser = async (userDto: IUserDto): Promise<IResponse<IUser | null>> => {
return await this.repository.addUser(userDto)
}
public loginUser = async (userDto: IUserDto): Promise<IResponse<IUser | 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