164 lines
4.1 KiB
TypeScript
164 lines
4.1 KiB
TypeScript
/* eslint-disable prettier/prettier */
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
UseGuards
|
|
} from '@nestjs/common'
|
|
import { User } from '@prisma/client'
|
|
import { UserDTO } from './dtos/user.dto'
|
|
import { UserService } from './user.service'
|
|
import { Roles } from '../auth/decorators/roles.decorator'
|
|
import { JwtAccessTokenGuard } from '../auth/guards/jwt.accesstoken.guard'
|
|
import { RolesGuard } from '../auth/guards/roles.guard'
|
|
import { ROLE } from '@prisma/client'
|
|
|
|
@Controller('user')
|
|
export class UserController {
|
|
constructor(private readonly userService: UserService) {}
|
|
|
|
@Roles(ROLE.ADMIN)
|
|
@UseGuards(JwtAccessTokenGuard, RolesGuard)
|
|
@Get()
|
|
async fetchAll(@Query() query) {
|
|
console.log(query)
|
|
|
|
//Field별 filter AND Sql용
|
|
let filterSql = {}
|
|
switch (query.filterField) {
|
|
case 'is_done':
|
|
if (query.filter) {
|
|
filterSql = {
|
|
AND: {
|
|
[query.filterField]: query.filter === 'true' ? true : false
|
|
}
|
|
}
|
|
}
|
|
break
|
|
case 'updatedAt':
|
|
case 'createdAt':
|
|
if (query.filterDateStart && query.filterDateEnd) {
|
|
filterSql = {
|
|
AND: {
|
|
[query.filterField]: {
|
|
gte: new Date(query.filterDateStart) as Date,
|
|
lte: new Date(query.filterDateEnd) as Date
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break
|
|
}
|
|
console.log(filterSql)
|
|
|
|
//Field별 search OR Sql용
|
|
let searchSql = {}
|
|
if (query.search) {
|
|
const searchFieldSQLs = []
|
|
for (const index in query.searchFields) {
|
|
switch (query.searchFields[index]) {
|
|
case 'title':
|
|
case 'content':
|
|
searchFieldSQLs.push({
|
|
[query.searchFields[index]]: {
|
|
contains: query.search as string
|
|
}
|
|
})
|
|
break
|
|
case 'updatedAt':
|
|
case 'createdAt':
|
|
searchFieldSQLs.push({
|
|
[query.searchFields[index]]: {
|
|
gte: new Date(query.search) as Date
|
|
}
|
|
})
|
|
break
|
|
}
|
|
}
|
|
console.log(searchFieldSQLs)
|
|
searchSql = { OR: searchFieldSQLs }
|
|
}
|
|
console.log(searchSql)
|
|
|
|
//Field별 Sort Sql용
|
|
if (!query.sortBy) {
|
|
query.sortBy = 'id'
|
|
}
|
|
const orderBySql = {
|
|
[query.sortBy]: query.sortDesc === 'true' ? 'desc' : 'asc'
|
|
}
|
|
console.log(orderBySql)
|
|
|
|
//fetch SQL용
|
|
const page = query.page
|
|
? parseInt(query.page) - 1
|
|
: parseInt(process.env.DEFAULT_TABLE_PAGE)
|
|
const perPage = query.perPage
|
|
? parseInt(query.perPage)
|
|
: parseInt(process.env.DEFAULT_TABLE_PERPAGE)
|
|
|
|
const fetchSQL = {
|
|
skip: page * perPage,
|
|
take: perPage,
|
|
where: {
|
|
...filterSql,
|
|
...searchSql
|
|
},
|
|
orderBy: orderBySql
|
|
}
|
|
console.log(fetchSQL)
|
|
|
|
//전체 갯수 및 fetched Data
|
|
const total = await this.userService.count(fetchSQL)
|
|
const rows = await this.userService.fetchAll(fetchSQL)
|
|
const result = {
|
|
total: total,
|
|
perPage: perPage,
|
|
page: page + 1,
|
|
sortBy: query.sortBy,
|
|
sortDesc: query.sortDesc,
|
|
rows: rows
|
|
}
|
|
//console.log(result)
|
|
console.log('--------------------------------------------')
|
|
return result
|
|
}
|
|
|
|
@Roles(ROLE.ADMIN)
|
|
@UseGuards(JwtAccessTokenGuard, RolesGuard)
|
|
@Get(':id')
|
|
async fetchOne(@Param('id') id: string) {
|
|
return await this.userService.fetchOne({ id: Number(id) })
|
|
}
|
|
|
|
@Roles(ROLE.ADMIN)
|
|
@UseGuards(JwtAccessTokenGuard, RolesGuard)
|
|
@Post('add')
|
|
async add(@Body() data: UserDTO) {
|
|
return await this.userService.add(data)
|
|
}
|
|
|
|
@Roles(ROLE.ADMIN)
|
|
@UseGuards(JwtAccessTokenGuard, RolesGuard)
|
|
@Put(':id')
|
|
async update(@Param('id') id: string, @Body() data: UserDTO) {
|
|
data.updatedAt = new Date()
|
|
return await this.userService.update({
|
|
where: { id: Number(id) },
|
|
data: data
|
|
})
|
|
}
|
|
|
|
@Roles(ROLE.ADMIN)
|
|
@UseGuards(JwtAccessTokenGuard, RolesGuard)
|
|
@Delete(':id')
|
|
async delete(@Param('id') id: string): Promise<User | undefined> {
|
|
return await this.userService.remove({ id: Number(id) })
|
|
}
|
|
}
|