我正在搜索游戏名称并想从外部 api 获取游戏数据.. 如果游戏名称丢失,请将其保存到数据库

问题描述 投票:0回答:0

我是 loopback4 上的新手,并通过使用 loopback 连接器 rest 包做了一切来处理来自外部 API 的数据,如果 s-game 名称搜索是 api 它成功但如果不是,应该将名称保存到 postgresql ..在这一点上我需要帮助。

这是我的模特:

@model()
export class Games extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  id?: number;

  @property({
    type: 'number',
  })
  cheapest: number;

  @property({
    type: 'string',
  })
  cheapestID?: string;

  @property({
    type: 'string',
  })
  external?: string;

  @property({
    type: 'string',
  })
  internalName?: string;

  @property({
    type: 'string',
  })
  thumb?: string;

这是我的外部 api rest 数据源配置:

const config = {
  name: 'rest',
  connector: 'rest',
  options: {
    headers: {
      accept: 'application/json',
      'content-type': 'application/json',
    },
  },
  operations: [
    {
      template: {
        method: 'GET',
        url: 'https://www.cheapshark.com/api/1.0/stores',
      },
      functions: {
        AllStores: [],
      },
    },
    {
      template: {
        method: 'GET',
        url: 'https://www.cheapshark.com/api/1.0/games?title={title}',
      },
      functions: {
        findByTitle: ['title'],
      },
    },
  ],
};

这是控制器:

export class GamesController {
  constructor(
    @repository(GamesRepository)
    public gamesRepository: GamesRepository,
    @inject('service.GameService')
    protected gameService: GameService,
  ) {}

  @get('/games')
  async findByTitle(
    @param.query.string('title') title: string,
  ): Promise<Games> {
    let game = await this.gameService.findByTitle(title);
    if (!game) {
      // create a new game if it doesn't exist
      game = new Games({
        external: title,
      });
      await this.gamesRepository.create(game);
    }

    return game;
  }

谢谢你的建议

我正在尝试保存数据库游戏名称,如果它缺少 api 数据但它是空的

node.js typescript loopbackjs loopback loopback4
© www.soinside.com 2019 - 2024. All rights reserved.