import { DataSource } from 'typeorm';
import { ServerEntity } from '../../../entities';
import { rowToServer } from '../../mappers';
import { loadServerRelationsMap } from '../../relations';

export async function handleGetAllPublicServers(dataSource: DataSource) {
  const repo = dataSource.getRepository(ServerEntity);
  const rows = await repo.find({ where: { isPrivate: 0 } });
  const relationsByServerId = await loadServerRelationsMap(dataSource, rows.map((row) => row.id));

  return rows.map((row) => rowToServer(row, relationsByServerId.get(row.id)));
}
