1

我有以下端点

  @Patch("/games/:id")
  @HttpCode(200)
  async updateGame(@Param("id") id: number, @Body() update: Partial<Game>) {
    const updatedGame = await Game.findOne(id);
    if (!updatedGame){
      throw new NotFoundError("HTTP 404 Not Found: No Games Here");
    }
    else if (update.color !== validColor(update.color)) {
      throw new BadRequestError("HTTP 400 Bad Request: No Such Color");
    } else if (update.board && moves(update.board, updatedGame.board) > 1) {
      throw new BadRequestError(
        "HTTP 400 Bad Request:  Only one move allowed. Wait your turn"
      );
    } else {
      console.log("Game has been updated");

      return Game.merge(updatedGame, update).save();
    }
  }

我想知道是否有一种“更清洁”的方式来做同样的事情。即如何使用解构、速记等来重构它?

4

0 回答 0