import{Logger}from"@nestjs/common";import{NestFactory}from"@nestjs/core";import{AppModule}from"./app.module";import*asconfigfrom"config";asyncfunctionbootstrap(){constapp=awaitNestFactory.create(AppModule);constserverConfig=config.get("server");constport=serverConfig.port;awaitapp.listen(port);Logger.log(`Application running on port ${port}`);}bootstrap();
import{Body,Controller,Delete,Get,Logger,Param,ParseIntPipe,Patch,Post,UseGuards,UsePipes,ValidationPipe,}from"@nestjs/common";import{AuthGuard}from"@nestjs/passport";import{GetUser}from"src/auth/get-user.decorator";import{User}from"src/auth/user.entity";import{BoardStatus}from"./board-status.enum";import{Board}from"./board.entity";import{BoardsService}from"./boards.service";import{CreateBoardDto}from"./dto/create-board.dto";import{BoardStatusValidationPipe}from"./pipes/board-status-validation.pipe";@Controller("boards")@UseGuards(AuthGuard())exportclassBoardsController{privatelogger=newLogger("BoardsController");constructor(privateboardsService:BoardsService){}@Get("/")getAllBoards(@GetUser()user:User):Promise<Board[]>{this.logger.verbose(`User ${user.username} trying to get all boards`);returnthis.boardsService.getAllBoards(user);}@Post("/")@UsePipes(ValidationPipe)createBoard(@Body()createBoardDto:CreateBoardDto,@GetUser()user:User):Promise<Board>{this.logger.verbose(`User ${user.username} creating a new board. Payload: ${JSON.stringify(createBoardDto)}`);returnthis.boardsService.createBoard(createBoardDto,user);}@Get("/:id")getBoardById(@Param("id",ParseIntPipe)id:number):Promise<Board>{returnthis.boardsService.getBoardById(id);}@Delete("/:id")deleteBoard(@Param("id",ParseIntPipe)id:number,@GetUser()user:User):Promise<void>{returnthis.boardsService.deleteBoard(id,user);}@Patch("/:id/status")updateBoardStatus(@Param("id",ParseIntPipe)id:number,@Body("status",BoardStatusValidationPipe)status:BoardStatus):Promise<Board>{returnthis.boardsService.updateBoardStatus(id,status);}}
import{Injectable,NotFoundException}from"@nestjs/common";import{InjectRepository}from"@nestjs/typeorm";import{User}from"src/auth/user.entity";import{Repository}from"typeorm";import{BoardStatus}from"./board-status.enum";import{Board}from"./board.entity";import{CreateBoardDto}from"./dto/create-board.dto";@Injectable()exportclassBoardsService{constructor(@InjectRepository(Board)privateboardRepository:Repository<Board>){}asyncgetAllBoards(user:User):Promise<Board[]>{constquery=this.boardRepository.createQueryBuilder("board");query.where("board.userId = :userId",{userId:user.id});constboards=awaitquery.getMany();returnboards;}asynccreateBoard(createBoardDto:CreateBoardDto,user:User):Promise<Board>{const{title,description}=createBoardDto;constboard=this.boardRepository.create({title,description,status:BoardStatus.PUBLIC,user,});awaitthis.boardRepository.save(board);returnboard;}asyncgetBoardById(id:number):Promise<Board>{constfound=awaitthis.boardRepository.findOneBy({id});if(!found){thrownewNotFoundException(`Can't find Board with id ${id}`);}returnfound;}asyncdeleteBoard(id:number,user:User):Promise<void>{constresult=awaitthis.boardRepository.delete({id,user});if(result.affected===0){thrownewNotFoundException(`Can't find Board with id ${id}`);}}asyncupdateBoardStatus(id:number,status:BoardStatus):Promise<Board>{constboard=awaitthis.getBoardById(id);board.status=status;awaitthis.boardRepository.save(board);returnboard;}}
import{BadRequestException,PipeTransform}from"@nestjs/common";import{BoardStatus}from"../board-status.enum";exportclassBoardStatusValidationPipeimplementsPipeTransform{readonlyStatusOptions=[BoardStatus.PUBLIC,BoardStatus.PRIVATE];privateisStatusValid(status:any):boolean{returnthis.StatusOptions.includes(status);}transform(value:any){value=value.toUpperCase();if(!this.isStatusValid(value)){thrownewBadRequestException(`${value} isn't in the status`);}returnvalue;}}