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

#4 #5 #6 added models and interfaces for TrackHistory

parent cd4dc7ce
...@@ -24,7 +24,7 @@ export class UsersController { ...@@ -24,7 +24,7 @@ export class UsersController {
} }
public loginUser = async(req: Request, res: Response): Promise<void> => { public loginUser = async(req: Request, res: Response): Promise<void> => {
const response: IResponse<IUser | null> = await this.service.loginUser(req.body) const response: IResponse<{token: string} | null> = await this.service.loginUser(req.body)
res.status(200).send(response) res.status(200).send(response)
} }
} }
......
import ITrack from "./ITrack";
import IUser from "./IUser";
export default interface ITrackHistory {
user: IUser['_id']
track: ITrack['_id']
datetime: Date
}
\ No newline at end of file
import ITrack from "./ITrack";
import IUser from "./IUser";
export default interface ITrackHistoryDto{
token: IUser['token']
track: ITrack['_id']
}
\ No newline at end of file
import {Document} from "mongoose" import {Document, Types} from "mongoose"
export default interface IUser extends Document { export default interface IUser extends Document {
_id: string _id: Types.ObjectId
username: string username: string
password: string password: string
token: string token: string
......
import mongoose, {Schema} from "mongoose";
import ITrackHistory from "../interfaces/ITrackHistory";
const TrackHistorySchema: Schema = new Schema<ITrackHistory>({
user:{
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
track:{
type: Schema.Types.ObjectId,
ref: 'Track',
required: true
},
datetime:{
type: Date,
timestamps: true
}
})
export const TrackHistory = mongoose.model<ITrackHistory>('TrackHistory', TrackHistorySchema)
...@@ -16,7 +16,7 @@ const UserSchema: Schema = new Schema<IUser>({ ...@@ -16,7 +16,7 @@ const UserSchema: Schema = new Schema<IUser>({
}, },
token: { token: {
type: String, type: String,
required: true required: false
} }
},{versionKey: false}) },{versionKey: false})
......
...@@ -197,7 +197,7 @@ export class MongooseDB { ...@@ -197,7 +197,7 @@ export class MongooseDB {
} }
} }
public loginUser = async(userDto: IUserDto): Promise<IResponse<IUser | null>> => { public loginUser = async(userDto: IUserDto): Promise<IResponse<{token: string} | null>> => {
try{ try{
const user = await User.findOne({username: userDto.username}) const user = await User.findOne({username: userDto.username})
if (!user) throw new Error('User not found') if (!user) throw new Error('User not found')
...@@ -206,9 +206,10 @@ export class MongooseDB { ...@@ -206,9 +206,10 @@ export class MongooseDB {
if (!isMatch) throw new Error('Wrong password') if (!isMatch) throw new Error('Wrong password')
// @ts-ignore // @ts-ignore
user.generateToken() user.generateToken()
const response: IResponse<IUser> = { const data = await user.save()
const response: IResponse<{token: string}> = {
status: EStatuses.SUCCESS, status: EStatuses.SUCCESS,
result: user, result: {token: data.token},
message: 'Access granted' message: 'Access granted'
} }
return response return response
......
...@@ -14,7 +14,7 @@ export class UsersService { ...@@ -14,7 +14,7 @@ export class UsersService {
return await this.repository.addUser(userDto) return await this.repository.addUser(userDto)
} }
public loginUser = async (userDto: IUserDto): Promise<IResponse<IUser | null>> => { public loginUser = async (userDto: IUserDto): Promise<IResponse<{token: string} | null>> => {
return await this.repository.loginUser(userDto) return await this.repository.loginUser(userDto)
} }
} }
......
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