Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exist.content = content;
exist.isCompleted = isCompleted;
exist.level = level;
try {
const updated = await this._todoService.update(id, exist);
return this._todoService.map(updated.toJSON(), Todo, TodoVm);
} catch (e) {
throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Delete(':id')
// @Roles(UserRole.Admin)
// @UseGuards(AuthGuard('jwt'), RolesGuard)
@ApiOkResponse({ type: TodoVm })
@ApiBadRequestResponse({ type: ApiException })
@ApiOperation(GetOperationId(Todo.modelName, 'Delete'))
async delete(@Param('id') id: string): Promise {
try {
const deleted = await this._todoService.delete(id);
return this._todoService.map(deleted.toJSON(), Todo, TodoVm);
} catch (e) {
throw new InternalServerErrorException(e);
}
}
}
export function EnumToArray(enumVariable: any): string[] {
return Object.keys(enumVariable).map(k => enumVariable[k]);
}
filter['isCompleted'] = isCompleted;
}
}
try {
const todos = await this._todoService.findAll(filter);
return this._todoService.mapArray(map(todos, todo => todo.toJSON()), Todo, TodoVm);
} catch (e) {
throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Put()
// @Roles(UserRole.Admin, UserRole.User)
// @UseGuards(AuthGuard('jwt'), RolesGuard)
@ApiOkResponse({ type: TodoVm })
@ApiBadRequestResponse({ type: ApiException })
@ApiOperation(GetOperationId(Todo.modelName, 'Update'))
async update(@Body() vm: TodoVm): Promise {
const { id, content, level, isCompleted } = vm;
if (!vm || !id) {
throw new HttpException('Missing parameters', HttpStatus.BAD_REQUEST);
}
const exist = await this._todoService.findById(id);
if (!exist) {
throw new HttpException(`${id} Not found`, HttpStatus.NOT_FOUND);
}
if (exist.isCompleted) {
import { ApiOperation, ApiResponse, ApiOkResponse, ApiForbiddenResponse, ApiCreatedResponse, ApiNotFoundResponse, ApiBadRequestResponse, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { HttpStatus, Get, Param, HttpCode, Post, Body, Put, Delete, Query } from '@nestjs/common';
import { CrudRepositoryService, Paginator, PaginationParams } from './crud-repository.service';
import { FindAndModifyWriteOpResultObject } from 'mongodb';
import { Typegoose, InstanceType } from 'typegoose';
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiForbiddenResponse({ description: 'Forbidden.' })
export abstract class CrudController {
constructor(private readonly crudService: CrudRepositoryService) { }
@ApiOperation({ title: 'find all' })
@ApiOkResponse({ description: 'Found records' })
@Get()
public async findAll(@Query() filter: PaginationParams>): Promise>> {
return this.crudService.paginator(filter);
}
@ApiOperation({ title: 'Find by id' })
@ApiOkResponse({ description: 'Found one record' })
@ApiNotFoundResponse({ description: 'Record not found' })
@Get(':id')
public async findById(@Param('id') id: string): Promise> {
return this.crudService.findOne(id);
}
@ApiOperation({ title: 'Create new record' })
@ApiCreatedResponse({ description: 'The record has been successfully created.' })
@ApiBadRequestResponse({ description: 'Invalid input, The response body may contain clues as to what went wrong' })
import { DbCharacter, DbCharacterShort } from '@DbModel/index';
import { CharacterIdParam, UserIdParam } from '@Parameter/index';
@ApiUseTags('Character')
@Controller('character')
export class CharacterController {
constructor(private readonly characterService: CharacterService) {}
@Get()
@ApiOperation({
title: 'Get All Unassigned Characters',
description:
'Get all of the characters who do not belong to a user. ' +
'These are returned and shown as an example for the user to get an idea of how the app works.'
})
@ApiOkResponse({ type: DbCharacterShort, isArray: true })
getAll(): Observable {
return this.characterService.getAll();
}
@Post('new/:userId')
@ApiOperation({
title: 'New Character',
description:
'Using the User id, create and assign a new character based on the incoming body'
})
@ApiImplicitBody({ name: 'character', type: CharacterDTO })
@ApiImplicitParam({ name: 'userId', type: 'string', required: true })
@UseGuards(AuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: DbCharacter })
newChar(
@ApiOkResponse({ type: DbSpell })
newSpell(
@Body('spell', SpellPipe) inSpell: DbSpell,
@Param() params: CharacterIdParam
): Observable {
return this.spellService.newSpell(inSpell, params.charId);
}
@Patch('update/:spellId')
@ApiOperation({
title: 'Update Spell',
description: 'Update an existing spell based on its id.'
})
@UseGuards(AuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: DbSpell })
@ApiImplicitParam({ name: 'spellId', required: true, type: 'string' })
@ApiImplicitBody({ name: 'spell', type: SpellDTO })
updateSpell(
@Body('spell', SpellPipe) inSpell: DbSpell,
@Param() params: SpellIdParam
): Observable {
return this.spellService.updateSpell(inSpell);
}
}
import { AuthService } from './auth.service';
import { LoginPayloadDto } from './dto/LoginPayloadDto';
import { UserLoginDto } from './dto/UserLoginDto';
import { UserRegisterDto } from './dto/UserRegisterDto';
@Controller('auth')
@ApiUseTags('auth')
export class AuthController {
constructor(
public readonly userService: UserService,
public readonly authService: AuthService,
) {}
@Post('login')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: LoginPayloadDto,
description: 'User info with access token',
})
async userLogin(
@Body() userLoginDto: UserLoginDto,
): Promise {
const userEntity = await this.authService.validateUser(userLoginDto);
const token = await this.authService.createToken(userEntity);
return new LoginPayloadDto(userEntity.toDto(), token);
}
@Post('register')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({ type: UserDto, description: 'Successfully Registered' })
@ApiImplicitFile({ name: 'avatar', required: true })
@Ip() ip: string
) {
this.logger.log(ip);
return this.authService.login(user);
}
/**
* 登出账号
* @returns
* @memberof AuthController
*/
@Get('/logout')
@UseGuards(AuthGuard('jwt'))
@ApiOperation({ title: '登出' })
@ApiBearerAuth()
@ApiOkResponse({ description: '登出成功' })
@ApiForbiddenResponse({ description: '未登录' })
@HttpCode(HttpStatus.OK)
public async logout(@CurrentUser() user: User) {
this.logger.log(user);
return {};
}
}
import { DbSkill } from '@DbModel/index';
import { CharacterIdParam } from '@Parameter/index';
import { SkillService } from '@Skill/skill.service';
import { Observable } from 'rxjs';
@ApiUseTags('Skill')
@Controller('character/skill')
export class SkillController {
constructor(private readonly skillService: SkillService) {}
@Get(':charId')
@ApiOperation({
title: 'get character skills',
description: 'Get all the skills of the specified character.'
})
@ApiOkResponse({ type: DbSkill, isArray: true })
getSkills(@Param() params: CharacterIdParam): Observable {
return this.skillService.getCharacterSkills(params.charId);
}
}
@Controller()
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('login')
@ApiOperation({ title: 'Login', description: 'Log the user in' })
@ApiImplicitBody({ name: 'user', type: UserDTO })
@ApiOkResponse({ type: JwtReturnDTO })
login(@Body('user') user: UserDTO): Observable {
return this.authService.login(user);
}
@Post('signup')
@ApiOperation({ title: 'Signup', description: 'Sign the new user up' })
@ApiImplicitBody({ name: 'user', type: NewUserDTO })
@ApiOkResponse({ type: JwtReturnDTO })
signup(@Body('user') user: NewUserDTO): Observable {
return this.authService.signup(user);
}
@Post('logout')
@ApiOperation({ title: 'Logout', description: 'Allow the user to log out.' })
logout(): void {
return;
}
}
description: 'Get all the notes of one character.'
})
@ApiImplicitParam({ name: 'charId', type: 'string', required: true })
@ApiOkResponse({ type: DbNote, isArray: true })
getNotes(@Param() params: CharacterIdParam): Observable {
return this.noteService.getNotes(params.charId);
}
@Post(':charId')
@ApiOperation({
title: 'New Note',
description: 'Make a new note tied to this character.'
})
@ApiBearerAuth()
@UseGuards(AuthGuard)
@ApiOkResponse({ type: DbNote })
@ApiImplicitParam({ name: 'charId', type: 'string', required: true })
@ApiImplicitBody({ name: 'note', type: NoteDTO })
newNote(
@Body('note', NotePipe) inNote: DbNote,
@Param() params: CharacterIdParam
): Observable {
return this.noteService.saveNote(inNote, params.charId);
}
}