From d8b66fb6e492c29ac263430c04a42be73ba39200 Mon Sep 17 00:00:00 2001 From: George Goodall Date: Fri, 2 Aug 2024 15:39:01 +0100 Subject: [PATCH] get organisations from datasette --- src/controllers/OrganisationsController.js | 97 +++++----------------- 1 file changed, 22 insertions(+), 75 deletions(-) diff --git a/src/controllers/OrganisationsController.js b/src/controllers/OrganisationsController.js index 5b65911c..301c717c 100644 --- a/src/controllers/OrganisationsController.js +++ b/src/controllers/OrganisationsController.js @@ -1,3 +1,4 @@ +import datasette from '../services/datasette.js' import performanceDbApi from '../services/performanceDbApi.js' // Assume you have an API service module import logger from '../utils/logger.js' import { dataSubjects } from '../utils/utils.js' @@ -70,85 +71,31 @@ const organisationsController = { } }, + /** + * Handles the GET /organisations request + * + * @param {Request} req + * @param {Response} res + * @param {NextFunction} next + */ async getOrganisations (req, res, next) { - const alphabetisedOrgs = { - A: [ - { - name: 'Aberdeen' - }, - { - name: 'Aylesbury' - }, - { - name: 'Ashford' - } - ], - B: [ - { - name: 'Bath' - }, - { - name: 'Birmingham' - }, - { - name: 'Brighton' - } - ], - C: [ - { - name: 'Cambridge' - }, - { - name: 'Cardiff' - }, - { - name: 'Cheltenham' - }, - { - name: 'Chester' - } - ], - D: [ - { - name: 'Derby' - }, - { - name: 'Dundee' - } - ], - E: [ - { - name: 'Edinburgh' - }, - { - name: 'Epsom' - } - ], - G: [ - { - name: 'Glasgow' - }, - { - name: 'Gloucester' - } - ], - H: [ - { - name: 'Hull' - } - ], - L: [ - { - name: 'Leeds' - }, - { - name: 'London' - } - ] - } + const sql = 'select name, organisation from organisation' + const result = await datasette.runQuery(sql) + + const sortedResults = result.formattedData.sort((a, b) => { + return a.name.localeCompare(b.name) + }) + + const alphabetisedOrgs = sortedResults.reduce((acc, current) => { + const firstLetter = current.name.charAt(0).toUpperCase() + acc[firstLetter] = acc[firstLetter] || [] + acc[firstLetter].push(current) + return acc + }, {}) res.render('organisations/find.html', { alphabetisedOrgs }) } + } export default organisationsController