Добавлен контроллер пользователей, описана схема, подключен сервис для работы с БД, описана первая ручка для получения списка пользователей
This commit is contained in:
36
src/users/users.contoller.ts
Normal file
36
src/users/users.contoller.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put, UseInterceptors, All} from '@nestjs/common';
|
||||
|
||||
import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger';
|
||||
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, USERS_CONTROLLER} from '../consts';
|
||||
import {UserService} from './users.service';
|
||||
import {UserRequest} from './users.schema';
|
||||
|
||||
@Controller(USERS_CONTROLLER)
|
||||
@ApiTags(USERS_CONTROLLER)
|
||||
export class UsersController {
|
||||
constructor(
|
||||
private readonly userService: UserService
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@Header(...ALLOW_ORIGIN_ALL)
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Список всех пользователей',
|
||||
type: [UserRequest]
|
||||
})
|
||||
async findAll(): Promise<UserRequest[]> {
|
||||
return this.userService.findAll();
|
||||
}
|
||||
|
||||
@Options()
|
||||
@Header(...ALLOW_ORIGIN_ALL)
|
||||
@Header(...ALLOW_METHOD)
|
||||
@Header(...ALLOW_CREDENTIALS)
|
||||
@Header(...CONTENT_LENGTH)
|
||||
@Header(...ALLOW_HEADERS)
|
||||
@HttpCode(204)
|
||||
async options(): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
63
src/users/users.schema.ts
Normal file
63
src/users/users.schema.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document } from 'mongoose';
|
||||
import {ApiProperty} from '@nestjs/swagger';
|
||||
|
||||
interface Token {
|
||||
token: string;
|
||||
expired_at: string;
|
||||
}
|
||||
|
||||
export class UserRequest {
|
||||
@ApiProperty()
|
||||
login: string;
|
||||
|
||||
@ApiProperty()
|
||||
avatar: string;
|
||||
|
||||
@ApiProperty()
|
||||
is_admin: string;
|
||||
}
|
||||
|
||||
@Schema()
|
||||
export class User extends Document {
|
||||
@Prop({
|
||||
required: true,
|
||||
unique: true,
|
||||
type: String,
|
||||
})
|
||||
login: string;
|
||||
|
||||
@Prop({
|
||||
required: true,
|
||||
type: String,
|
||||
})
|
||||
password: string;
|
||||
|
||||
@Prop({
|
||||
type: String,
|
||||
})
|
||||
avatar: string;
|
||||
|
||||
@Prop({
|
||||
type: Boolean,
|
||||
})
|
||||
is_admin: boolean;
|
||||
|
||||
@Prop({
|
||||
type: {
|
||||
token: String,
|
||||
expired_at: String,
|
||||
}
|
||||
})
|
||||
access_token: Token;
|
||||
|
||||
@Prop({
|
||||
type: {
|
||||
token: String,
|
||||
expired_at: String,
|
||||
}
|
||||
})
|
||||
refresh_token: Token;
|
||||
}
|
||||
|
||||
export const UserSchema = SchemaFactory.createForClass(User);
|
||||
20
src/users/users.service.ts
Normal file
20
src/users/users.service.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import {Model, Connection} from 'mongoose';
|
||||
import {Injectable} from '@nestjs/common';
|
||||
import {InjectConnection} from '@nestjs/mongoose';
|
||||
import {DB_NAME, USERS_CONTROLLER} from 'src/consts';
|
||||
import {User, UserSchema} from './users.schema';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectConnection(DB_NAME) private dbConnection: Connection,
|
||||
) {}
|
||||
|
||||
get userModel(): Model<User> {
|
||||
return this.dbConnection.model<User>(USERS_CONTROLLER, UserSchema);
|
||||
}
|
||||
|
||||
findAll(): any {
|
||||
return this.userModel.find().exec();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user