Skip to content

Commit

Permalink
Merge pull request #25 from badgeteam/refactor/dir-structure-and-ts-s…
Browse files Browse the repository at this point in the history
…ettings

Refactor dir structure, tsoa and ts settings
  • Loading branch information
edwinm authored Oct 31, 2024
2 parents 4e20689 + 04e86c0 commit 2342d69
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 78 deletions.
33 changes: 33 additions & 0 deletions public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,39 @@
}
]
}
},
"/api/v3/apps/{slug}/version": {
"post": {
"operationId": "CreateVersion",
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/Device"
},
"type": "array"
}
}
}
}
},
"description": "Get list of devices (badges)",
"tags": ["public"],
"security": [],
"parameters": [
{
"in": "path",
"name": "slug",
"required": true,
"schema": {
"type": "string"
}
}
]
}
}
},
"servers": [
Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import { RegisterRoutes } from "./generated/routes.js";
import openapi from "./openapi.js";
import { RegisterRoutes } from "./generated/routes";
import openapi from "./openapi";
import { pinoHttp } from "pino-http";

const app = express();
Expand Down
14 changes: 14 additions & 0 deletions src/controllers/private-rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Path, Post, Route, Tags } from "tsoa";
import { Device } from "../db/models";

@Route("/api/v3")
@Tags("public")
export class PrivateRestController {
/**
* Get list of devices (badges)
*/
@Post("/apps/{slug}/version")
public async createVersion(@Path() slug: string): Promise<Device[]> {
throw new Error("Not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test, expect } from "vitest";
import request from "supertest";
import app from "./app";
import app from "../app";

describe("API Routes", () => {
test("GET /vitest", async () => {
Expand Down
65 changes: 20 additions & 45 deletions src/public-rest.ts → src/controllers/public-rest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pg from "pg";
import pg, { Pool } from "pg";
import { Get, Path, Query, Res, Route, Tags } from "tsoa";
import type { TsoaResponse } from "tsoa";
import { getPool } from "../db/connectionPool";
import { App, AppDetails, Category, Device } from "../db/models";

/**
* The code is annotated so that OpenAPI documentation can be generated with tsoa
Expand All @@ -14,49 +16,22 @@ import type { TsoaResponse } from "tsoa";
* npm run swagger
*/

const pool = new pg.Pool({
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: 5432,
});

interface Device {
name: string;
slug: string;
}

interface Category {
name: string;
slug: string;
}

interface App {
name: string;
slug: string;
category_slug: string;
user_name: string;
}

interface AppDetails {
name: string;
slug: string;
description: string;
category_slug: string;
user_name: string;
devices: string[];
}

@Route("/api/v3")
@Tags("public")
export class RestController {
export class PublicRestController {
private pool: Pool;
public constructor() {
this.pool = getPool();
}

/**
* Get list of devices (badges)
*/
@Get("/devices")
public async getDevices(): Promise<Device[]> {
const result = await pool.query<Device>(`select name, slug from badges`);
const result = await this.pool.query<Device>(
`select name, slug from badges`
);
return result.rows;
}

Expand All @@ -65,7 +40,7 @@ export class RestController {
*/
@Get("/categories")
public async getCategories(): Promise<Category[]> {
const result = await pool.query<Category>(
const result = await this.pool.query<Category>(
`select name, slug from categories`
);
return result.rows;
Expand Down Expand Up @@ -93,31 +68,31 @@ export class RestController {

let result: pg.QueryResult<App>;
if (category && !device) {
result = await pool.query<App>(
result = await this.pool.query<App>(
`${mainQuery}
where c.slug = $3
limit $1 offset $2
`,
[pageLength ?? null, pageStart ?? 0, category]
);
} else if (!category && device) {
result = await pool.query<App>(
result = await this.pool.query<App>(
`${mainQuery} ${badgeQuery}
where b.slug=$3
limit $1 offset $2
`,
[pageLength ?? null, pageStart ?? 0, device]
);
} else if (category && device) {
result = await pool.query<App>(
result = await this.pool.query<App>(
`${mainQuery} ${badgeQuery}
where c.slug = $3 and b.slug=$4
limit $1 offset $2
`,
[pageLength ?? null, pageStart ?? 0, category, device]
);
} else {
result = await pool.query<App>(
result = await this.pool.query<App>(
`${mainQuery}
limit $1 offset $2
`,
Expand All @@ -135,7 +110,7 @@ export class RestController {
@Path() slug: string,
@Res() notFoundResponse: TsoaResponse<404, { reason: string }>
): Promise<AppDetails | undefined> {
const result = await pool.query<AppDetails & { id: number }>(
const result = await this.pool.query<AppDetails & { id: number }>(
`select p.id, p.name, p.slug, p.description, c.slug as category_slug, u.name as user_name
from projects p
inner join categories c on p.category_id = c.id
Expand All @@ -145,11 +120,11 @@ export class RestController {
);
if (result.rows[0]) {
const projectId = result.rows[0].id;
const badgeResult = await pool.query<AppDetails & { id: number }>(
const badgeResult = await this.pool.query<AppDetails & { id: number }>(
`select b.slug from badge_project bp inner join badges b on bp.badge_id=b.id where project_id=$1`,
[projectId]
);
const devices = badgeResult.rows.map((badge) => badge.slug);
const devices = badgeResult.rows.map((badge: Device) => badge.slug);
const { id, ...resultWithoutId } = result.rows[0];
return { ...resultWithoutId, devices };
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/db/connectionPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pg from "pg";
let pool: pg.Pool;
export const getPool = () => {
if (!pool) {
pool = new pg.Pool({
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: 5432,
});
}
return pool;
};
25 changes: 25 additions & 0 deletions src/db/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface Device {
name: string;
slug: string;
}

export interface Category {
name: string;
slug: string;
}

export interface App {
name: string;
slug: string;
category_slug: string;
user_name: string;
}

export interface AppDetails {
name: string;
slug: string;
description: string;
category_slug: string;
user_name: string;
devices: string[];
}
Loading

0 comments on commit 2342d69

Please sign in to comment.