From 010a7f4d595a2815fb605c0c7314811f01ea6ed6 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 1 Feb 2023 11:07:57 -0500 Subject: [PATCH] map next and previous --- handlers/lots-get/next.js | 4 ++- handlers/lots-get/next.ts | 4 ++- handlers/maps-get/next.d.ts | 3 ++ handlers/maps-get/next.js | 15 ++++++++ handlers/maps-get/next.ts | 30 ++++++++++++++++ handlers/maps-get/previous.d.ts | 3 ++ handlers/maps-get/previous.js | 15 ++++++++ handlers/maps-get/previous.ts | 30 ++++++++++++++++ helpers/lotOccupancyDB/getNextMapId.d.ts | 2 ++ helpers/lotOccupancyDB/getNextMapId.js | 18 ++++++++++ helpers/lotOccupancyDB/getNextMapId.ts | 30 ++++++++++++++++ helpers/lotOccupancyDB/getPreviousMapId.d.ts | 2 ++ helpers/lotOccupancyDB/getPreviousMapId.js | 17 +++++++++ helpers/lotOccupancyDB/getPreviousMapId.ts | 29 +++++++++++++++ routes/maps.js | 4 +++ routes/maps.ts | 7 ++++ views/map-view.ejs | 37 +++++++++++++++----- 17 files changed, 239 insertions(+), 11 deletions(-) create mode 100644 handlers/maps-get/next.d.ts create mode 100644 handlers/maps-get/next.js create mode 100644 handlers/maps-get/next.ts create mode 100644 handlers/maps-get/previous.d.ts create mode 100644 handlers/maps-get/previous.js create mode 100644 handlers/maps-get/previous.ts create mode 100644 helpers/lotOccupancyDB/getNextMapId.d.ts create mode 100644 helpers/lotOccupancyDB/getNextMapId.js create mode 100644 helpers/lotOccupancyDB/getNextMapId.ts create mode 100644 helpers/lotOccupancyDB/getPreviousMapId.d.ts create mode 100644 helpers/lotOccupancyDB/getPreviousMapId.js create mode 100644 helpers/lotOccupancyDB/getPreviousMapId.ts diff --git a/handlers/lots-get/next.js b/handlers/lots-get/next.js index 863c4fda..226d2810 100644 --- a/handlers/lots-get/next.js +++ b/handlers/lots-get/next.js @@ -8,6 +8,8 @@ export async function handler(request, response) { '/lots/?error=noNextLotIdFound'); return; } - response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId.toString()); + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + + '/lots/' + + nextLotId.toString()); } export default handler; diff --git a/handlers/lots-get/next.ts b/handlers/lots-get/next.ts index bb975b8e..97b871b7 100644 --- a/handlers/lots-get/next.ts +++ b/handlers/lots-get/next.ts @@ -21,7 +21,9 @@ export async function handler( } response.redirect( - configFunctions.getProperty('reverseProxy.urlPrefix') + '/lots/' + nextLotId.toString() + configFunctions.getProperty('reverseProxy.urlPrefix') + + '/lots/' + + nextLotId.toString() ) } diff --git a/handlers/maps-get/next.d.ts b/handlers/maps-get/next.d.ts new file mode 100644 index 00000000..7c872b55 --- /dev/null +++ b/handlers/maps-get/next.d.ts @@ -0,0 +1,3 @@ +import type { Request, Response } from 'express'; +export declare function handler(request: Request, response: Response): Promise; +export default handler; diff --git a/handlers/maps-get/next.js b/handlers/maps-get/next.js new file mode 100644 index 00000000..523804d7 --- /dev/null +++ b/handlers/maps-get/next.js @@ -0,0 +1,15 @@ +import * as configFunctions from '../../helpers/functions.config.js'; +import { getNextMapId } from '../../helpers/lotOccupancyDB/getNextMapId.js'; +export async function handler(request, response) { + const mapId = Number.parseInt(request.params.mapId, 10); + const nextMapId = await getNextMapId(mapId); + if (nextMapId === undefined) { + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/?error=noNextMapIdFound'); + return; + } + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/' + + nextMapId.toString()); +} +export default handler; diff --git a/handlers/maps-get/next.ts b/handlers/maps-get/next.ts new file mode 100644 index 00000000..45f83ab2 --- /dev/null +++ b/handlers/maps-get/next.ts @@ -0,0 +1,30 @@ +import type { Request, Response } from 'express' + +import * as configFunctions from '../../helpers/functions.config.js' + +import { getNextMapId } from '../../helpers/lotOccupancyDB/getNextMapId.js' + +export async function handler( + request: Request, + response: Response +): Promise { + const mapId = Number.parseInt(request.params.mapId, 10) + + const nextMapId = await getNextMapId(mapId) + + if (nextMapId === undefined) { + response.redirect( + configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/?error=noNextMapIdFound' + ) + return + } + + response.redirect( + configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/' + + nextMapId.toString() + ) +} + +export default handler diff --git a/handlers/maps-get/previous.d.ts b/handlers/maps-get/previous.d.ts new file mode 100644 index 00000000..7c872b55 --- /dev/null +++ b/handlers/maps-get/previous.d.ts @@ -0,0 +1,3 @@ +import type { Request, Response } from 'express'; +export declare function handler(request: Request, response: Response): Promise; +export default handler; diff --git a/handlers/maps-get/previous.js b/handlers/maps-get/previous.js new file mode 100644 index 00000000..60e0879e --- /dev/null +++ b/handlers/maps-get/previous.js @@ -0,0 +1,15 @@ +import * as configFunctions from '../../helpers/functions.config.js'; +import { getPreviousMapId } from '../../helpers/lotOccupancyDB/getPreviousMapId.js'; +export async function handler(request, response) { + const mapId = Number.parseInt(request.params.mapId, 10); + const previousMapId = await getPreviousMapId(mapId); + if (previousMapId === undefined) { + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/?error=noPreviousMapIdFound'); + return; + } + response.redirect(configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/' + + previousMapId.toString()); +} +export default handler; diff --git a/handlers/maps-get/previous.ts b/handlers/maps-get/previous.ts new file mode 100644 index 00000000..a8414c08 --- /dev/null +++ b/handlers/maps-get/previous.ts @@ -0,0 +1,30 @@ +import type { Request, Response } from 'express' + +import * as configFunctions from '../../helpers/functions.config.js' + +import { getPreviousMapId } from '../../helpers/lotOccupancyDB/getPreviousMapId.js' + +export async function handler( + request: Request, + response: Response +): Promise { + const mapId = Number.parseInt(request.params.mapId, 10) + + const previousMapId = await getPreviousMapId(mapId) + + if (previousMapId === undefined) { + response.redirect( + configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/?error=noPreviousMapIdFound' + ) + return + } + + response.redirect( + configFunctions.getProperty('reverseProxy.urlPrefix') + + '/maps/' + + previousMapId.toString() + ) +} + +export default handler diff --git a/helpers/lotOccupancyDB/getNextMapId.d.ts b/helpers/lotOccupancyDB/getNextMapId.d.ts new file mode 100644 index 00000000..d5749e33 --- /dev/null +++ b/helpers/lotOccupancyDB/getNextMapId.d.ts @@ -0,0 +1,2 @@ +export declare function getNextMapId(mapId: number | string): Promise; +export default getNextMapId; diff --git a/helpers/lotOccupancyDB/getNextMapId.js b/helpers/lotOccupancyDB/getNextMapId.js new file mode 100644 index 00000000..3f31c192 --- /dev/null +++ b/helpers/lotOccupancyDB/getNextMapId.js @@ -0,0 +1,18 @@ +import { acquireConnection } from './pool.js'; +export async function getNextMapId(mapId) { + const database = await acquireConnection(); + const result = database + .prepare(`select mapId + from Maps + where recordDelete_timeMillis is null + and mapName > (select mapName from Maps where mapId = ?) + order by mapName + limit 1`) + .get(mapId); + database.release(); + if (result === undefined) { + return undefined; + } + return result.mapId; +} +export default getNextMapId; diff --git a/helpers/lotOccupancyDB/getNextMapId.ts b/helpers/lotOccupancyDB/getNextMapId.ts new file mode 100644 index 00000000..7c9c8ab4 --- /dev/null +++ b/helpers/lotOccupancyDB/getNextMapId.ts @@ -0,0 +1,30 @@ +import { acquireConnection } from './pool.js' + +export async function getNextMapId( + mapId: number | string +): Promise { + const database = await acquireConnection() + + const result: { + mapId: number + } = database + .prepare( + `select mapId + from Maps + where recordDelete_timeMillis is null + and mapName > (select mapName from Maps where mapId = ?) + order by mapName + limit 1` + ) + .get(mapId) + + database.release() + + if (result === undefined) { + return undefined + } + + return result.mapId +} + +export default getNextMapId diff --git a/helpers/lotOccupancyDB/getPreviousMapId.d.ts b/helpers/lotOccupancyDB/getPreviousMapId.d.ts new file mode 100644 index 00000000..d8a81668 --- /dev/null +++ b/helpers/lotOccupancyDB/getPreviousMapId.d.ts @@ -0,0 +1,2 @@ +export declare function getPreviousMapId(mapId: number | string): Promise; +export default getPreviousMapId; diff --git a/helpers/lotOccupancyDB/getPreviousMapId.js b/helpers/lotOccupancyDB/getPreviousMapId.js new file mode 100644 index 00000000..1ff31c1c --- /dev/null +++ b/helpers/lotOccupancyDB/getPreviousMapId.js @@ -0,0 +1,17 @@ +import { acquireConnection } from './pool.js'; +export async function getPreviousMapId(mapId) { + const database = await acquireConnection(); + const result = database + .prepare(`select mapId from Maps + where recordDelete_timeMillis is null + and mapName < (select mapName from Maps where mapId = ?) + order by mapName desc + limit 1`) + .get(mapId); + database.release(); + if (result === undefined) { + return undefined; + } + return result.mapId; +} +export default getPreviousMapId; diff --git a/helpers/lotOccupancyDB/getPreviousMapId.ts b/helpers/lotOccupancyDB/getPreviousMapId.ts new file mode 100644 index 00000000..c07f70a8 --- /dev/null +++ b/helpers/lotOccupancyDB/getPreviousMapId.ts @@ -0,0 +1,29 @@ +import { acquireConnection } from './pool.js' + +export async function getPreviousMapId( + mapId: number | string +): Promise { + const database = await acquireConnection() + + const result: { + mapId: number + } = database + .prepare( + `select mapId from Maps + where recordDelete_timeMillis is null + and mapName < (select mapName from Maps where mapId = ?) + order by mapName desc + limit 1` + ) + .get(mapId) + + database.release() + + if (result === undefined) { + return undefined + } + + return result.mapId +} + +export default getPreviousMapId diff --git a/routes/maps.js b/routes/maps.js index f76b2028..7c4720b8 100644 --- a/routes/maps.js +++ b/routes/maps.js @@ -2,6 +2,8 @@ import { Router } from 'express'; import * as permissionHandlers from '../handlers/permissions.js'; import handler_search from '../handlers/maps-get/search.js'; import handler_view from '../handlers/maps-get/view.js'; +import handler_next from '../handlers/maps-get/next.js'; +import handler_previous from '../handlers/maps-get/previous.js'; import handler_new from '../handlers/maps-get/new.js'; import handler_edit from '../handlers/maps-get/edit.js'; import handler_doCreateMap from '../handlers/maps-post/doCreateMap.js'; @@ -11,6 +13,8 @@ export const router = Router(); router.get('/', handler_search); router.get('/new', permissionHandlers.updateGetHandler, handler_new); router.get('/:mapId', handler_view); +router.get('/:mapId/next', handler_next); +router.get('/:mapId/previous', handler_previous); router.get('/:mapId/edit', permissionHandlers.updateGetHandler, handler_edit); router.post('/doCreateMap', permissionHandlers.updatePostHandler, handler_doCreateMap); router.post('/doUpdateMap', permissionHandlers.updatePostHandler, handler_doUpdateMap); diff --git a/routes/maps.ts b/routes/maps.ts index df2525c6..b1789cc7 100644 --- a/routes/maps.ts +++ b/routes/maps.ts @@ -5,6 +5,9 @@ import * as permissionHandlers from '../handlers/permissions.js' import handler_search from '../handlers/maps-get/search.js' import handler_view from '../handlers/maps-get/view.js' +import handler_next from '../handlers/maps-get/next.js' +import handler_previous from '../handlers/maps-get/previous.js' + import handler_new from '../handlers/maps-get/new.js' import handler_edit from '../handlers/maps-get/edit.js' @@ -24,6 +27,10 @@ router.get( router.get('/:mapId', handler_view as RequestHandler) +router.get('/:mapId/next', handler_next as RequestHandler) + +router.get('/:mapId/previous', handler_previous as RequestHandler) + router.get( '/:mapId/edit', permissionHandlers.updateGetHandler, diff --git a/views/map-view.ejs b/views/map-view.ejs index dbad376c..3b02926e 100644 --- a/views/map-view.ejs +++ b/views/map-view.ejs @@ -21,14 +21,33 @@ <%= map.mapName || "(No Name)" %> -<% if (user.userProperties.canUpdate) { %> -
-
- - <%= map.mapName || "(No Name)" %> - + +