Skip to content

Commit

Permalink
add get all maps route
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Whitacre committed Feb 11, 2025
1 parent c5066be commit 3ff26e4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
10 changes: 7 additions & 3 deletions api/routes/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ class Maps extends Route {
}

const campaign = req.getQueryParam("campaign");
if (!campaign) return ApiResponse.badRequest(req);
if (campaign) {
const maps = await req.services.maps.getCampaign(campaign);
if (!maps) return ApiResponse.badRequest(req);

const maps = await req.services.maps.getAll(campaign);
if (!maps) return ApiResponse.badRequest(req);
return ApiResponse.ok(req, { maps: maps.map((m) => m.toJson()) });
}

const maps = await req.services.maps.getAll();
if (!maps) return ApiResponse.badRequest(req);
return ApiResponse.ok(req, { maps: maps.map((m) => m.toJson()) });
}

Expand Down
13 changes: 12 additions & 1 deletion api/services/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Maps {
return Map.fromJson(response.rows[0]);
}

async getAll(campaign: string) {
async getCampaign(campaign: string) {
const response = await this.db.pool.query(
`
select * from Maps
Expand All @@ -34,6 +34,17 @@ export class Maps {
return response.rows.map(Map.fromJson);
}

async getAll() {
const response = await this.db.pool.query(
`
select * from Maps
`,
[]
);
if (!response?.rowCount) return [];
return response.rows.map(Map.fromJson);
}

async insert(map: Map) {
return this.db.pool.query(
`
Expand Down
6 changes: 5 additions & 1 deletion test/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ export const mapGet = (_: Pool, mapUid: string) => {
return fetch(`http://localhost:8081/maps?mapUid=${mapUid}`);
};

export const mapGetAll = (campaign: string) => {
export const mapGetCampaign = (campaign: string) => {
return fetch(`http://localhost:8081/maps?campaign=${campaign}`);
};

export const mapGetAll = () => {
return fetch(`http://localhost:8081/maps`);
};

export const mapCreate = ({
mapUid = faker.string.uuid(),
authorTime = faker.number.int({ min: 1, max: 20000 }),
Expand Down
53 changes: 43 additions & 10 deletions test/api/map.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { afterAll, beforeAll, expect, test } from "bun:test";
import { faker } from "@faker-js/faker";
import { Pool } from "pg";
import { mapCreate, mapGet, mapGetAll, playerAdminCreate } from "./api";
import {
mapCreate,
mapGet,
mapGetAll,
mapGetCampaign,
playerAdminCreate,
} from "./api";

let pool: Pool;

Expand All @@ -21,11 +27,6 @@ test("get map dne", async () => {
expect(response.status).toEqual(400);
});

test("get map, no mapuid", async () => {
const response = await fetch(`http://localhost:8081/maps`);
expect(response.status).toEqual(400);
});

test("create map no adminkey", async () => {
const response = await mapCreate({
headers: {},
Expand Down Expand Up @@ -293,7 +294,7 @@ test("create map with properties repeat without properties doesnt override optio
expect(json.map.nadeo).toEqual(nadeo);
});

test("get all, campaign dne", async () => {
test("get campaign, campaign dne", async () => {
const apikey = await playerAdminCreate(pool);
const mapUid = faker.string.uuid();
const name = faker.word.words(3);
Expand All @@ -318,14 +319,14 @@ test("get all, campaign dne", async () => {

expect(response.status).toEqual(200);

const mapResponse = await mapGetAll("notfound");
const mapResponse = await mapGetCampaign("notfound");
const json = await mapResponse.json();

expect(json.maps).toBeDefined();
expect(json.maps).toHaveLength(0);
});

test("get all", async () => {
test("get campaign", async () => {
const apikey = await playerAdminCreate(pool);
const mapUid = faker.string.uuid();
const name = faker.word.words(3);
Expand All @@ -350,7 +351,7 @@ test("get all", async () => {

expect(response.status).toEqual(200);

const mapResponse = await mapGetAll(campaign);
const mapResponse = await mapGetCampaign(campaign);
const json = await mapResponse.json();

expect(json.maps).toBeDefined();
Expand All @@ -363,3 +364,35 @@ test("get all", async () => {
expect(json.maps[0].totdDate).toEqual(totdDate);
expect(json.maps[0].nadeo).toEqual(nadeo);
});

test("get all", async () => {
const apikey = await playerAdminCreate(pool);
const mapUid = faker.string.uuid();
const name = faker.word.words(3);
const authorTime = faker.number.int({ min: 1, max: 20000 });
const campaign = faker.word.words(4);
const campaignIndex = 3;
const totdDate = "2024-01-01";
const nadeo = true;

const response = await mapCreate({
body: {
mapUid,
name,
authorTime,
campaign,
campaignIndex,
totdDate,
nadeo,
},
apikey,
});

expect(response.status).toEqual(200);

const mapResponse = await mapGetAll();
const json = await mapResponse.json();

expect(json.maps).toBeDefined();
expect(json.maps.length).toBeGreaterThanOrEqual(1);
});

0 comments on commit 3ff26e4

Please sign in to comment.