수정 3번째...

This commit is contained in:
최준흠 2022-08-22 19:59:04 +09:00
parent fb2102168f
commit 31a2557f65
3 changed files with 36 additions and 33 deletions

4
.env
View File

@ -11,5 +11,5 @@ JWT_SECURITY_KEY = "security_key"
JWT_EXPIRE_MAX = "600s" JWT_EXPIRE_MAX = "600s"
AUTH_USERNAME_FIELD="email" AUTH_USERNAME_FIELD="email"
DEFAULT_TABLE_PER_PAGE = 20 DEFAULT_TABLE_PER_PAGE = 5
DEFAULT_TABLE_PAGE = 0 DEFAULT_TABLE_PAGE = 1

View File

@ -10,7 +10,6 @@ import {
UseGuards UseGuards
} from '@nestjs/common' } from '@nestjs/common'
import { Todo } from '@prisma/client' import { Todo } from '@prisma/client'
import { env } from 'process'
import { HasRoles } from 'src/auth/decorators/has-roles.decorator' import { HasRoles } from 'src/auth/decorators/has-roles.decorator'
import { JwtAuthGuard } from 'src/auth/guards/jwt.authguard' import { JwtAuthGuard } from 'src/auth/guards/jwt.authguard'
import { Role } from 'src/auth/guards/role.enum' import { Role } from 'src/auth/guards/role.enum'
@ -24,30 +23,14 @@ export class TodoController {
@Get() @Get()
async fetchAll(@Query() query): Promise<{}> { async fetchAll(@Query() query): Promise<{}> {
//쿼리값 변경
const per_page: number = isNaN(query.per_page)
? Number(env.DEFAULT_TABLE_PER_PAGE)
: Number(query.per_page)
const page: number = isNaN(query.page)
? Number(env.DEFAULT_TABLE_PAGE)
: Number(query.page)
const searchString: string = isNaN(query.searchString)
? ''
: String(query.searchString)
//리턴 할 데이터
const datas = {
query: {
page: page,
per_page: per_page,
searchString: searchString
},
total: 0,
rows: []
}
datas.total = await this.todoService.count()
//Sql용 //Sql용
console.log(query)
const page = query.page
? parseInt(query.page)
: parseInt(process.env.DEFAULT_TABLE_PAGE)
const per_page = query.per_page
? parseInt(query.per_page)
: parseInt(process.env.DEFAULT_TABLE_PER_PAGE)
const searchSql = query.searchString const searchSql = query.searchString
? { ? {
OR: [ OR: [
@ -57,18 +40,37 @@ export class TodoController {
} }
: {} : {}
const sql = { const countSQL = {
take: per_page,
skip: page,
where: { where: {
//is_done: true, //is_done: true,
...searchSql ...searchSql
}, },
orderBy: { id: 'desc' } orderBy: { id: 'desc' }
} }
datas.rows = await this.todoService.fetchAll(sql) const total = await this.todoService.count(countSQL)
//console.log(datas) console.log(countSQL)
return datas
const fetchSQL = {
skip: (page - 1) * per_page,
take: per_page,
where: {
//is_done: true,
...searchSql
},
orderBy: { id: 'desc' }
}
const rows = await this.todoService.fetchAll(fetchSQL)
console.log(fetchSQL)
const data = {
page: page,
per_page: per_page,
searchString: query.searchString,
total: total,
rows: rows
}
//console.log(data)
return data
} }
@HasRoles(Role.USER) @HasRoles(Role.USER)

View File

@ -10,9 +10,10 @@ 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(): Promise<number> { async count(sql: any): Promise<number> {
return this.prismaService.todo.count() return this.prismaService.todo.count()
} }
async fetchAll(sql: any): Promise<Todo[]> { async fetchAll(sql: any): Promise<Todo[]> {
return this.prismaService.todo.findMany(sql) return this.prismaService.todo.findMany(sql)
} }