9 changed files with 156 additions and 23 deletions
@ -0,0 +1,36 @@ |
|||||
|
'use strict'; |
||||
|
/** @type {import('sequelize-cli').Migration} */ |
||||
|
module.exports = { |
||||
|
async up(queryInterface, Sequelize) { |
||||
|
await queryInterface.createTable('Users', { |
||||
|
auth0id: { |
||||
|
allowNull: false, |
||||
|
primaryKey: true, |
||||
|
type: Sequelize.STRING |
||||
|
}, |
||||
|
nickname: { |
||||
|
type: Sequelize.STRING |
||||
|
}, |
||||
|
picture: { |
||||
|
type: Sequelize.STRING |
||||
|
}, |
||||
|
realName:{ |
||||
|
type: Sequelize.STRING |
||||
|
}, |
||||
|
lastLogin: { |
||||
|
type: Sequelize.DATE |
||||
|
}, |
||||
|
createdAt: { |
||||
|
allowNull: false, |
||||
|
type: Sequelize.DATE |
||||
|
}, |
||||
|
updatedAt: { |
||||
|
allowNull: false, |
||||
|
type: Sequelize.DATE |
||||
|
} |
||||
|
}); |
||||
|
}, |
||||
|
async down(queryInterface, Sequelize) { |
||||
|
await queryInterface.dropTable('Users'); |
||||
|
} |
||||
|
}; |
||||
@ -0,0 +1,27 @@ |
|||||
|
'use strict'; |
||||
|
const { |
||||
|
Model |
||||
|
} = require('sequelize'); |
||||
|
module.exports = (sequelize, DataTypes) => { |
||||
|
class User extends Model { |
||||
|
/** |
||||
|
* Helper method for defining associations. |
||||
|
* This method is not a part of Sequelize lifecycle. |
||||
|
* The `models/index` file will call this method automatically. |
||||
|
*/ |
||||
|
static associate(models) { |
||||
|
// define association here
|
||||
|
} |
||||
|
} |
||||
|
User.init({ |
||||
|
nickname: DataTypes.STRING, |
||||
|
auth0id: DataTypes.STRING, |
||||
|
picture: DataTypes.STRING, |
||||
|
realName: DataTypes.STRING, |
||||
|
lastLogin: DataTypes.DATE |
||||
|
}, { |
||||
|
sequelize, |
||||
|
modelName: 'User', |
||||
|
}); |
||||
|
return User; |
||||
|
}; |
||||
@ -0,0 +1,21 @@ |
|||||
|
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common'; |
||||
|
import { JwtAuthGuard } from 'src/authz/authz.guard'; |
||||
|
import { UsersService } from './users.service'; |
||||
|
|
||||
|
@Controller('users') |
||||
|
export class UsersController { |
||||
|
constructor( |
||||
|
private usersService: UsersService |
||||
|
) {} |
||||
|
|
||||
|
@Get(':id') |
||||
|
findOne(@Param() params: any) { |
||||
|
//return this.seasonsService.findOne(params.id);
|
||||
|
} |
||||
|
|
||||
|
@UseGuards(JwtAuthGuard) |
||||
|
@Post('login') |
||||
|
updateLastLogin(@Body() body: any) { |
||||
|
return this.usersService.updateLastLogin(body.id, body.nickname, body.picture, body.time); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
import { Column, HasMany, Model, PrimaryKey, Table } from "sequelize-typescript"; |
||||
|
|
||||
|
@Table |
||||
|
export class User extends Model { |
||||
|
@PrimaryKey |
||||
|
@Column |
||||
|
auth0id: string; |
||||
|
|
||||
|
@Column |
||||
|
nickname: string; |
||||
|
|
||||
|
@Column |
||||
|
picture: string; |
||||
|
|
||||
|
@Column |
||||
|
realName: string; |
||||
|
|
||||
|
@Column |
||||
|
lastLogin: Date; |
||||
|
} |
||||
@ -1,8 +1,15 @@ |
|||||
import { Module } from '@nestjs/common'; |
import { Module } from '@nestjs/common'; |
||||
|
import { SequelizeModule } from '@nestjs/sequelize'; |
||||
import { UsersService } from './users.service'; |
import { UsersService } from './users.service'; |
||||
|
import { UsersController } from './users.controller'; |
||||
|
import { User } from './users.model'; |
||||
|
|
||||
@Module({ |
@Module({ |
||||
|
imports: [ |
||||
|
SequelizeModule.forFeature([User]), |
||||
|
], |
||||
providers: [UsersService], |
providers: [UsersService], |
||||
exports: [UsersService], |
controllers: [UsersController], |
||||
|
exports: [SequelizeModule, UsersService], |
||||
}) |
}) |
||||
export class UsersModule {} |
export class UsersModule {} |
||||
|
|||||
@ -1,24 +1,25 @@ |
|||||
import { Injectable } from '@nestjs/common'; |
import { Injectable } from '@nestjs/common'; |
||||
|
import { InjectModel } from '@nestjs/sequelize'; |
||||
|
import { Sequelize } from 'sequelize-typescript'; |
||||
|
import { User } from './users.model'; |
||||
|
|
||||
// This should be a real class/interface representing a user entity
|
// This should be a real class/interface representing a user entity
|
||||
export type User = any; |
//export type User = any;
|
||||
|
|
||||
@Injectable() |
@Injectable() |
||||
export class UsersService { |
export class UsersService { |
||||
private readonly users = [ |
|
||||
{ |
|
||||
userId: 1, |
|
||||
username: 'john', |
|
||||
password: 'changeme', |
|
||||
}, |
|
||||
{ |
|
||||
userId: 2, |
|
||||
username: 'maria', |
|
||||
password: 'guess', |
|
||||
}, |
|
||||
]; |
|
||||
|
|
||||
async findOne(username: string): Promise<User | undefined> { |
async findOne(username: string): Promise<User | undefined> { |
||||
return this.users.find(user => user.username === username); |
return undefined; |
||||
|
} |
||||
|
|
||||
|
constructor( |
||||
|
@InjectModel(User) private userModel: typeof User, |
||||
|
private sequelize: Sequelize |
||||
|
) |
||||
|
{} |
||||
|
|
||||
|
updateLastLogin(id, nickname, picture, time) { |
||||
|
this.userModel.upsert({auth0id: id, nickname: nickname, picture: picture, lastLogin: time}); |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue