변경 5번째..
This commit is contained in:
parent
f7e941d6aa
commit
c77808b9fc
@ -1,22 +1,22 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller'
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service'
|
||||||
|
|
||||||
describe('AppController', () => {
|
describe('AppController', () => {
|
||||||
let appController: AppController;
|
let appController: AppController
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const app: TestingModule = await Test.createTestingModule({
|
const app: TestingModule = await Test.createTestingModule({
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService],
|
providers: [AppService]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
appController = app.get<AppController>(AppController);
|
appController = app.get<AppController>(AppController)
|
||||||
});
|
})
|
||||||
|
|
||||||
describe('root', () => {
|
describe('root', () => {
|
||||||
it('should return "Hello World!"', () => {
|
it('should return "Hello World!"', () => {
|
||||||
expect(appController.getHello()).toBe('Hello World!');
|
expect(appController.getHello()).toBe('Hello World!')
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Controller, Get } from '@nestjs/common';
|
import { Controller, Get } from '@nestjs/common'
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service'
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class AppController {
|
export class AppController {
|
||||||
@ -7,6 +7,6 @@ export class AppController {
|
|||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getHello(): string {
|
getHello(): string {
|
||||||
return this.appService.getHello();
|
return this.appService.getHello()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AppService {
|
export class AppService {
|
||||||
getHello(): string {
|
getHello(): string {
|
||||||
return 'Hello World!';
|
return 'Hello World!'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { AuthController } from './auth.controller';
|
import { AuthController } from './auth.controller'
|
||||||
|
|
||||||
describe('AuthController', () => {
|
describe('AuthController', () => {
|
||||||
let controller: AuthController;
|
let controller: AuthController
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
controllers: [AuthController],
|
controllers: [AuthController]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
controller = module.get<AuthController>(AuthController);
|
controller = module.get<AuthController>(AuthController)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(controller).toBeDefined();
|
expect(controller).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service'
|
||||||
|
|
||||||
describe('AuthService', () => {
|
describe('AuthService', () => {
|
||||||
let service: AuthService;
|
let service: AuthService
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [AuthService],
|
providers: [AuthService]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
service = module.get<AuthService>(AuthService);
|
service = module.get<AuthService>(AuthService)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(service).toBeDefined();
|
expect(service).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
import {
|
import {
|
||||||
ExecutionContext,
|
ExecutionContext,
|
||||||
Injectable,
|
Injectable,
|
||||||
UnauthorizedException,
|
UnauthorizedException
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common'
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||||
canActivate(context: ExecutionContext) {
|
canActivate(context: ExecutionContext) {
|
||||||
// Add your custom authentication logic here
|
// Add your custom authentication logic here
|
||||||
// for example, call super.logIn(request) to establish a session.
|
// for example, call super.logIn(request) to establish a session.
|
||||||
return super.canActivate(context);
|
return super.canActivate(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRequest(err, user, info) {
|
handleRequest(err, user, info) {
|
||||||
// You can throw an exception based on either "info" or "err" arguments
|
// You can throw an exception based on either "info" or "err" arguments
|
||||||
if (err || !user) {
|
if (err || !user) {
|
||||||
throw err || new UnauthorizedException();
|
throw err || new UnauthorizedException()
|
||||||
}
|
}
|
||||||
return user;
|
return user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common'
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LocalAuthGuard extends AuthGuard('local') {}
|
export class LocalAuthGuard extends AuthGuard('local') {}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { RolesGuard } from './roles.guard';
|
import { RolesGuard } from './roles.guard'
|
||||||
|
|
||||||
describe('RolesGuard', () => {
|
describe('RolesGuard', () => {
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(new RolesGuard()).toBeDefined();
|
expect(new RolesGuard()).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
12
src/main.ts
12
src/main.ts
@ -1,17 +1,15 @@
|
|||||||
//참고 : https://wikidocs.net/book/7059
|
//참고 : https://wikidocs.net/book/7059
|
||||||
|
|
||||||
import { NestFactory } from '@nestjs/core'
|
import { NestFactory } from '@nestjs/core'
|
||||||
import { env } from 'process'
|
|
||||||
import { AppModule } from './app.module'
|
import { AppModule } from './app.module'
|
||||||
var cors = require('cors')
|
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule)
|
const app = await NestFactory.create(AppModule)
|
||||||
//Enable All CORS Requests : https://github.com/expressjs/cors#enable-cors-for-a-single-route
|
//Enable All CORS Requests : https://docs.nestjs.com/security/cors
|
||||||
var corsOptions = function (req, callback) {
|
const corsOptions = function (req, callback) {
|
||||||
const origin = req.header('Origin')
|
const origin = req.header('Origin')
|
||||||
var corsOptions
|
let corsOptions
|
||||||
if (env.CORS_ALLOW_LIST.indexOf(origin) !== -1) {
|
if (process.env.CORS_ALLOW_LIST.indexOf(origin) !== -1) {
|
||||||
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
|
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
|
||||||
console.log('Allowed Origin URL: ' + origin)
|
console.log('Allowed Origin URL: ' + origin)
|
||||||
} else {
|
} else {
|
||||||
@ -20,7 +18,7 @@ async function bootstrap() {
|
|||||||
}
|
}
|
||||||
callback(null, corsOptions) // callback expects two parameters: error and options
|
callback(null, corsOptions) // callback expects two parameters: error and options
|
||||||
}
|
}
|
||||||
app.use(cors(corsOptions))
|
app.enableCors(corsOptions)
|
||||||
await app.listen(3000, function () {
|
await app.listen(3000, function () {
|
||||||
console.log(
|
console.log(
|
||||||
'[CORS-enabled->npm install -g webpack webpack-cli] web server listening on port 3000'
|
'[CORS-enabled->npm install -g webpack webpack-cli] web server listening on port 3000'
|
||||||
|
|||||||
@ -1,15 +1,15 @@
|
|||||||
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
|
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
await this.$connect();
|
await this.$connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
async enableShutdownHooks(app: INestApplication) {
|
async enableShutdownHooks(app: INestApplication) {
|
||||||
this.$on('beforeExit', async () => {
|
this.$on('beforeExit', async () => {
|
||||||
await app.close();
|
await app.close()
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
export class TodoDTO {
|
export class TodoDTO {
|
||||||
title: string;
|
title: string
|
||||||
content: string;
|
content: string
|
||||||
is_done: boolean;
|
is_done: boolean
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { TodoController } from './todo.controller';
|
import { TodoController } from './todo.controller'
|
||||||
|
|
||||||
describe('TodoController', () => {
|
describe('TodoController', () => {
|
||||||
let controller: TodoController;
|
let controller: TodoController
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
controllers: [TodoController],
|
controllers: [TodoController]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
controller = module.get<TodoController>(TodoController);
|
controller = module.get<TodoController>(TodoController)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(controller).toBeDefined();
|
expect(controller).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -21,15 +21,15 @@ import { TodoService } from './todo.service'
|
|||||||
export class TodoController {
|
export class TodoController {
|
||||||
constructor(private readonly todoService: TodoService) {}
|
constructor(private readonly todoService: TodoService) {}
|
||||||
|
|
||||||
@Get()
|
@Get('/vue3')
|
||||||
async fetchAll(@Query() query): Promise<{}> {
|
async fetchAllVue3(@Query() query): Promise<{ total: number; rows: Todo[] }> {
|
||||||
//Sql용
|
//Sql용
|
||||||
console.log(query)
|
console.log(query)
|
||||||
const searchSql = query.searchString
|
const searchSql = query.search
|
||||||
? {
|
? {
|
||||||
OR: [
|
OR: [
|
||||||
{ title: { contains: query.searchString as string } },
|
{ title: { contains: query.search as string } },
|
||||||
{ content: { contains: query.searchString as string } }
|
{ content: { contains: query.search as string } }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
: {}
|
: {}
|
||||||
@ -68,6 +68,34 @@ export class TodoController {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('/vue2')
|
||||||
|
async fetchAllVue2(@Query() query): Promise<{ total: number; rows: Todo[] }> {
|
||||||
|
//Sql용
|
||||||
|
console.log(query)
|
||||||
|
const searchSql = query.search
|
||||||
|
? {
|
||||||
|
OR: [
|
||||||
|
{ title: { contains: query.search as string } },
|
||||||
|
{ content: { contains: query.search as string } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
: {}
|
||||||
|
const fetchSQL = {
|
||||||
|
where: {
|
||||||
|
//is_done: true,
|
||||||
|
...searchSql
|
||||||
|
},
|
||||||
|
orderBy: { id: 'desc' }
|
||||||
|
}
|
||||||
|
console.log(fetchSQL)
|
||||||
|
const rows = await this.todoService.fetchAll(fetchSQL)
|
||||||
|
const data = {
|
||||||
|
total: rows.length,
|
||||||
|
rows: rows
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
@HasRoles(Role.USER)
|
@HasRoles(Role.USER)
|
||||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common'
|
||||||
import { PrismaService } from 'src/prisma.service';
|
import { PrismaService } from 'src/prisma.service'
|
||||||
import { TodoController } from './todo.controller';
|
import { TodoController } from './todo.controller'
|
||||||
import { TodoService } from './todo.service';
|
import { TodoService } from './todo.service'
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [TodoController],
|
controllers: [TodoController],
|
||||||
providers: [TodoService, PrismaService],
|
providers: [TodoService, PrismaService]
|
||||||
})
|
})
|
||||||
export class TodoModule {}
|
export class TodoModule {}
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { TodoService } from './todo.service';
|
import { TodoService } from './todo.service'
|
||||||
|
|
||||||
describe('TodoService', () => {
|
describe('TodoService', () => {
|
||||||
let service: TodoService;
|
let service: TodoService
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [TodoService],
|
providers: [TodoService]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
service = module.get<TodoService>(TodoService);
|
service = module.get<TodoService>(TodoService)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(service).toBeDefined();
|
expect(service).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common'
|
import { Injectable } from '@nestjs/common'
|
||||||
import { Todo, Prisma } from '@prisma/client'
|
import { Todo } from '@prisma/client'
|
||||||
import { PrismaService } from 'src/prisma.service'
|
import { PrismaService } from 'src/prisma.service'
|
||||||
import { TodoDTO } from './dtos/todo.dto'
|
import { TodoDTO } from './dtos/todo.dto'
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ export class TodoService {
|
|||||||
//참고: https://intrepidgeeks.com/tutorial/05-instagram-clone-code-5-user-summary
|
//참고: https://intrepidgeeks.com/tutorial/05-instagram-clone-code-5-user-summary
|
||||||
//전체조회
|
//전체조회
|
||||||
async count(sql: any): Promise<number> {
|
async count(sql: any): Promise<number> {
|
||||||
return this.prismaService.todo.count()
|
return this.prismaService.todo.count(sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchAll(sql: any): Promise<Todo[]> {
|
async fetchAll(sql: any): Promise<Todo[]> {
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { UserController } from './user.controller';
|
import { UserController } from './user.controller'
|
||||||
|
|
||||||
describe('UserController', () => {
|
describe('UserController', () => {
|
||||||
let controller: UserController;
|
let controller: UserController
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
controllers: [UserController],
|
controllers: [UserController]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
controller = module.get<UserController>(UserController);
|
controller = module.get<UserController>(UserController)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(controller).toBeDefined();
|
expect(controller).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common'
|
||||||
import { PrismaService } from 'src/prisma.service';
|
import { PrismaService } from 'src/prisma.service'
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service'
|
||||||
import { UserController } from './user.controller';
|
import { UserController } from './user.controller'
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [UserController],
|
controllers: [UserController],
|
||||||
providers: [UserService, PrismaService],
|
providers: [UserService, PrismaService],
|
||||||
exports: [UserService],
|
exports: [UserService]
|
||||||
})
|
})
|
||||||
export class UsersModule {}
|
export class UsersModule {}
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service'
|
||||||
|
|
||||||
describe('UserService', () => {
|
describe('UserService', () => {
|
||||||
let service: UserService;
|
let service: UserService
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [UserService],
|
providers: [UserService]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
service = module.get<UserService>(UserService);
|
service = module.get<UserService>(UserService)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(service).toBeDefined();
|
expect(service).toBeDefined()
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
@ -1,24 +1,24 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing'
|
||||||
import { INestApplication } from '@nestjs/common';
|
import { INestApplication } from '@nestjs/common'
|
||||||
import * as request from 'supertest';
|
import * as request from 'supertest'
|
||||||
import { AppModule } from './../src/app.module';
|
import { AppModule } from './../src/app.module'
|
||||||
|
|
||||||
describe('AppController (e2e)', () => {
|
describe('AppController (e2e)', () => {
|
||||||
let app: INestApplication;
|
let app: INestApplication
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||||
imports: [AppModule],
|
imports: [AppModule]
|
||||||
}).compile();
|
}).compile()
|
||||||
|
|
||||||
app = moduleFixture.createNestApplication();
|
app = moduleFixture.createNestApplication()
|
||||||
await app.init();
|
await app.init()
|
||||||
});
|
})
|
||||||
|
|
||||||
it('/ (GET)', () => {
|
it('/ (GET)', () => {
|
||||||
return request(app.getHttpServer())
|
return request(app.getHttpServer())
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.expect('Hello World!');
|
.expect('Hello World!')
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user